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(); } } |