3_Selenium+TestNG

1 Eclipse中TestNG插件安裝java

  路徑:Help->Install New Software,插件地址:http://beust.com/eclipseios

2 新建TestNG Classsql

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參數化eclipse

  • 代碼編寫
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監聽ide

  • 代碼編寫
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>

 

栗子測試

  • 全部文章均爲原創,是栗子測試全部人員智慧的結晶,若有轉載請標明出處
  • 若是您在閱讀以後以爲有所收穫,請點擊右下角推薦
  • QQ:2472471982,歡迎你們前來諮詢和探討(暗號:栗子測試)
相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息