LoginPage.java
public LoginPage(WebDriver driver){
//initialize elements
PageFactory.initElements(driver, this);
}
public void set_email(String useremail){
email.clear();
email.sendKeys(useremail);
}
public void set_password(String userpassword){
password.clear();
password.sendKeys(userpassword);
}
public void click_button(){
button.submit();
}
public void equal_titles(String actualTitle,String expectedTitle){
Assert.assertEquals(actualTitle,expectedTitle);
}
public void set_reset_email(String resetemail){
reset_psw_email.clear();
reset_psw_email.sendKeys(resetemail);
}
public void click_reset_button(){
reset_button.submit();
}
TestingLogin.java
@BeforeTest
public void setup(){
System.setProperty("webdriver.gecko.driver", "\\GeckoDriver\\geckodriver.exe");
driver=new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://test.admin.placer.life/login");
}
@Test(priority=9)
public void reset_psw(){
LoginPage login=new LoginPage(driver);
driver.findElement(By.xpath("//*[@id='app']/div/div[2]/a")).click();
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='app']/div/div[2]/form/div[3]/div[2]/button[contains(text(),'Send Password Reset Link')]")));
String expectedTitle = "Placer Admin - Password recovery";
String actualTitle = driver.getTitle();
login.equal_titles(actualTitle, expectedTitle);
}
@Test(priority=8)
public void reset_email(){
LoginPage login=new LoginPage(driver);
driver.findElement(By.xpath("//*[@id='app']/div/div[2]/a")).click();
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='app']/div/div[2]/form/div[3]/div[2]/button[contains(text(),'Send Password Reset Link')]")));
login.set_reset_email("");
login.click_reset_button();
WebDriverWait wait_reset_psw = new WebDriverWait(driver, 10);
wait_reset_psw.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='app']/div/div[2]/form/div[2]/span[2]")));
Assert.assertEquals(driver.findElement(By.xpath("//*[@id='app']/div/div[2]/form/div[2]/span[2]")).getText(),"The email field is required.");
// driver.navigate().refresh();
}
@AfterTest
public void close(){
driver.close();
}
结果
PASSED:reset_email
失败:reset_psw
org.openqa.selenium.TimeoutException:预期的条件失败:等待By.xpath定位的元素的可见性:// * [@ id =' app'] / div / div [2] / form / div [3] / div [2] / button [包含(文本(),'发送密码重置链接')](尝试5秒钟,间隔500 MILLISECONDS)
运行代码时发生此错误。但是当我评论其中一个(reset_email或reset_psw)时,另一个将成功通过而没有任何错误。我可以知道解决方案吗?需要对此代码进行哪些修改?
请帮忙, 非常感谢你
答案 0 :(得分:0)
它看起来像它所做的那样。 您是否尝试增加WebDriverWait应该等待的时间?
public void reset_psw()
{
LoginPage login=new LoginPage(driver);
driver.findElement(By.xpath("//*[@id='app']/div/div[2]/a")).click();
WebDriverWait wait = new WebDriverWait(driver, 60); // <-- Here
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='app']/div/div[2]/form/div[3]/div[2]/button[contains(text(),'Send Password Reset Link')]")));
String expectedTitle = "Placer Admin - Password recovery";
String actualTitle = driver.getTitle();
login.equal_titles(actualTitle, expectedTitle);
}
public void reset_email()
{
LoginPage login=new LoginPage(driver);
driver.findElement(By.xpath("//*[@id='app']/div/div[2]/a")).click();
WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='app']/div/div[2]/form/div[3]/div[2]/button[contains(text(),'Send Password Reset Link')]")));
login.set_reset_email("");
login.click_reset_button();
WebDriverWait wait_reset_psw = new WebDriverWait(driver, 60);
wait_reset_psw.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='app']/div/div[2]/form/div[2]/span[2]")));
Assert.assertEquals(driver.findElement(By.xpath("//*[@id='app']/div/div[2]/form/div[2]/span[2]")).getText(),"The email field is required.");
// driver.navigate().refresh();
}
这会增加超时。
答案 1 :(得分:0)
根据问题和代码试用中的结果部分, Testcase reset_email()
PASSED 和 Testcase reset_psw()
FAILED 为:
TimeoutException: Expected condition failed: waiting for visibility of element
似乎在你的@Test
步骤中,代码块几乎相似。带有(priority = 8)的 @Test 首先在其中调用函数login.click_reset_button();
,该函数调用reset_button.submit();
并在您重定向到新页面断言文本电子邮件字段是必需的。。
现在,值得一提的是,当您调用click()
或submit()
并且用户被重定向到新页面时,HTML DOM会发生变化。随着 DOM树的更改,部分/所有元素可能会添加 / 已删除 / 已修改。因此,当您使用@Test
方法调用 ExpectedConditions 时,您的下一个visibilityOfElementLocated()
因为之前的元素未附加到 DOM {{3} }或NoSuchElementException被引发,最终导致StaleElementReferenceException