Webdriver 自動化測試初試
測試場景如下:
1.打開百度首頁
2.在搜索框輸入關鍵字搜索,比如:webdriverautomationtesting
3.點擊百度一下button
4.驗證搜索結果是否包含輸入的關鍵字
用例自動化測試代碼實例如下:
packagecom.example.tests; importorg.openqa.selenium.By; importorg.openqa.selenium.WebDriver; importorg.openqa.selenium.firefox.FirefoxDriver; importorg.testng.Assert; importorg.testng.annotations.AfterMethod; importorg.testng.annotations.BeforeMethod; importorg.testng.annotations.Test; publicclassBaiDuSearchTest{ privateWebDriverdriver; privateStringbaseUrl; @BeforeMethod publicvoidsetUp()throwsException{ //LaunchFirefoxbrowser driver=newFirefoxDriver(); baseUrl="http://www.baidu.com"; } @Test publicvoidbaiDuSearchTest()throwsException{ StringexResult="WebDriverautomationtesting"; //Open百度homepage driver.get(baseUrl); //Locatesearchboxandinputsearchkeyword driver.findElement(By.id("kw1")).sendKeys("WebDriverautomationtesting"); //Click百度一下button driver.findElement(By.id("su1")).click(); //在結果頁面找到第一個link并驗證搜索關鍵字顯示在鏈接中 StringactResult=driver.findElement(By.id("1")).getText(); Assert.assertTrue(actResult.contains(exResult)); } @AfterMethod publicvoidtearDown()throwsException{ driver.quit(); } } |
最簡單的一個測試用例就到這里了。是不是很easy?
posted on 2014-06-18 10:46 順其自然EVO 閱讀(270) 評論(0) 編輯 收藏 所屬分類: selenium and watir webdrivers 自動化測試學習