3_Selenium+TestNG
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(); //等待2秒 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(); //等待3秒 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(); //index从0开始 //血型 - 此下拉框非Select,只是样式像 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:定义一个测试套件,可包含多个测试用例或测试group--> <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 //将执行Test之前需要准备的内容卸载@BeforeClass中,如:初始化WebDriver,链接数据库等等
public void beforeClass() {
System.out.println("beforeClass"); } @Test public void login3() throws InterruptedException { System.out.println("login3"); } @Test(dependsOnMethods={ "login3" }) //依赖login3方法,如果login3失败,则basicInfo3方法跳过 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/"}, {"QQ", "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>
- 栗子测试