使用Java在Selenium WebDriver中使用PageObjects,Page Factory和WebDriverWait [英] Using PageObjects, Page Factory and WebDriverWait in Selenium WebDriver using Java

查看:106
本文介绍了使用Java在Selenium WebDriver中使用PageObjects,Page Factory和WebDriverWait的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用Selenium WebDriver为我合作过的一些项目实现功能测试。我正在尝试使用Page Factory设计模式与Page Factory来分解我的定位器。我还创建了一个静态WaitTool对象(singleton),它使用可选的超时参数实现了几种等待技术。

I've been using Selenium WebDriver to implement functional tests for some projects that I've worked with. I'm trying to use the Page Object design pattern with Page Factory to factor out my locators. I've also created a static WaitTool object (singleton) that implements several waiting techniques with optional timeout parameters.

我当前的问题是我想使用我的wait方法在PageFactory尝试初始化WebElements之前。我想等待的原因是因为PageFactory可能会尝试在页面元素可用之前初始化页面元素。

My current problem is that I would like to use my wait methods before the PageFactory attempts to initialise the WebElements. The reason I would like to wait is because the PageFactory may try to initialise the page elements before they are available on the page.

这是一个示例PageObject:

Here is a sample PageObject:

public class SignInPage extends PageBase {
    @FindBy(id = "username")
    @CacheLookup
    private WebElement usernameField;

    @FindBy(id = "password")
    @CacheLookup
    private WebElement passwordField;

    @FindBy(name = "submit")
    @CacheLookup
    private WebElement signInButton;

    public SignInPage(WebDriver driver) {
        super(driver);

        WaitTool.waitForPageToLoad(driver, this);

        // I'd like initialisation to occur here
    }

    public MainPage signInWithValidCredentials(String username, String password){
        return signIn(username, password, MainPage.class);
    }

    private <T>T signIn(String username, String password, Class<T> expectedPage) {
        usernameField.type(username);
        passwordField.type(password);
        signInButton.click();

        return PageFactory.initElements(driver, expectedPage);
    }
}

以下是TestObject示例:

Here is a sample TestObject:

public class SignInTest extends TestBase {
    @Test
    public void SignInWithValidCredentialsTest() {
        SignInPage signInPage = PageFactory.initElements(driver, SignInPage.class);

        MainPage mainPage = signInPage.signInWithValidCredentials("sbrown", "sbrown");

        assertThat(mainPage.getTitle(), is(equalTo(driver.getTitle())));
    }
}

我倾向于把我的逻辑放在Page Object中尽可能多(包括等待),因为它使测试用例更具可读性。

I tend to put my logic in the Page Object as much as possible (including waits), as it makes the test cases much more readable.

推荐答案

PageFactroy中的WebElements确实是WebElements的代理。这意味着每次访问WebElement时,它都会执行搜索以查找页面上的元素。

The WebElements in a PageFactroy are really proxies to WebElements. This means that every time you access a WebElement it will perform a search to find the element on the page.

这有一些优点:


  • 初始化PageFactory时,代理已配置,但此时未找到WebElements(因此您不会获得NoSuchElementException)

  • 每次使用WebElement时它都会再次找到它,所以你不应该发现StaleElementException的

你正在使用@CacheLookup注释失去了你的第二个好处,因为它会找到一次元素,然后保持对它的引用,你现在更有可能看到StaleElementExceptions。

You are using the @CacheLookup annotation which is losing you the second benefit as it will find the element once and then keep a reference to it, you are now far more likely to see StaleElementExceptions.

据说你仍然保留了主要的好处,即Selenium在第一次使用之前不会进入页面并实际找到该元素。

That being said you still keep the main benefit which is that Selenium will not go to the page and actually find the element until you first use it.

总而言之,您需要做的就是移动

So in summary all you need to do is move

PageFactory.initElements(driver, this);

进入你的构造函数,一切正常。

Into your constructor and it will all work fine.

这篇关于使用Java在Selenium WebDriver中使用PageObjects,Page Factory和WebDriverWait的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆