How to explicitly wait while using page factory in Selenium?

橙三吉。 提交于 2020-07-20 17:06:58

问题


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

WebDriverWait = new WebDriverWait(driver,30);

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

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?


回答1:


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

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




回答2:


I think a better solution will be if you pass a driver in the page class from its caller Test class. Please refer below implementation for more clarity.

Page Class:

public class YourTestPage {
    private WebDriver driver;
    private WebDriverWait wait;

    @FindBy(xpath = "//textarea")
    private WebElement authorField;

    public YourTestPage(WebDriver driver) {
        this.driver = driver;
        wait = new WebDriverWait(driver, 15, 50);
        PageFactory.initElements(driver,this);
    }

    public String getAuthorName() {
        wait.until(ExpectedConditions.visibilityOf(authorField)).getText();
    }
}

Test Class:

public class YourTest{

    private YourTestPage yourTestPage;
    private WebDriver driver;

    @BeforeTest
    public void setup() throws IOException {
      driver = WebDriverFactory.getDriver("chrome");
      yourTestPage = new YourTestPage(driver);
    }

    @Test
    private void validateAuthorName() {     
      Assert.assertEquals(yourTestPage.getAuthorName(),"Author Name");
    }
}


来源:https://stackoverflow.com/questions/47191185/how-to-explicitly-wait-while-using-page-factory-in-selenium

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!