2010-10-20 97 views
11

我真的很喜歡selenium 2按照慣例推動你使用PageObjects作爲POJO,然後使用PageFactory來實例化這個類中的字段。硒頁面對象重用

我發現限制的是我們在很多不同的頁面上重複使用了很多元素。最大的問題是這些重複使用的組件在它們出現在不同的頁面上時沒有相同的ID /名稱;然而我們爲每個人運行的測試都是一樣的。

作爲一個例子,我們在很多地方收集日期。因此,對於這樣的一個例子頁面對象可以是(月,日字段中刪除):

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(); 
    } 
} 

我真正喜歡做的事是有一個設置,藉此Spring可以將該id/name/xpath等注入到應用程序中。

有沒有一種方法可以做到這一點,而不會失去利用PageFactory的能力?

編輯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

你可以建立通用的Web元素(只是發明了這個名字:))你的頁面對象 - 每個CWE將是一個「小部件」,即使用在不同的頁面上。在你的例子中,這將是某種日期小工具 - 它包含年,月和日。基本上它將是一個頁面對象。

PageFactory要求在@FindBy註釋中使用字符串常量。

要解決這個限制,我們創建了我們自己的ElementLocator s。

您可以在測試中使用的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

這無疑得到了我正確的軌道上。我希望進一步擴展它不僅僅是ID(你可以注入任何需要的方法,然後找到)。我的問題是我沒有看到這些Field是如何創建的 - 這導致我無法正確掌握ElementLocator。 – Scott 2010-10-21 16:56:23