首页 > 解决方案 > 页面对象模型 Selenium,在测试类中添加等待

问题描述

在底部,我尝试为 Web 元素创建等待方法,然后在测试类中声明它,但它不起作用。添加等待的最佳方法是什么?我希望在填写完邮政编码框后声明等待

页面对象

    package pageFactory;


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.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.util.concurrent.TimeUnit;

public class ECUTestform {
    WebDriverWait wait; 
    WebDriver driver;



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

    }


@FindBy(id="RegisterModel_Phone")
WebElement phone;

@FindBy(xpath="//*[@id=\"RegisterModel_Name\"]")
WebElement firstname;

@FindBy(xpath="//*[@id=\"RegisterModel_Address\"]")
WebElement addressone;

@FindBy(xpath="//*[@id=\"RegisterModel_Street\"]")
WebElement addresstwo;

@FindBy(xpath="//*[@id=\"RegisterModel_City\"]")
WebElement city;

@FindBy(id="RegisterModel_Email")
WebElement email;

@FindBy(xpath="//*[@id=\"RegisterModel_ConfirmEmail\"]")
WebElement emailconfirm;

@FindBy(id="RegisterModel_CountryID")
WebElement selectcountry;

public Select getCountrySelect() {
      return new Select(selectcountry);
    }

@FindBy(xpath="//*[@id=\"RegisterModel_Password\"]")
WebElement passwd;

@FindBy(xpath="//*[@id=\"RegisterModel_ConfirmPassword\"]")
WebElement passwdconfirm;

@FindBy(id="RegisterModel_Postcode")
WebElement postcode;

@FindBy(xpath="//*[@id=\"tabs-1\"]/div/div/div[16]/div/ins")
WebElement buttonno;





public WebElement button()
{
    return buttonno;
}

public WebElement Phone()
{
    return phone;
}


public WebElement PostCode()
{
    return postcode;
}


public WebElement City()
{
    return city; 
}


public WebElement FirstName()
{

return firstname;
}

public WebElement AddressOne()
{

return addressone;
}

public WebElement Addresstwo()
{

return addresstwo;
}


public WebElement SelectCount()
{

return selectcountry;
}

public WebElement Emailconfirm()
{

return emailconfirm;
}

public WebElement Email()
{

return email;
}

public WebElement Password()
{
    return passwd;
}


public WebElement Passwordconfirm()
{

return passwdconfirm;
}

public WebElement explicitWait() {
    WebDriverWait w=new WebDriverWait(driver, 5, 200);
    return w.until(ExpectedConditions.visibilityOf(selectcountry)); 


}

}

测试班

package testCases;

import pageFactory.ECUHomePage;
import pageFactory.ECUTestform;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;

public class TESTForm {
    WebDriver driver;
    WebDriverWait wait;


@Test
public void Test() throws InterruptedException
{
System.setProperty("webdriver.chrome.driver","C:\\Users\\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("https://www.website.com/");
ECUHomePage rd = new ECUHomePage(driver);
ECUTestform rt = new ECUTestform(driver);
rd.TestForm().click();

// Name and Address 
rt.FirstName().sendKeys("TEST");
rt.AddressOne().sendKeys("TEST123");
rt.Addresstwo().sendKeys("TESTING3434");
rt.City().sendKeys("TESTINGTON");
rt.PostCode().sendKeys("TEST");

rt.explicitWait();

rt.getCountrySelect().selectByVisibleText("United Kingdom");
rt.Emailconfirm().sendKeys("test@testing.com");
rt.Email().sendKeys("test@testing.com");
rt.Password().sendKeys("testingpassword101");
rt.Passwordconfirm().sendKeys("password");
rt.Phone().sendKeys("0115 841003320");
rt.button().click();

我已经尝试在底部创建一个等待的 web 元素,但是我在测试类中声明它并且它不起作用。添加等待的最佳方法是什么?我希望在填写完邮政编码框后声明等待

标签: javaseleniumtestingselenium-webdriverpom.xml

解决方案


创建名为“Explicitwait”的单独类,并将 WebDriverWait 中的所有方法添加到具有公共静态的此类中。确保以毫秒为单位获取驱动程序、元素和时间作为参数。

在此之前声明私有静态 WebDriverWait 等待;

在每个方法中为这个等待变量创建一个对象,例如“wait = new WebDriverWait(driver, milliseconds)”。第二行应该是 wait.until(ExpectedConditions.visibilityOf(element)'。

在您的测试类调用之后应该是'Explicitwait.methodName(driver, time in mills, element)。这将适用于您框架中的类。


推荐阅读