2017-02-21 81 views
0

我想在我的UI測試中使用頁面對象模式。許多示例假設在類字段中保存By(Locator)。其他人建議保存WebElement(或SelenideElement,如果您使用的是Selenide)。雖然,兩者對於硬編碼定位器都很好,但我不明白如何將這個用於路徑包含變量的定位器。在Selenide/Selenium中定位模板的正確方法是什麼?

例如,如何在類字段保存此定位?

public SelenideElement getTotal(String type) { 
    return $(By.xpath("//h4[contains(text(), '"+ type +"')]"); 
} 

回答

1

您的解決方案在我看來是正確的。

我通常把它們放在我的PageObject的頂部,旁邊的其他選擇器就像你所做的一樣。只需使用該方法,就可以使用您的SelenideElement字段之一。喜歡的東西:

private SelenideElement getTotalElementByType(String type) { 
    return $(By.xpath("//h4[contains(text(), '"+ type +"')]"); 
} 

我會讓它privateprotected不過,因爲與頁面對象模式測試腳本不應該知道的WebElements頁面對象。

你可公開訪問的方法是這樣的:

public int getTotalByType(String type) { 
    string totalString = getTotalElementByType(type).getText(); 
    int total = //convert to int or whatever 
    return total; 
} 

如果你想與元素,而不是獲取值進行互動,你將返回PageObject你希望去,而不是跟隨POPattern。 :)

0

實際上,您不需要將定位器保存到類字段。頁面對象不一定必須聲明類字段中的所有元素。頁面對象是一個對象,這意味着它必須提供操作它的方法。

所以,你的解決方案僅僅是理想的。 :)

相關問題