简体   繁体   English

在测试类中使用PageFactory POM中的WebElements

[英]Using WebElements from PageFactory POMs in my test class

Basically, I would like to make an assertion (from my test class) that a WebElement contains text(). 基本上,我想(从我的测试类中)断言WebElement包含text()。 Since all of my WebElements are defined in my page.class, I think I have to make them public to do this. 由于我的所有WebElement都是在page.class中定义的,因此我认为必须公开它们。

I am running into a few problems with the webdriver and elements, and I think it may be because multiple test classes are accessing WebElements from the page class simultaneously. 我在遇到网络驱动程序和元素的一些问题,我认为这可能是因为多个测试类正在同时从页面类访问WebElement。 My question is: Is there a reason the WebElements must be private? 我的问题是:WebElements是否必须为私有是有原因的?

Code example: 代码示例:

All PageFactory tutorials I have seen say to make your WebElements private, like 我见过的所有PageFactory教程都说要使您的WebElements私有化,例如

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

But to assert that an element contains text (from another class), I have to define them like this: 但是要断言一个元素包含文本(来自另一个类),我必须这样定义它们:

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

Consider this example: 考虑以下示例:

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 is private but the method searchFor is public. searchbox是私有的,但searchFor方法是公共的。 The test would use searchFor but never searchBox. 该测试将使用searchFor,但不会使用searchBox。

I usually put the page factory initElements call in the last line of the page constructor. 我通常将页面工厂initElements调用放在页面构造函数的最后一行。 This makes all the public functions available to the tests. 这使得所有公共功能可用于测试。 So 所以

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();
    }
}

In your test you can do something like: 在测试中,您可以执行以下操作:

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

You can keep your class filed public but as a standard practice it is advised to have getter method for class filed rather than direct filed access. 您可以公开公开您的课程,但通常的做法是,建议对课程提交使用getter方法,而不要直接进行访问。
Problem is static keyword you are using with public. 问题是您与public一起使用的static关键字。 When you are using @FindBy annotation with filed (webelement), it initialize at the time when class initialize (or depending on your implementation where initElements() called). 当您将@FindBy批注与@FindBy (webelement)一起使用时,它将在类初始化时进行初始化(或取决于您调用initElements()实现)。 So it is fine to have webelement public but not static . 因此,将webelement public是很好的,但不是static For instance: 例如:

In your page class: 在您的网页课程中:

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

In test: 在测试中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM