在Selenium中使用页面工厂时如何显式等待? [英] How to explicitly wait while using page factory in Selenium?

查看:157
本文介绍了在Selenium中使用页面工厂时如何显式等待?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将像这样在Selenium中组织一次显式等待:

I'm going to organize an explicit wait in Selenium like this:

WebDriverWait = new WebDriverWait(driver,30);

WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(locator));

问题是我的类中没有驱动程序,因为我使用的是PageFactory,而不是测试类中的构造函数:

The problem is that I don't have the driver in my class, because I used the PageFactory, not a constructor in a test class:

MyClass myform = PageFactory.InitElements(driver, MyClass.class)

在这种情况下,组织显式等待的一个好决定是什么?

What is a good decision to organize explicit wait in this case?

推荐答案

我建议您按预期使用PageFactory,并为您的类提供一个构造函数,以在其中使用显式等待.脚本和页面对象之间的分隔使得将来使用它变得更加容易.

I would suggest that you use the PageFactory as intended and have a constructor for your class where you would like to use the explicit wait. Having a separation between the script and the page objects makes it much easier to work with in the future.

public class MyClass {

    WebDriverWait wait; 
    WebDriver driver; 
    @FindBy(how=How.ID, id="locatorId")
    WebElement locator; 

    // Construct your class here 
    public MyClass(WebDriver driver){
        this.driver = driver; 
        wait = new WebDriverWait(driver,30);
    }

    // Call whatever function you want to create 
    public void MyFunction(){
        wait.until(ExpectedConditions.presenceOfElementLocated(locator));
        // Perform desired actions that you wanted to do in myClass
    } 

然后在您的测试用例中使用代码来执行您的测试.在您的示例中,等待包含在页面内.

Then in your test case use code to perform your test. In your example, the wait is contained inside the page.

public class MyTestClass {
    public static void main (string ... args){
        WebDriver driver = new FireFoxDriver(); 
        MyClass myForm = PageFactory.initElements(driver,Myclass.class); 
        myForm.MyFunction(); 
    }
}

此示例以Selenium WebDriver实用指南一书中的示例为模型,可在此处找到这里

This example was modeled after the example in the book Selenium WebDriver Practical Guide that can be found here here

这篇关于在Selenium中使用页面工厂时如何显式等待?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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