概述
框架提供了excel数据驱动方式运行测试用例的工具,本文将针对数据驱动,进行详细演示。
详见类:lazy.test.ui.browser.ExcelDataProvider
被测对象:
http://bj.sqyishi.com/user/login.htm
测试场景:
输入用户名,点击登录,校验各种异常输入
输入后,红框里会出现一些异常提示,如图:
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); } }
数据驱动文件
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列的文字。