qileilove

blog已经转移至github,大家请访问 http://qaseven.github.io/

Webdriver 自动化测试初试

 之前已经搭建了测试需要的环境,也学习了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;
@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();
}
}
  然后直接右击该java文件选择runasTestNGtest,然后可以查看自动化测试用例的执行了。
  最简单的一个测试用例就到这里了。是不是很easy?

posted on 2014-06-18 10:46 顺其自然EVO 阅读(264) 评论(0)  编辑  收藏 所属分类: selenium and watir webdrivers 自动化测试学习


只有注册用户登录后才能发表评论。


网站导航:
 
<2014年6月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

导航

统计

常用链接

留言簿(55)

随笔分类

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜