我正在研究John Sonmez的Selenium测试框架教程,尝试执行第一个测试时遇到问题。测试部分包括两种方法:1. LoginPage.GoTo();
打开wordpress登录页面,而2. LoginPage.LoginAs("admin").Login();
插入用户名并单击继续。
运行测试时,将打开wordpress登录页面,并在2秒钟后打开空白的Chrome浏览器,并且JUnit显示NoSuchElementException。本教程的作者解决了此问题,在WebDriverWait
方法中添加了switchTo.ActiveElement.getAttribute()
和LoginPage.GoTo();
。但是,他使用C#进行编码,并且不提供我正在使用Java进行编码的解决方案。我也尝试应用WebDriverWait
,但问题仍然存在。谁能建议我如何使用Java中的WebDriverWait
解决这个问题。
LoginTest
import static org.junit.Assert.*;
import org.junit.Assert;
import org.junit.Test;
import wordpressAutomation.Driver;
import wordpressAutomation.LoginPage;
public class LoginTest extends Driver {
@Test
public void admin_user_can_login() {
LoginPage l = new LoginPage();
l.GoTo();
LoginPage.LoginAs("scenicrail").Login();
//Assert.assertTrue(DashboardPage.IsAt, "Failed to login");
}
}
LoginPage
import org.junit.Test;
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;
public class LoginPage extends Driver {
@Test
public void GoTo() {
openBrowser().get("https://wordpress.com/log-in");
}
public static LoginCommand LoginAs(String username) {
return new LoginCommand(username);
}
}
public class LoginCommand extends Driver {
private String username;
private String password;
public LoginCommand(String username) {
this.username = username;
}
public LoginCommand WithPassword(String password) {
this.password = password;
return this;
}
public void Login() {
WebElement login = openBrowser().findElement(By.xpath("//*[@id='usernameOrEmail']"));
login.sendKeys(username);
openBrowser().findElement(By.xpath("//button[@type = 'submit']")).click();
}
}
public class Driver {
public WebDriver openBrowser() {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\KatarinaOleg\\Desktop\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
//driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
return driver;
}
}
答案 0 :(得分:2)
在打开页面后,您将获得NoSuchElementException,您可以添加如下内容。
FluentWait<WebDriver> webDriverWait = new WebDriverWait(driver, 25).pollingEvery(5, TimeUnit.SECONDS);
webDriverWait.until(ExpectedConditions.visibilityOf(webElement));
只需添加要用作检查的任何元素,以确保页面已加载
答案 1 :(得分:1)
您已从LoginCommand类的Login方法中多次调用Openbrowser。因此,将不必要地创建其他驱动程序实例。因此,您需要对代码进行如下修改以及显式等待(驱动程序类也需要进行如下修改)
LoginCommand类的登录方法:
public void Login() {
WebDriverWait wait=new WebDriverWait(driver,20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='usernameOrEmail']")));
WebElement login = driver.findElement(By.xpath("//*[@id='usernameOrEmail']"));
login.sendKeys(username);
driver.findElement(By.xpath("//button[@type = 'submit']")).click();
}
驱动程序类:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class Driver {
WebDriver driver;
public WebDriver openBrowser() {
System.setProperty("webdriver.chrome.driver", "drivers/chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
//driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
return driver;
}
}
编辑:(LoginTest类中的dmin_user_can_login()方法)
@Test
public void admin_user_can_login() {
LoginPage l = new LoginPage();
l.GoTo();
l.LoginAs("scenicrail").Login();
//Assert.assertTrue(DashboardPage.IsAt, "Failed to login");
}
LoginPage类:
import org.junit.Test;
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;
public class LoginPage extends Driver {
@Test
public void GoTo() {
openBrowser().get("https://wordpress.com/log-in");
}
public LoginCommand LoginAs(String username) {
return new LoginCommand(username);
}
}