[英]Assertions in selenium with Page Object Factory
How can I use the path/location of a @FindBy variable as an argument to a method? 如何使用@FindBy变量的路径/位置作为方法的参数?
I have the following @FindBy value in my class... 我的课上有以下@FindBy值...
@FindBy(xpath=".//*[@id='HasAnotherSubsidisedQual_container']")
@CacheLookup WebElement mSubsidisedQual;
I then have a method for checking whether an element exists... 然后,我有了一种检查元素是否存在的方法。
public boolean isElementPresent(By element){
try {
mDriver.findElement(element);
return true;
}
catch (org.openqa.selenium.NoSuchElementException e){
return false;
}
}
I then use that method in another method which contains an assertion 然后,我在另一个包含断言的方法中使用该方法
public void checkSmartAndSkilled () {
Assert.assertTrue(isElementPresent(By.xpath(".//*[@id='HasAnotherSubsidisedQual_container']")));
}
This all works fine, however instead of specifying By.xpath... etc in the assertion, is there anyway to pass in as an argument the path for my @FindBy WebElement mSubsidisedQual? 这一切都很好,但是不是在断言中指定By.xpath ...等,还是有将@FindBy WebElement mSubsidisedQual的路径作为参数传递吗?
Many thanks 非常感谢
You do not need to provide the xpath again. 您无需再次提供xpath。 Once it is initialized by Page Factory, simply pass the element as an argument. 由Page Factory初始化后,只需将元素作为参数传递即可。
public void checkSmartAndSkilled () {
Assert.assertTrue(isElementPresent(mSubsidisedQual));
}
You just created a wrong method. 您刚创建了错误的方法。 And the way you check element existence is wrong. 而且您检查元素存在的方式是错误的。 But if you want to do it this way then what you should do is to overload it so the parameter is your element. 但是,如果您想以这种方式进行操作,那么您应该做的就是对其进行重载,以使参数成为您的元素。
Then call the element (for example click, or use its property like Enable, or Count or Length or whatever is available in Java) and if the element doesn't exist it will catch the same error. 然后调用该元素(例如单击或使用其属性,如Enable或Count或Length或Java中可用的任何内容),如果该元素不存在,它将捕获相同的错误。 If it exists then return true. 如果存在,则返回true。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.