gpt4 book ai didi

java - 页面对象模型 Selenium,将等待添加到测试类中

转载 作者:行者123 更新时间:2023-11-28 21:37:39 26 4
gpt4 key购买 nike

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

最佳答案

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

在此之前声明 private static WebDriverWait 等待;

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

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

关于java - 页面对象模型 Selenium,将等待添加到测试类中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56570327/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com