Selenium PageFactory-检查是否在正确的页面上 [英] Selenium PageFactory - Check if on correct page

查看:118
本文介绍了Selenium PageFactory-检查是否在正确的页面上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

摘要

使用PageFactory,如何确保将正确的页面传递给适当的页面对象?例如,确保将真实的登录页面传递给LoginPage对象的实例.

With the PageFactory, how do I ensure the correct page has been passed to the appropriate page object? For example making sure that a real login page is being passed to an instance of the LoginPage object.

详细信息

我在 PageObjects 文档中注意到它们在构造函数中解释了如何,您可以检查一下是否在正确的页面上.例如

I notice on the PageObjects documentation that they explain how, in the constructor, that you can check to see if you are on the right page. For example

    // Check that we're on the right page.
    if (!"Login".equals(driver.getTitle())) {
        // Alternatively, we could navigate to the login page, perhaps logging out first
        throw new IllegalStateException("This is not the login page");
    }

但是,当阅读 PageFactory 文档时,它们没有说明如何检查是否传递了正确的页面.他们只是继续尝试运行测试.使用PageFactory时如何最好地进行检查?

However, when reading the PageFactory docs, they don't explain how to check if the correct page has been passed in. They just go ahead and attempt to run the test. How can I best check this when using PageFactory?

推荐答案

您可以在构造函数中放置一个断言,以检查URL是否有效,例如

You could put an assertion in your constructor that checks the URL is valid e.g.

MyPage(WebDriver driver) {
    PageFactory.initElements(driver, this);

    assert wait.until(currentURIIs(new URI("http://www.mydomain.com/mypage.html")));
}

上面使用了以下预期条件:

The above is using the following expected condition:

public static ExpectedCondition<Boolean> currentURIIs(final URI pageURI) {
    new ExpectedCondition<Boolean>() {
        @Override
        Boolean apply(WebDriver driver) {
            new URI(driver.currentUrl) == pageURI;
        }
    }
}

您当然可以搜索页面上已知的元素或任何其他唯一标识功能.检查当前页面的URI是一种可能的选择,您还可以检查许多其他内容以确保您位于正确的页面上.

You could of course search for an element you know is on the page, or any other uniquely identifying feature. Checking the current page URI is one possible option, there are many other things you could check to ensure you are on the correct page.

这篇关于Selenium PageFactory-检查是否在正确的页面上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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