摘要: 之前已经搭建了测试需要的环境,也学习了locateelements的方法,下面我们就来创建第一个简单的自动化测试用例。 测试场景如下: 1.打开百度首页 2.在搜索框输入关键字搜索,比如:webdriverautomationt ...
之前已经搭建了测试需要的环境,也学习了locateelements的方法,下面我们就来创建第一个简单的自动化测试用例。 测试场景如下: 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;@BeforeMethodpublicvoidsetUp()throwsException{//LaunchFirefoxbrowserdriver=newFirefoxDriver();baseUrl="http://www.baidu.com";}@TestpublicvoidbaiDuSearchTest()throwsException{StringexResult="WebDriverautomationtesting";//Open百度homepagedriver.get(baseUrl);//Locatesearchboxandinputsearchkeyworddriver.findElement(By.id("kw1")).sendKeys("WebDriverautomationtesting");//Click百度一下buttondriver.findElement(By.id("su1")).click();//在结果页面找到第一个link并验证搜索关键字显示在链接中StringactResult=driver.findElement(By.id("1")).getText();Assert.assertTrue(actResult.contains(exResult));}@AfterMethodpublicvoidtearDown()throwsException{driver.quit();}} 然后直接右击该java文件选择runasTestNGtest,然后可以查看自动化测试用例的执行了。 最简单的一个测试用例就到这里了。是不是很easy?