【问题标题】:Using WebElements from PageFactory POMs in my test class在我的测试类中使用来自 PageFactory POM 的 WebElements
【发布时间】:2017-04-01 04:34:13
【问题描述】:

基本上,我想(从我的测试类)断言 WebElement 包含 text()。由于我所有的 WebElements 都在我的 page.class 中定义,我认为我必须将它们公开才能做到这一点。

我在使用 webdriver 和元素时遇到了一些问题,我认为这可能是因为多个测试类同时从页面类访问 WebElements。我的问题是:WebElements 必须是私有的有什么原因吗?

代码示例:

我见过的所有 PageFactory 教程都说要让你的 WebElements 私有,比如

    @FindBy(xpath = "//*[@id='searchStringMain']")
    private WebElement searchField;

但要断言一个元素包含文本(来自另一个类),我必须像这样定义它们:

    @FindBy(xpath = "(//*[contains (text(),'Hrs')])[2]")
    public static WebElement yourLoggedTime;

【问题讨论】:

  • PageObjects 的目的是封装有关页面元素的详细信息。如果您将它们公开,那么当(而不是如果)其中一个发生更改时,您将不得不寻找您引用它的所有地方并复制更改。通过将 Web 元素封装在页面对象中,您只需更改一次。您的测试应该关注行为(例如 searchFor),而不是有关操作哪些元素的详细信息。元素详细信息只能在 PageObjects 中找到。

标签: java selenium selenium-webdriver pageobjects


【解决方案1】:

考虑这个例子:

package org.openqa.selenium.example;

import org.openqa.selenium.By;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.WebElement;

public class GoogleSearchPage {
    // The element is now looked up using the name attribute
    @FindBy(how = How.NAME, using = "q")
    private WebElement searchBox;


    public void searchFor(String text) {
        // We continue using the element just as before
        searchBox.sendKeys(text);
        searchBox.submit();
    }
}

searchbox 是私有的,但方法 searchFor 是公共的。测试将使用 searchFor 但从不使用 searchBox。

我通常把页面工厂的initElements调用放在页面构造函数的最后一行。这使得所有公共功能都可用于测试。所以

package org.openqa.selenium.example;

import org.openqa.selenium.By;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.WebElement;

public class GoogleSearchPage {
    public WebDriver driver;
    // The element is now looked up using the name attribute
    @FindBy(how = How.NAME, using = "q")
    private WebElement searchBox;

    public GoogleSearchPage(WebDriver driver) {
        this.driver = driver
        PageFactory.initElements(driver, this);
    }


    public void searchFor(String text) {
        // We continue using the element just as before
        searchBox.sendKeys(text);
        searchBox.submit();
    }
}

在您的测试中,您可以执行以下操作:

new GoogleSearchPage(webDriver).searchFor("Foo");

【讨论】:

    【解决方案2】:

    您可以将您的类归档公开,但作为标准做法,建议对类归档使用 getter 方法,而不是直接归档访问。
    问题是 static 关键字您与公共一起使用。当您使用带有字段(webelement)的@FindBy 注释时,它会在类初始化时初始化(或取决于您调用initElements() 的实现)。所以有 webelement public 但不是 static 很好。例如:

    在您的页面类中:

    @FindBy(xpath = "(//*[contains (text(),'Hrs')])[2]")
    public WebElement yourLoggedTime;
    

    测试中:

    String yourLoggedTime = pageObj.yourLoggedTime.getText(); //pageObj is object of your page class
    

    【讨论】:

    • 通过暴露 WebElement 来打破封装,如果在许多测试中使用 yourLoggedTime 可能很重要。我宁愿有一个描述行为并返回字符串的页面对象方法,但我认为这是一个品味问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-09
    • 2014-01-25
    • 1970-01-01
    • 2021-11-12
    相关资源
    最近更新 更多