码迷,mamicode.com
首页 > 其他好文 > 详细

3_Selenium+TestNG

时间:2016-01-16 22:35:40      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:

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();
    }
  
}

 

4 使用XML文件控制TestNG代码执行顺序

<?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>

 

3_Selenium+TestNG

标签:

原文地址:http://www.cnblogs.com/lizitest/p/5136459.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!