Webdriver 自动化测试初试

发表于:2014-6-17 11:58

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:AnthonyGao1105    来源:51Testing软件测试网采编

  之前已经搭建了测试需要的环境,也学习了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?
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号