1. 程式人生 > >Web自動化框架LazyUI使用手冊(8)--excel資料驅動詳解(ExcelDataProvider)

Web自動化框架LazyUI使用手冊(8)--excel資料驅動詳解(ExcelDataProvider)

概述

框架提供了excel資料驅動方式執行測試用例的工具,本文將針對資料驅動,進行詳細演示。

詳見類:lazy.test.ui.browser.ExcelDataProvider

被測物件:

測試場景:

輸入使用者名稱,點選登入,校驗各種異常輸入

輸入後,紅框裡會出現一些異常提示,如圖:

image

bean層程式碼:

使用外掛生成
package test;
import lazy.test.ui.annotations.*;
import lazy.test.ui.beans.PageBean;
import lazy.test.ui.controls.*;
import lazy.test.ui.browser.BrowserEmulator;
public class login extends PageBean {
    @Xpath(xpath={"//input[@id='username']", "//input[@name='username']", "//input[contains(@class,'text highlight1')]"})
    @Frame(frame="")
    @Description(description="username")
    public Text username;
    @Xpath(xpath={"//button[@id='pwdLoginSubmit']", "/html/body/form/div/div[3]/div/div[8]/button"})
    @Frame(frame="")
    @Description(description="pwdLoginSubmit")
    public Click pwdLoginSubmit;
    public login(BrowserEmulator be) { super(be); }
}


page層程式碼

import lazy.test.ui.browser.BrowserEmulator;
public class LoginRegisterBean {
private BrowserEmulator be;
Login loginBean = new Login(be);
//開啟登陸頁
public void openLoginURL(){
   be.open("http://bj.sqyishi.com/user/login.htm");
}
//校驗是否存在文字
public void expectTextCheck(String expectText){
   be.expectTextExistOrNot(true, expectText, 3500);
}
//手機號輸入校驗
public void userNameCheck(String telephone, String expectText){
   openLoginURL();
   loginBean.username.input(userName);
   loginBean.pwdLoginSubmit.click();
   expectTextCheck(expectText);
}
}


資料驅動檔案

image

1. Excel放在Data資料夾下,即根目錄的/data/下面

2. Excel命名方式:測試類名.xls,如圖中①

3. Excel的sheet命名方式:測試方法名,如圖中②

4. Excel第一行為Map鍵值,如圖中第一行

5. 最後一樣必須以“#”號結尾,表示終止,如圖中③

6. 可以使用第一列控制其是否執行,如圖中④,第六行,不執行

Test層程式碼

package com.ebl.UIAutomation.test.loginRegister;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.Map;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import lazy.test.ui.browser.ExcelDataProvider;

public class LoginRegisterParamTest extends loginBaseTest{

   //使用驅動資料執行測試用例
   @Test(dataProvider = "dp" )
   public void UsernameCheck(Map<String,String> data) {
      if(data.get("isRun").equals("1")){//使用第一列控制其是否執行
         loginPage.userNameCheck(data.get("telephone"), data.get("expectText"));
      }
   }
   //根據類名、方法名,載入驅動資料
    @DataProvider(name = "dp")
    public Iterator<Object[]> dataFortestMethod(Method method) throws IOException {
       return new ExcelDataProvider(this.getClass().getName(),method.getName());
    }
}


執行

使用testng執行test:UsernameCheck

便會啟動瀏覽器

開啟登入頁面

一行為一個case,按excel中順序,向用戶名框中填入telephone列的值,

點選登入,

校驗頁面上是否出現了expectText列的文字。

wx1