我簡單地沿着這條路線走了,但由於我們的應用程序,它不是很容易實現(一旦你訪問過一個頁面,頁面並不總是以相同的順序顯示)。
public class PageElement implements WebElementAdapter, Locatable {
private How how;
private String using;
private boolean required;
@FindBy(how = How.ID_OR_NAME, using = DEFAULT_LOCATION_STRATEGY)
private WebElement backingElement;
public PageElement(How how, String using using) {
this.how = how;
this.using = using;
this.required = true;
}
/**
* This is how the overriding of the element location is done. I then injected
* these values in a spring configured bean file.
*
* This is needed on your config file:
* default-lazy-init="true" default-init-method="initialize">
*/
public final void initElement() {
if (backingElement == null || isStale() {
backingElement = getDriver().findElement(getLocationStrategy());
}
}
public By getLocationStrategy() {
By by = new ByIdOrName(using.replace(DEFAULT_LOCATION_STRATEGY, using));
switch(how) {
case CLASS_NAME:
by = By.className(using.replace(DEFAULT_LOCATION_STRATEGY, using));
break;
//Do for others
}
return by;
}
public WebElement getBackingElement() {
return backingElement;
}
}
public interface WebElementAdapter {
WebElement getBackingElement();
}
public interface Locatable {
By getLocationStrategy();
}
然後我POJO中創建常見的小部件,並注入到這些人哪個這些小部件的集合頁面對象。
從那裏我做了一個簡單的測試工具,其負責採取的字符串(然後將其處決。基本上,它允許測試案例被寫入規劃環境地政司和行爲,在其上注入了蠶豆。
它是我想到了一個漂亮整潔的項目,但我不得不貨架它得到一些其他的事情做好。
註釋屬性必須是常量表達式。所以你將無法從外部資源加載ID。您可以使用一個需要將常量鍵作爲屬性的註釋,然後使用該註釋的工具將從資源束中加載與該鍵關聯的值。 –
@JBNizet是的,不斷的表達是我目前的路障。我一直在尋找資源包的東西,並沒有發現任何有用的東西,有什麼建議嗎?否則,我會繼續尋找。 – Cromat