WebDriver/PageObject/FindBy:如何指定具有动态值的xpath? [英] WebDriver/PageObject/FindBy: how to specify xpath with dynamic value?
问题描述
我正在尝试在Java中使用 Page Object 模式,并对 @ FindBy/XPath 遇到麻烦.
之前,我在Groovy中使用了以下构造:
driver.findElement(By.xpath("//td[contains(text(),'$SystemName')]")).click()
Here, SystemName is a parameter that can be different.
现在,我想做同样的事情,但要遵循Java中的Page Object范式:
public class ManagedSystems {
private WebDriver driver;
@FindBy(id="menu_NewSystem")
private WebElement menuNewSystem;
@FindBy (xpath = "//td[contains(text(),'$SystemName')]") // ??? How to use SystemName from deleteSystem method (below)?
private WebElement plantSystemName;
....
public SystemHomePage deleteSystem (String systemName) {
plantSystemName.click();
}
}
在我的测试中,我调用deleteSystem:
SystemHomePage.deleteSystem("Firestone");
问题:如何链接为PlantSystemName的 @FindBy表示法和为deleteSystem指定的 SystemName ?
谢谢, 浣熊
为此,我使用了另一种解决方法,即使在页面工厂的情况下,我们也可以使用动态xpath.
解决方案:添加任何静态父元素的xpath并引用具有动态路径的子元素. 在您的情况下,//td [contains(text(),'$ SystemName')),td的父元素可能是'tr'或'table'.如果表是静态的,请使用以下代码:
@FindBy(xpath = "<..table xpath>")
public WebElement parentElement;
public WebElement getDynamicEmement(String SystemName){
parentElement.findElement(By.xpath("//tr/td[contains(text(),'"+SystemName+"')]"))
}
现在,在脚本中,首先访问表(以便将其引用加载到内存中),然后调用getDynamicElement方法.
waitForElement(parentElement)
getDynamicElement("System-A")
I'm trying to use Page Object pattern in Java and having some trouble with @FindBy/XPath.
Earlier, I used the following construction in Groovy:
driver.findElement(By.xpath("//td[contains(text(),'$SystemName')]")).click()
Here, SystemName is a parameter that can be different.
Now, I want to do the same thing but in accordance with Page Object paradigm in Java:
public class ManagedSystems {
private WebDriver driver;
@FindBy(id="menu_NewSystem")
private WebElement menuNewSystem;
@FindBy (xpath = "//td[contains(text(),'$SystemName')]") // ??? How to use SystemName from deleteSystem method (below)?
private WebElement plantSystemName;
....
public SystemHomePage deleteSystem (String systemName) {
plantSystemName.click();
}
}
In my test, I call deleteSystem:
SystemHomePage.deleteSystem("Firestone");
Question: How to link @FindBy notation for PlantSystemName and SystemName specified for deleteSystem?
Thanks, Racoon
I used another workaround for this which will allow us to use dynamic xpath even with page factory.
Solution: Add the xpath of any parent element that is static and reference child element with dynamic path. In your case, //td[contains(text(),'$SystemName'), parent element of td might be 'tr' or 'table'. If table is static, use below code:
@FindBy(xpath = "<..table xpath>")
public WebElement parentElement;
public WebElement getDynamicEmement(String SystemName){
parentElement.findElement(By.xpath("//tr/td[contains(text(),'"+SystemName+"')]"))
}
Now in your script, access table first(so that its reference is loaded in memory) and then call the getDynamicElement method.
waitForElement(parentElement)
getDynamicElement("System-A")
这篇关于WebDriver/PageObject/FindBy:如何指定具有动态值的xpath?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!