2011-09-30 40 views
4

不知道這是不是一個體面的問題,但它在這裏。我們正在努力實現一個UI測試框架(硒網絡驅動程序),並希望使用頁面驅動設計例如Java註釋從中刪除字符串文字?

class HomePage { 
@FindBy(how = How.Id, id="myPageHeaderID") 
private String pageHeader 

在簡單的例子,上面我需要硬編碼「myPageHeaderID」字符串文字。提出的一個要求是,我們可以從屬性中提取「myPageHeaderID」,出於維護原因(如果某些事情發生變化,不需要部署代碼)以及國際化原因。我一直在四處搜尋,可能沒有進行適當的搜索,但有什麼方法可以做我上面要求的?

+1

註釋屬性必須是常量表達式。所以你將無法從外部資源加載ID。您可以使用一個需要將常量鍵作爲屬性的註釋,然後使用該註釋的工具將從資源束中加載與該鍵關聯的值。 –

+0

@JBNizet是的,不斷的表達是我目前的路障。我一直在尋找資源包的東西,並沒有發現任何有用的東西,有什麼建議嗎?否則,我會繼續尋找。 – Cromat

回答

-1

AFAIK,你可以從Annotation中調用一個方法。

@FindBy(how = How.Id, id=getProp()) 
private String pageHeader; 

private String getProp() 
{ 
    String prop = //whatever way you want to get the value 
    return prop; 
} 

這不正常嗎?

+1

否,因爲大多數註釋會給您以下錯誤:註釋屬性******的值必須是常量表達式 – Cromat

+1

對不起,沒有測試它。看來只有通過代碼工具才能獲得註釋上的「動態參數」。檢查這個問題http://stackoverflow.com/questions/2522746/dynamic-class-annotation和這個鏈接http://ayoubelabbassi.blogspot.com/2011/01/how-to-add-annotations-at-runtime-to html的 – everton

0

我簡單地沿着這條路線走了,但由於我們的應用程序,它不是很容易實現(一旦你訪問過一個頁面,頁面並不總是以相同的順序顯示)。

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中創建常見的小部件,並注入到這些人哪個這些小部件的集合頁面對象。

從那裏我做了一個簡單的測試工具,其負責採取的字符串(然後將其處決。基本上,它允許測試案例被寫入規劃環境地政司和行爲,在其上注入了蠶豆。

它是我想到了一個漂亮整潔的項目,但我不得不貨架它得到一些其他的事情做好。

0

註解基本上。以數據庫的元數據例如,這將是奇怪的,如果Oracle數據庫將轉進入MySQL,對吧?這是article註釋變形金剛 in TestNG。沒有嘗試過,但我認爲它可以以某種方式實施。