2017-02-21 87 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

实际上,您不需要将定位器保存到类字段。页面对象不一定必须声明类字段中的所有元素。页面对象是一个对象,这意味着它必须提供操作它的方法。

所以,你的解决方案仅仅是理想的。 :)

相关问题