3_Selenium+TestNG
上一篇 / 下一篇 2016-02-16 10:22:32 / 个人分类:Selenium Java
1 Eclipse中TestNG插件安装
路径:Help->Install New Software,插件地址:http://beust.com/eclipse
2 新建TestNG Class
3 TestNG代码重构
代码编写
package com.selenium.test;
import java.sql.Driver;
import java.util.List;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.Test;
public class Test3 {
WebDriver driver = null;
@Test
public void login1() throws InterruptedException {
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.navigate().to("http://www.baidu.com/");
WebElement linkLogin = driver.findElement(By.xpath("//div[@id='u1']/a[text()='登录']"));
linkLogin.click();
Thread.sleep(3000);
WebElement textUsername = driver.findElement(By.xpath("//input[@id='TANGRAM__PSP_8__userName']"));
textUsername.clear();
textUsername.sendKeys("栗子测试");
WebElement textPassword = driver.findElement(By.xpath("//input[@id='TANGRAM__PSP_8__password']"));
textPassword.clear();
textPassword.sendKeys("2472471982");
WebElement buttonLogin = driver.findElement(By.xpath("//input[@id='TANGRAM__PSP_8__submit']"));
buttonLogin.click();
Thread.sleep(3000);
}
@Test
public void basicInfo1() throws InterruptedException {
Actions action = new Actions(driver);
WebElement linkMe = driver.findElement(By.xpath("//a[@id='s_username_top']/span"));
action.moveToElement(linkMe).perform();
WebElement linkSeniorSearch = driver.findElement(By.xpath("//div[@id='s_user_name_menu']/div/a[3]"));
linkSeniorSearch.click();
String firstWindowHandle = driver.getWindowHandle();
Set<String> towHandles = driver.getWindowHandles();
for (String handle : towHandles) {
System.out.println("+++" + handle);
driver.switchTo().window(handle);
}
WebElement linkModifyData = driver.findElement(By.xpath("//div[@id='content']//a[text()='修改资料']"));
linkModifyData.click();
Set<String> threeHandles = driver.getWindowHandles();
threeHandles.removeAll(towHandles);
String thirdWindowHandle = threeHandles.iterator().next();
driver.switchTo().window(thirdWindowHandle);
List<WebElement> radiosGender = driver.findElements(By.xpath("//input[@name='passport_sex']"));
radiosGender.get(1).click();
WebElement divBlood= driver.findElement(By.xpath("//div[@id='cussel1000002']/div"));
divBlood.click();
WebElement linkBlood= driver.findElement(By.xpath("//div[@id='cussel1000002']//a[text()='AB']"));
linkBlood.click();
WebElement buttonSaveBasic = driver.findElement(By.xpath("//form[@id='profile']/child::input2"));
buttonSaveBasic.click();
driver.quit();
}
}
XML文件 - 1
<?xml version="1.0" encoding="UTF-8"?>
<suite name="BaiduSuite" thread-count="1">
<test name="bd_updateInfo">
<classes>
<class name="com.selenium.test.Test3" >
<methods>
<include name="login1" />
<include name="basicInfo1" />
</methods>
</class>
<class name="com.selenium.test.Test4" >
<methods>
<include name="login2" />
<include name="basicInfo2" />
</methods>
</class>
</classes>
</test>
</suite>
4 TestNG方法依赖
代码编写
public class Test5 {
@BeforeClass
public void beforeClass() {
System.out.println("beforeClass");
}
@Test
public void login3() throws InterruptedException {
System.out.println("login3");
}
@Test(dependsOnMethods={ "login3" })
public void basicInfo3() throws InterruptedException {
System.out.println("basicInfo3");
}
@AfterClass
public void afterClass() {
System.out.println("afterClass");
}
}
XML文件 - 2
<?xml version="1.0" encoding="UTF-8"?>
<suite name="BaiduSuite" thread-count="1">
<test name="bd_updateInfo">
<classes>
<class name="com.selenium.test.Test5" />
</classes>
</test>
</suite>
5 TestNG参数化
代码编写
package com.selenium.test;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class Test6 {
@DataProvider(name="loginData")
public Object[][] loginData(){
return new Object[][]{
{"栗子测试","http://www.cnblogs.com/lizitest/"},
{"Q-Q", "2472471982"}
};
}
@Test(dataProvider="loginData")
public void login(String username,String password) {
System.out.println(username + " " + password);
}
}
XML文件 - 3
复制代码
<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite" verbose="1" parallel="false" thread-count="1">
<test name="TestDataProvider">
<packages>
<package name="com.selenium.test2" />
</packages>
</test>
</suite>
6 TestNG监听
代码编写
package com.selenium.test2;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
import com.selenium.util.Listener;
@Listeners({Listener.class})
public class Test8{
@DataProvider
public Object[][] dataInfo(){
return new Object[][]{{10}};
}
@Test
public void test1() {
System.out.println("1===========");
Assert.assertEquals(5, 1);
}
@Test(dependsOnMethods = "test1",dataProvider = "dataInfo")
public void test2(int info){
System.out.println("2==============");
Assert.assertEquals(info, 10);
}
@Test
public void test3(){
System.out.println("3");
Assert.assertEquals(100, 100);
}
}
监听类
package com.selenium.util;
import org.testng.ITestResult;
import org.testng.TestListenerAdapter;
public class Listener extends TestListenerAdapter{
@Override
public void onTestFailure(ITestResult result){
super.onTestFailure(result);
System.out.println(result.getName() + "===============失败!");
}
@Override
public void onTestSkipped(ITestResult result) {
super.onTestSkipped(result);
System.out.println(result.getName() + "===============跳过!");
}
@Override
public void onTestSuccess(ITestResult result) {
super.onTestSuccess(result);
System.out.println(result.getName() + "===============成功!");
}
}
XML文件 - 4
<?xml version="1.0" encoding="UTF-8"?>
<suite name="BaiduSuite" >
<listeners>
<listener class-name="com.selenium.util.Listener" />
</listeners>
<test name="bd_updateInfo" >
<classes>
<class name="com.selenium.test2.Test8" />
</classes>
</test>
</suite>
相关阅读:
- 2_Selenium对象识别 (lirenlong_9990, 2016-2-04)
- Java中的匿名内部类总结 (zaza9084, 2016-2-05)
- Selenium+Python浏览器调用:伪浏览器 (zaza9084, 2016-2-05)
- Java的static块执行时机 (zaza9084, 2016-2-06)
- selenium实现firefox浏览器的自动下载保存文件 (ewwerpm, 2016-2-13)
- Java使用JDBC方式连接数据库 (zaza9084, 2016-2-14)
- Java注解全面解析 (zaza9084, 2016-2-15)
- Java开源报表工具选型 (zaza9084, 2016-2-15)
- Selenium之偷懒教程 (zaza9084, 2016-2-15)
- 白盒测试学习之TestNG的参数分离 (zaza9084, 2016-2-15)
TAG: java Selenium selenium TestNG testng webdriver