webdriver 自动化测试初试
2014-06-02 21:51
417 查看
之前已经搭建了测试需要的环境,也学习了locate elements的方法,下面我们就来创建第一个简单的自动化测试用例。
测试场景如下:
1.打开百度首页
2.在搜索框输入关键字搜索,比如:webdriver automation testing
3.点击百度一下button
4.验证搜索结果是否包含输入的关键字
用例自动化测试代码实例如下:
package com.example.tests;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class BaiDuSearchTest {
private WebDriver driver;
private String baseUrl;
@BeforeMethod
public void setUp() throws Exception {
//Launch Firefox browser
driver = new FirefoxDriver();
baseUrl = "http://www.baidu.com";
}
@Test
public void baiDuSearchTest() throws Exception {
String exResult="WebDriver automation testing";
//Open 百度 home page
driver.get(baseUrl);
//Locate search box and input search keyword
driver.findElement(By.id("kw1")).sendKeys("WebDriver automation testing");
//Click 百度一下 button
driver.findElement(By.id("su1")).click();
//在结果页面找到第一个link并验证搜索关键字显示在链接中
String actResult=driver.findElement(By.id("1")).getText();
Assert.assertTrue(actResult.contains(exResult));
}
@AfterMethod
public void tearDown() throws Exception {
driver.quit();
}
}
然后直接右击该java文件选择run as TestNG test,然后可以查看自动化测试用例的执行了。
最简单的一个测试用例就到这里了。是不是很easy?
本文出自 “WebDriver自动化测试” 博客,请务必保留此出处http://anthonygao.blog.51cto.com/8653110/1421257
测试场景如下:
1.打开百度首页
2.在搜索框输入关键字搜索,比如:webdriver automation testing
3.点击百度一下button
4.验证搜索结果是否包含输入的关键字
用例自动化测试代码实例如下:
package com.example.tests;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class BaiDuSearchTest {
private WebDriver driver;
private String baseUrl;
@BeforeMethod
public void setUp() throws Exception {
//Launch Firefox browser
driver = new FirefoxDriver();
baseUrl = "http://www.baidu.com";
}
@Test
public void baiDuSearchTest() throws Exception {
String exResult="WebDriver automation testing";
//Open 百度 home page
driver.get(baseUrl);
//Locate search box and input search keyword
driver.findElement(By.id("kw1")).sendKeys("WebDriver automation testing");
//Click 百度一下 button
driver.findElement(By.id("su1")).click();
//在结果页面找到第一个link并验证搜索关键字显示在链接中
String actResult=driver.findElement(By.id("1")).getText();
Assert.assertTrue(actResult.contains(exResult));
}
@AfterMethod
public void tearDown() throws Exception {
driver.quit();
}
}
然后直接右击该java文件选择run as TestNG test,然后可以查看自动化测试用例的执行了。
最简单的一个测试用例就到这里了。是不是很easy?
本文出自 “WebDriver自动化测试” 博客,请务必保留此出处http://anthonygao.blog.51cto.com/8653110/1421257
相关文章推荐
- 使用WebDriver + Java + Junit做自动化测试教程
- 编写unit test以及自动化测试WebDriver
- 自动化测试工具Webdriver(2)API
- [原创]Juint4 + WebDriver 搭建自动化测试框架
- 使用selenium和webdriver进行自动化测试
- selenium.WebDriver 自动化测试脚本
- selenium webdriver + Junit 自动化测试:打开不同浏览器网页
- Selenium Webdriver 自动化测试环境搭建、入门
- Juint4 + WebDriver 搭建自动化测试框架
- ruby+selenium-webdriver一步一步完成自动化测试(4)—–逻辑与数据分离
- Cucumber + Watir webdriver + Ruby 功能自动化测试
- [资料]自动化e2e测试 -- WebDriverJS,Jasmine和Protractor
- Python爬虫工具 Selenium webdriver -自动化测试工具
- 自动化测试工具 Selenium WebDriver 入门教程
- Python selenium+webdriver 自动化测试例子
- 自动化测试工具 Selenium WebDriver 入门教程
- 自动化测试:Selenium webdriver 学习笔记-C#版(四)
- 自动化测试:Selenium webdriver 学习笔记-C#版(一)
- 自动化测试工具 Selenium WebDriver 入门教程