我想在同一個類中使用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