您的位置:首页 > 其它

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: