我正在使用 Selenium2 在 Java 中设计一个网站登录测试程序。
当我在 NetBeans 中的 JUnit 中运行它时,出现错误:“testLogin(JUnitTest.LoginTest): 无法启动新会话。可能的原因是远程服务器地址无效或浏览器启动失败。” 对此有任何想法吗?谢谢!注意:我还没有想到测试登录结果的方法,所以断言暂时不起作用。
我的代码:
public class LoginTest {
private static WebDriver driver;
private static String baseUrl;
private static String loginUrl;
@Before
public void setUp() {
baseUrl = "https://web.kitchology.com/kitchology/";
loginUrl = "https://web.kitchology.com/kitchology/faces/Home.xhtml";
System.setProperty("webdriver.chrome.driver", "E:\\Google\\Chrome\\Application\\Chrome.exe");
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get(baseUrl);
driver.findElement(By.id("j_idt16:sitepassword")).sendKeys("sitepass");
driver.findElement(By.id("j_idt16:j_idt19")).click();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void testLogin() throws Exception {
driver.get(loginUrl);
driver.findElement(By.id("j_idt16:loginLink")).click();
driver.findElement(By.id("login")).clear();//clear any previous text
driver.findElement(By.id("j_idt16:loginForm:username")).sendKeys("myuser");
driver.findElement(By.id("j_idt16:loginForm:password")).sendKeys("mypass");
driver.findElement(By.id("j_idt16:loginForm:j_idt28")).submit();
assert("Welcome to Kitchology!" == driver.getTitle());
assert("" == WelcomePage.getWelcomeMessage());
}
@Test
public void testFailedLogin() throws Exception {
driver.get(loginUrl);
driver.findElement(By.id("j_idt16:loginLink")).click();
driver.findElement(By.id("login")).clear();//clear any previous text
driver.findElement(By.id("j_idt16:loginForm:username")).sendKeys("myuser");
driver.findElement(By.id("j_idt16:loginForm:password")).sendKeys("mypass");
driver.findElement(By.id("j_idt16:loginForm:j_idt28")).submit();
assert("" == FailedPage.getErrorMessage());
}
@After
public void after() {
driver.quit();
}
}