如何在 PageObjectModel 的 PageFactory 中添加显式等待? [英] How to add explicit wait in PageFactory in PageObjectModel?

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

问题描述

我在下面的代码中添加了硬编码等待 thread.sleep().如何使用显式等待.我想等到用户名"WebElement 出现.我的程序运行良好.我已经写了测试用例.

I have added hardcode wait thread.sleep() in my below code. How to use explicit wait. I want to wait till "username" WebElement appear. My program is working perfectly. I have already written testcases.

package com.pol.zoho.PageObjects;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.WebDriverWait;

public class ZohoLoginPage {

WebDriver driver;
public ZohoLoginPage(WebDriver driver)
{
    PageFactory.initElements(driver, this);
}

@FindBy(xpath=".//*[@id='lid']")
public WebElement email;

@FindBy(xpath=".//*[@id='pwd']")
public WebElement password;

@FindBy(xpath="//*[@id='signin_submit']")
public WebElement signin;

public void doLogin(String username,String userpassword)
{
    try {
        Thread.sleep(3000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    email.sendKeys(username);
    password.sendKeys(userpassword);
    signin.click();
}

}

推荐答案

PageObjectModel 中使用 PageFactory 时,如果您希望通过某些 JavaScript 并且它可能不存在于页面上,您可以使用显式等待支持正常定位器工厂如下:

When using PageFactory in PageObjectModel if you expect the element to be loaded through some JavaScript and it might not be present on the page already you can use the Explicit Wait support with a normal locator factory as follows:

  • 代码块:

  • Code Block:

package com.pol.zoho.PageObjects;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class ZohoLoginPage {

    WebDriver driver;
    public ZohoLoginPage(WebDriver driver)
    {
        PageFactory.initElements(driver, this);
    }

    @FindBy(xpath=".//*[@id='lid']")
    public WebElement email;

    @FindBy(xpath=".//*[@id='pwd']")
    public WebElement password;

    @FindBy(xpath="//*[@id='signin_submit']")
    public WebElement signin;

    public void doLogin(String username,String userpassword)
    {
        WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(ZohoLoginPage.getWebElement()));
        email.sendKeys(username);
        password.sendKeys(userpassword);
        signin.click();
    }

    public WebElement getWebElement()
    {
        return email;
    }

}

您可以在 如何对 PageFactory 字段使用显式等待中找到详细讨论和 PageObject 模式

这篇关于如何在 PageObjectModel 的 PageFactory 中添加显式等待?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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