我想在同一个类中使用Selenium webdriver编写两个testng测试 - 一个登录到应用程序和其他人创建一个新帐户。driver.findElement(By.linkText(“”))在一个测试中工作,但在另一个抛出异常
这是我下面 步骤 - 使用@BeforeClass对火狐浏览器中打开应用程序
@BeforeClass
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "http://www.salesforce.com";
driver.get(baseUrl + "/");
}
首先测试器登录到网站
@Test public void testLogin() throws Exception { driver.findElement(By.id("username")).sendKeys(strUsername); driver.findElement(By.id("password")).sendKeys(strPassword); driver.findElement(By.id("Login")).click();
}
第二次测试创建一个新帐号
@Test public void createAccount() throws Exception { driver.findElement(By.linkText("Accounts")).click(); ************************ ************************ ************************ ************************ ************************
}
我的问题是,当我运行这个测试TestNG的,我得到异常的第二个测试: org.openqa.selenium.NoSuchElementException:找不到元素:{ “方法”:“链接文本”,“选择器”:“帐户”}
但是,如果我包含命令“driver.findElement(By.linkText(”Accounts“))。click();”在testLogin()测试中,它起作用。我想在同一浏览器会话中运行所有测试。
任何输入将不胜感激。 谢谢。
TestNG中,在测试中运行相同的浏览器窗口,但createAccount()方法在login()之前被调用,因此它抛出异常。使用dependsOnMethods解决了这个问题。 – user1387236