简体   繁体   English

Page对象工厂中的硒声明

[英]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.

相关问题 Selenium Grid与页面对象模型页面工厂 - Selenium Grid With Page Object Model Page Factory 硒中的页面对象模型和页面工厂有什么区别? - What is the differnce between page object model and page factory in selenium? Selenium [Java] PageFactory设计:在页面对象模型之后,我该在哪里编写断言 - Selenium [Java] PageFactory Design : Where do I write my Assertions following Page Object Model 无法在页面 Object Class 中调用“org.openqa.selenium.WebDriver.getTitle()”和工厂 - Cannot invoke "org.openqa.selenium.WebDriver.getTitle()" in Page Object Class using Selenium and Page Factory 页面工厂退货 Selenium 中的 null - Page factory returns null in Selenium 页面对象工厂-NullPointerException - Page Object Factory - NullPointerException 使用 Page Factory Design 和 Page Object Model 进行测试,使用 Selenium 和 ZD5238788375ZA21833187880E1EA218 打开浏览器的两个实例 - Tests using Page Factory Design and Page Object Model opens two instances of the browser using Selenium and Java 在使用带有Selenium Webdriver的Page Factory来实现页面对象建模时,这两者之间的好方法是什么? - While implementing the page object modelling using page factory with selenium webdriver, which is the good approach out of the two? Selenium,Page Factory,无法使用PageFactory继承类 - Selenium, Page Factory,Was not able to inherit a class with PageFactory 硒页工厂未捕获的下拉值 - Drop down value not captured with selenium page factory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM