2010-10-20 9 views
11

मुझे वास्तव में पसंद है कि सम्मेलन द्वारा सेलेनियम 2 आपको POOO के रूप में पेजऑब्जेक्ट्स का उपयोग करने के लिए प्रेरित करता है, और उसके बाद बस इस श्रेणी में फ़ील्ड को तुरंत चालू करने के लिए पेजफैक्टरी का उपयोग करता है।सेलेनियम पेज ऑब्जेक्ट रीयूज

मुझे जो सीमित है, वह यह है कि हम कई अलग-अलग पृष्ठों पर कई तत्वों का पुन: उपयोग करते हैं। बड़ी समस्या यह है कि इन पुन: उपयोग किए गए घटकों के पास अलग-अलग पृष्ठों पर दिखाई देने पर एक ही आईडी/नाम नहीं होता है; हालांकि हम उनमें से प्रत्येक के लिए परीक्षण करेंगे, वही है।

उदाहरण के तौर पर हम कई स्थानों पर तिथियां एकत्र करते हैं। तो इस के लिए एक उदाहरण पेज वस्तु हो सकता है (माह, दिन खेतों हटाया):

public class DatePageObject { 
    private WebDriver driver; 

    DatePageObject(WebDriver driver) { 
     this.driver = driver; 
    } 

    @FindBy(id = "someIdForThisInstance") 
    private WebElement year; 

    public void testYearNumeric() { 
     this.year.sendKeys('aa'); 
     this.year.submit(); 
     //Logic to determine Error message shows up 
    } 
} 

तो मैं बस इस नीचे दिए गए कोड के साथ परीक्षण कर सकते हैं:

public class Test { 
    public static void main(String[] args) { 
     WebDriver driver = new FirefoxDriver(); 
     DatePageObject dpo = PageFactory.initElements(driver, DriverPageObject.class); 
     driver.get("Some URL"); 
     dpo.testYearNumeric(); 
    } 
} 

मैं वास्तव में क्या करना चाहते हैं क्या एक सेटअप है जिससे स्प्रिंग I के साथ उस आईडी/नाम/xpath, आदि ... इंजेक्शन में इंजेक्ट कर सकते हैं।

क्या कोई तरीका है कि मैं पेजफैक्टरी का उपयोग करने की क्षमता खोने के बिना ऐसा कर सकता हूं?

संपादित करें 1 - कस्टम लोकेटर और कारखानों पर काम कर रहे आदर्श आधार स्तर कक्षाएं जोड़ना।

public class PageElement { 
    private WebElement element; 
    private How how; 
    private String using; 

    PageElement(How how, String using) { 
     this.how = how; 
     this.using = using; 
    } 
    //Getters and Setters 
} 


public class PageWidget { 
    private List<PageElement> widgetElements; 
} 


public class Screen { 
    private List<PageWidget> fullPage; 
    private WebDriver driver; 

    public Screen(WebDriver driver) { 
     this.driver = driver; 
     for (PageWidget pw : fullPage) { 
      CustomPageFactory.initElements(driver, pw.class); 
     } 
} 

संपादित करें 2 - बस एक नोट के रूप में, जब तक आप सेलेनियम 2.0.a5 या अधिक से अधिक चल रहे हैं के रूप में, अब आप ड्राइवर एक अंतर्निहित टाइमआउट मान दे सकते हैं।

तो आप के साथ अपने कोड की जगह ले सकता:

private class CustomElementLocator implements ElementLocator { 
    private WebDriver driver; 
    private int timeOutInSeconds; 
    private final By by; 


    public CustomElementLocator(WebDriver driver, Field field, 
      int timeOutInSeconds) { 
     this.driver = driver; 
     this.timeOutInSeconds = timeOutInSeconds; 
     CustomAnnotations annotations = new CustomAnnotations(field); 
     this.by = annotations.buildBy(); 
     driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); //Set this value in a more realistic place 
    } 


    public WebElement findElement() { 
     return driver.findElement(by); 
    } 
} 

उत्तर

15

आप आम वेब तत्वों (सिर्फ इस नाम का आविष्कार किया :)) के अपने पृष्ठ वस्तु का निर्माण कर सकते हैं - प्रत्येक CWE प्रतिनिधित्व करते हैं एक "विजेट" है कि प्रयोग किया जाता है विभिन्न पृष्ठों पर। आपके उदाहरण में यह किसी प्रकार का दिनांक विजेट होगा - इसमें वर्ष, महीना और एक दिन शामिल है। असल में यह एक पृष्ठ वस्तु होगी।

PageFactory@FindBy एनोटेशन में स्ट्रिंग स्थिरांक का उपयोग करने की आवश्यकता है।

इस सीमा को हल करने के लिए हमने अपना खुद का ElementLocator बनाया।

आप अपने परीक्षण में DateWidget उपयोग कर सकते हैं:

.... 
DateWidget widget = new DateWidget(driver, "yearId", "monthId", "dayId"); 
.... 

public void testYearNumeric() { 
     widget.setYear("aa"); 
     widget.submit(); 
     //Logic to determine Error message shows up 

     // ... and day 
     widget.setDay("bb"); 
     widget.submit(); 
     //Logic to determine Error message shows up 
    } 

DateWidget वर्ग है, जो कस्टम लोकेटर और एनोटेशन पारसर्स होता है:

package pagefactory.test; 

import java.lang.reflect.Field; 

import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.support.FindBy; 
import org.openqa.selenium.support.PageFactory; 
import org.openqa.selenium.support.pagefactory.Annotations; 
import org.openqa.selenium.support.pagefactory.ElementLocator; 
import org.openqa.selenium.support.pagefactory.ElementLocatorFactory; 
import org.openqa.selenium.support.ui.ExpectedCondition; 
import org.openqa.selenium.support.ui.Wait; 
import org.openqa.selenium.support.ui.WebDriverWait; 

public class DateWidget { 

    // These constants are used to identify that they should be changed to the actual IDs 
    private static final String YEAR_ID = "$YEAR_ID$"; 
    private static final String MONTH_ID = "$MONTH_ID$"; 
    private static final String DAY_ID = "$DAY_ID$"; 

    // Elements whose ids will be replaced during run-time 
    /** Year element */ 
    @FindBy(id = YEAR_ID) 
    private WebElement year; 

    /** Month element */ 
    @FindBy(id = MONTH_ID) 
    private WebElement month; 

    /** day element */ 
    @FindBy(id = DAY_ID) 
    private WebElement day; 

    // The ids of the elements 
    /** ID of the year element */ 
    private String yearId; 

    /** ID of the month element */ 
    private String monthId; 

    /** ID of the day element */ 
    private String dayId; 

    public DateWidget(WebDriver driver, String yearId, String monthId, 
      String dayId) { 
     this.yearId = yearId; 
     this.monthId = monthId; 
     this.dayId = dayId; 

     PageFactory.initElements(new CustomLocatorFactory(driver, 15), this); 
    } 

    public String getYear() { 
     return year.getValue(); 
    } 

    public void setYear(String year) { 
     setValue(this.year, year); 
    } 

    public String getMonth() { 
     return month.getValue(); 
    } 

    public void setMonth(String month) { 
     setValue(this.month, month); 
    } 

    public String getDay() { 
     return day.getValue(); 
    } 

    public void setDay(String day) { 
     setValue(this.day, day); 
    } 

    public void submit() { 
     year.submit(); 
    } 

    private void setValue(WebElement field, String value) { 
     field.clear(); 
     field.sendKeys(value); 
    } 

    private class CustomLocatorFactory implements ElementLocatorFactory { 
     private final int timeOutInSeconds; 
     private WebDriver driver; 

     public CustomLocatorFactory(WebDriver driver, int timeOutInSeconds) { 
      this.driver = driver; 
      this.timeOutInSeconds = timeOutInSeconds; 
     } 

     public ElementLocator createLocator(Field field) { 
      return new CustomElementLocator(driver, field, timeOutInSeconds); 
     } 
    } 

    private class CustomElementLocator implements ElementLocator { 
     private WebDriver driver; 
     private int timeOutInSeconds; 
     private final By by; 

     public CustomElementLocator(WebDriver driver, Field field, 
       int timeOutInSeconds) { 
      this.driver = driver; 
      this.timeOutInSeconds = timeOutInSeconds; 
      CustomAnnotations annotations = new CustomAnnotations(field); 
      this.by = annotations.buildBy(); 
     } 

     @Override 
     public WebElement findElement() { 
      ExpectedCondition<Boolean> e = new ExpectedCondition<Boolean>() { 
       public Boolean apply(WebDriver d) { 
        d.findElement(by); 
        return Boolean.TRUE; 
       } 
      }; 
      Wait<WebDriver> w = new WebDriverWait(driver, timeOutInSeconds); 
      w.until(e); 

      return driver.findElement(by); 
     } 
    } 

    private class CustomAnnotations extends Annotations { 

     public CustomAnnotations(Field field) { 
      super(field); 
     } 

     @Override 
     protected By buildByFromShortFindBy(FindBy findBy) { 

      if (!"".equals(findBy.id())) { 
       String id = findBy.id(); 
       if (id.contains(YEAR_ID)) { 
        id = id.replace(YEAR_ID, yearId); 
        return By.id(id); 
       } else if (id.contains(MONTH_ID)) { 
        id = id.replace(MONTH_ID, monthId); 
        return By.id(id); 
       } else if (id.contains(DAY_ID)) { 
        id = id.replace(DAY_ID, dayId); 
        return By.id(id); 
       } 
      } 

      return super.buildByFromShortFindBy(findBy); 
     } 

    } 

} 
+0

यह निश्चित रूप से उचित पटरी पर मुझे हो गया है। मैं बस आईडी के मुकाबले इसे आगे बढ़ाने की उम्मीद कर रहा था (आप किसी भी साधन को इंजेक्ट कर सकते हैं और फिर पता लगा सकते हैं)। मेरी समस्या यह थी कि मैं नहीं देख रहा था कि उन फ़ील्ड कैसे बनाए जा रहे थे - जो मुझे एलिमेंट लोकेटर के ठीक से समझने के लिए प्रेरित नहीं कर रहा था। – Scott