页面对象模型Selenium,在测试类中添加了一个等待时间

时间:2019-06-12 21:03:26

标签: java selenium testing selenium-webdriver pom.xml

我在底部尝试过为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元素以等待,但是我随后在测试类中对其进行了声明,但它不起作用。添加等待的最佳方法是什么?我希望在填写邮政编码框

之后声明等待

1 个答案:

答案 0 :(得分:0)

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

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

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

在那之后,来自测试类的调用应该是'Explicitwait.methodName(驱动程序,以毫秒为单位的时间,元素)。 这将适用于您框架中的类。