测试框架总体来说与应用开发的框架并无太大的差异,在设计的起始阶段初衷也都一样,满足测试绝大多数活动并提高测试代码的编写效率,然后兼顾易用、兼容、通用以及简单维护等几个维度是其存在的唯一意义,它是测试开发过程中提取特定领域测试方法共性部分而形成的体系结构,然而不同的测试领域有不同的测试框架,不同的软件公司也应有属于自己的测试框架
框架设计前提
- 已经熟练掌握了Appium环境的安装与配置,请参考JavaApp自动化测试系列[v1.0.0][Appium开发环境搭建]和JavaApp自动化测试系列[v1.0.1][Appium开发环境搭建],它不单单是代码环境,也应该是你的工作环境
- 已经熟练掌握了定位工具和方法,请参考JavaApp自动化测试系列[v1.0.0][uiautomatorviewer元素定位]和JavaApp自动化测试系列[v1.0.0][Appium Inspector元素定位]
- 已经熟练掌握了Driver API,请参考JavaApp自动化测试系列[v1.0.0][Driver API详解]
- 已经熟练掌握了KeyCode,请参考JavaApp自动化测试系列[v1.0.0][Keycode详解]
- 已经熟练掌握了Desired Capabilities,请参考JavaApp自动化测试系列[v1.0.0][Desired Capabilities]
- 已经熟练掌握了Appium的使用,请参考JavaApp自动化测试系列[v1.0.0][Appium使用详解]
- 已经熟练掌握了SDK的安装配置与使用,请参考JavaApp自动化测试系列[v1.0.0][安卓SDK]
- 已经熟练掌握了adb命令的使用,请参考JavaApp自动化测试系列[v1.0.0][adb命令]
- 已经熟练掌握了Maven环境的安装配置与使用,请参考Java自动化测试系列[v1.0.0][Maven开发环境]
- 已经熟练掌握了Java单元测试框架TestNG的使用,请参考Java自动化测试系列[v1.0.0][TestNG测试开发环境配置]和单元测试系列[v1.0.1][TestNG单元测试报告]和单元测试系列[v1.0.0][Maven深度绑定TestNG]
框架结构
AppTestFrameWorkDT/
|----src/main/java
|----com.davieyang.base/
|----BaseActivity.java
|----com.davieyang.pages/
|----HomePage.java
|----RegisterPage.java
|----RegisterVerifyPage.java
|----com.davieyang.util/
|----Constants.java
|----DataProviderFromCsv.java
|----src/test/java
|----com.davieyang.testScripts/
|----TestRegister.java
|----data
|----register.csv
|----config
|----log4j.properties
|----pom.xml
|----testng.xml
无框架支撑的代码示例
配置pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.davieyang</groupId>
<artifactId>AppTestFramework</artifactId>
<version>1.0-SNAPSHOT</version>
<name>AppTestFramework</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-api -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-api</artifactId>
<version>3.141.59</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-server-standalone -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-server-standalone</artifactId>
<version>2.53.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.appium/java-client -->
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>7.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.14.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- default lifecycle, jar packaging: see https://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
<!-- site lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#site_Lifecycle -->
<plugin>
<artifactId>maven-site-plugin</artifactId>
<version>3.7.1</version>
</plugin>
<plugin>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
代码示例
package org.davieyang.testscripts;
import io.appium.java_client.android.AndroidDriver;
import java.io.File;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import io.appium.java_client.android.AndroidElement;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import io.appium.java_client.remote.AndroidMobileCapabilityType;
import io.appium.java_client.remote.AutomationName;
import io.appium.java_client.remote.MobileCapabilityType;
public class TestRegister
{
AndroidDriver<AndroidElement> driver;
@BeforeMethod
public void setUp() throws Exception{
DesiredCapabilities capabilities = new DesiredCapabilities();
File appDir = new File("E:\\");
File app = new File(appDir, "selendroid-test-app-0.17.0.apk");
capabilities.setCapability("deviceName", "Redmi 3");
capabilities.setCapability("app", "app.getAbsolutePath()");
capabilities.setCapability("automationName", AutomationName.APPIUM);
capabilities.setCapability("appPackage", "io.selendroid.testapp");
capabilities.setCapability("appActivity", ".HomeScreenActivity");
driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}
@Test
public void testWebApp(){
// 对象时别
WebElement startRegister_btn = driver.findElementById("io.selendroid.testapp:id/startUserRegistration");
// 单击文件夹控制,页面跳转
startRegister_btn.click();
// 对象:用户名
WebElement username_txt = driver.findElementById("io.selendroid.testapp:id/inputUsername");
// 输入用户名
username_txt.sendKeys("davieyang");
// 对象:Email
WebElement email_txt = driver.findElementById("io.selendroid.testapp:id/inputEmail");
// 输入Email
email_txt.sendKeys("davieyang@qq.com");
// 对象:密码
WebElement password_txt = driver.findElementById("io.selendroid.testapp:id/inputPassword");
// 输入密码
password_txt.sendKeys("davieyang");
// 对象:姓名
WebElement name_txt = driver.findElementById("io.selendroid.testapp:id/inputName");
// 清空姓名默认值
name_txt.clear();
// 输入姓名
name_txt.sendKeys("davieyang");
// 对象:编程语言
WebElement language = driver.findElementById("io.selendroid.testapp:id/input_preferedProgrammingLanguage");
// 进入编程语言选择界面
language.click();
// 对象Scala语言
WebElement scaLanguage = driver.findElementByAndroidUIAutomator("text{\"Scala\"}");
// 选择Scala语言
scaLanguage.click();
// 对象:接受条件
WebElement accept_check = driver.findElementById("io.selendroid.testapp:id/input_adds");
// 单击接受条件
accept_check.click();
// 对象:注册用户
WebElement register_btn = driver.findElementById("io.selendroid.testapp:id/btnRegisterUser");
// 单击注册用户按钮
register_btn.click();
// 对象:姓名确认
WebElement label_name_data = driver.findElementById("label_name_data");
// 断言判断,姓名确认对象的值是否为前面输入的“davieyang”
Assert.assertEquals(label_name_data.getText().toString(), "davieyang");
}
@AfterMethod
public void tearDown(){
driver.quit();
}
}
Page Object模式
- 在本章节开始前应熟练掌握PO模式的思想,请参考Java自动化测试系列[v1.0.0][PO设计模式]和Java自动化测试系列[v1.0.1][PO设计模式]
Page Object模式是Selenium中的一种设计模式,Appium也继承了这种设计模式
页面对象封装
在项目结构中新增页面元素对象的package,在该package下新增三个页面对象类,分别为HomePage
,RegisterPage
,RegisterVerifyPage
,代码如下
package org.davieyang.pages;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.By;
public class HomePage {
AndroidDriver<?> driver;
public HomePage(AndroidDriver<?> driver){
this.driver = driver;
}
private static By startRegister_btn = By.id("startUserRegistration");
public RegisterPage navigate_register_page(){
driver.findElement(startRegister_btn).click();
// 返回RegisterPage页面对象
return new RegisterPage(driver);
}
}
package org.davieyang.pages;
import org.openqa.selenium.By;
import io.appium.java_client.android.AndroidDriver;
public class RegisterPage {
AndroidDriver<?> driver;
public RegisterPage(AndroidDriver<?> driver){
this.driver = driver;
}
public static By username_txt = By.id("inputUsername");
public static By email_txt = By.id("inputEmail");
public static By password_txt = By.id("inputPassword");
public static By name_txt = By.id("inputName");
public static By language_sel = By.id("input_preferedProgrammingLanguage");
public static By prgLanguage = By.name("Scala");
public static By accept_check = By.id("input_adds");
public static By register_btn = By.id("btnRegisterUser");
public RegisterVerifyPage register_sucess(String username, String email, String password, String name){
driver.findElement(username_txt).sendKeys(username);
driver.findElement(email_txt).sendKeys(email);
driver.findElement(password_txt).sendKeys(password);
driver.findElement(name_txt).clear();
driver.findElement(language_sel).sendKeys(name);
driver.findElement(prgLanguage).click();
driver.findElement(accept_check).click();
driver.findElement(register_btn).click();
// 返回RegisterVerifyPage页面对象
return new RegisterVerifyPage(driver);
}
}
package org.davieyang.pages;
import org.openqa.selenium.By;
import io.appium.java_client.android.AndroidDriver;
public class RegisterVerifyPage {
AndroidDriver<?> driver;
public RegisterVerifyPage(AndroidDriver<?> driver){
this.driver = driver;
}
public static By label_name_data = By.id("label_name_data");
public String get_name_value(){
return driver.findElement(label_name_data).getText().toString();
}
}
PO模式下的测试代码
修改之前的测试代码,如下所示
package org.davieyang.testscripts;
import io.appium.java_client.android.AndroidDriver;
import java.io.File;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import io.appium.java_client.android.AndroidElement;
import org.davieyang.pages.HomePage;
import org.davieyang.pages.RegisterPage;
import org.davieyang.pages.RegisterVerifyPage;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import io.appium.java_client.remote.AndroidMobileCapabilityType;
import io.appium.java_client.remote.AutomationName;
import io.appium.java_client.remote.MobileCapabilityType;
public class TestRegister
{
AndroidDriver<AndroidElement> driver;
@BeforeMethod
public void setUp() throws Exception{
DesiredCapabilities capabilities = new DesiredCapabilities();
// File appDir = new File("E:\\");
// File app = new File(appDir, "selendroid-test-app-0.17.0.apk");
capabilities.setCapability("deviceName", "Redmi 3");
capabilities.setCapability("udid", "claeae297d72");
capabilities.setCapability("platformVersion", "5.1.1");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("app", "app.getAbsolutePath()");
// capabilities.setCapability("automationName", AutomationName.APPIUM);
capabilities.setCapability("appPackage", "io.selendroid.testapp");
capabilities.setCapability("appActivity", ".HomeScreenActivity");
capabilities.setCapability("unicodeKeyboard", "True");
capabilities.setCapability("noSign", "True");
driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void test_Register_success(){
HomePage homePage = new HomePage(driver);
RegisterPage registerPage = homePage.navigate_register_page();
RegisterVerifyPage registerVerifyPage = registerPage.register_sucess("davieyang",
"davieyang@qq.com","111111", "Daveiyang.D.Y");
Assert.assertEquals(registerVerifyPage.get_name_value(), "Daveiyang.D.Y");
}
@AfterMethod
public void tearDown(){
driver.quit();
}
}
PageFactory模式
PageFactory模式的概念和PO类似,或者说是它的一种扩展,通过注解的方式定位元素对象,需要在构建函数里调用PageFactory.initElements(driver,this)
来初始化PO对象,修改页面对象类文件,代码示例如下
package org.davieyang.pages;
import io.appium.java_client.android.AndroidDriver;
// PageFactory
import org.openqa.selenium.support.PageFactory;
import io.appium.java_client.pagefactory.AppiumFieldDecorator;
import io.appium.java_client.pagefactory.AndroidFindBy;
import io.appium.java_client.pagefactory.iOSXCUITFindBy;
import io.appium.java_client.MobileElement;
public class HomePage_PF {
AndroidDriver<?> driver;
public HomePage_PF(AndroidDriver<?> driver){
this.driver = driver;
PageFactory.initElements(new AppiumFieldDecorator(driver), this);
}
// 页面对象时别,进入注册页面
@AndroidFindBy(id="startUserRegistration")
@iOSXCUITFindBy(id="startUserRegistration")
public MobileElement startRegister_btn;
// 单击注册页面按钮,进入注册页面,返回注册页面对象
public RegisterPage_PF navigate_register_page(){
this.startRegister_btn.click();
return new RegisterPage_PF(driver);
}
}
package org.davieyang.pages;
import java.util.List;
import org.openqa.selenium.By;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.support.PageFactory;
import io.appium.java_client.pagefactory.AppiumFieldDecorator;
import io.appium.java_client.pagefactory.AndroidFindBy;
import io.appium.java_client.pagefactory.iOSXCUITFindBy;
import io.appium.java_client.MobileElement;
public class RegisterPage_PF {
AndroidDriver<?> driver;
public RegisterPage_PF(AndroidDriver<?> driver){
this.driver = driver;
PageFactory.initElements(new AppiumFieldDecorator(driver), this);
}
// 页面对象时别
// 用户名
@AndroidFindBy(id="inputUsername")
@iOSXCUITFindBy(id="inputUsername")
public MobileElement username_txt;
// 邮箱
@AndroidFindBy(id="inputEmail")
@iOSXCUITFindBy(id="inputEmail")
public MobileElement email_txt;
// 密码
@AndroidFindBy(id="inputPassword")
@iOSXCUITFindBy(id="inputPassword")
public MobileElement password_txt;
// 姓名
@AndroidFindBy(id="inputName")
@iOSXCUITFindBy(id="inputName")
public MobileElement name_txt;
// 编程语言
@AndroidFindBy(id="input_preferedProgrammingLanguage")
@iOSXCUITFindBy(id="input_preferedProgrammingLanguage")
public MobileElement language_sel;
public List<MobileElement> prgLanguage;
// 是否确认注册
@AndroidFindBy(id="input_adds")
@iOSXCUITFindBy(id="input_adds")
public MobileElement accept_check;
// 注册按钮
@AndroidFindBy(id="btnRegisterUser")
@iOSXCUITFindBy(id="btnRegisterUser")
public MobileElement register_btn;
public RegisterVerifyPage_PF register_sucess(String username, String email, String password, String name,
String language, String check){
this.username_txt.sendKeys(username);
this.email_txt.sendKeys(email);
this.password_txt.sendKeys(password);
this.name_txt.clear();
this.name_txt.sendKeys(name);
this.language_sel.click();
checkedTextView(language);
if(check.equals("Yes")){
if(!this.accept_check.isSelected())
this.accept_check.click();
}
this.register_btn.click();
return new RegisterVerifyPage_PF(driver);
}
public void checkedTextView(String language){
// 使用class属性选择所有的单选按钮,并存放在一个list中
@SuppressWarnings("unchecked")
List<MobileElement> checkTextViews = (List<MobileElement>) driver
.findElementsByClassName("android.widget.CheckedTextVeiw");
// 使用for循环将List中的每个单选按钮进行遍历,查找name值为Ruby的单选按钮
// 如果该按钮未处于选中状态则调用click方法进行选择
for (MobileElement checkedTextView:checkTextViews){
if(checkedTextView.getAttribute("name").equals(language)){
if(!checkedTextView.isSelected()){
checkedTextView.click();
}
}
}
}
}
package org.davieyang.pages;
import org.openqa.selenium.By;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.support.PageFactory;
import io.appium.java_client.pagefactory.iOSXCUITFindBy;
import io.appium.java_client.pagefactory.AndroidFindBy;
import io.appium.java_client.pagefactory.AppiumFieldDecorator;
import io.appium.java_client.MobileElement;
public class RegisterVerifyPage_PF {
AndroidDriver<?> driver;
public RegisterVerifyPage_PF(AndroidDriver<?> driver){
this.driver = driver;
PageFactory.initElements(new AppiumFieldDecorator(driver), this);
}
// 页面对象识别
@AndroidFindBy(id="label_name_data")
@iOSXCUITFindBy(id="label_name_data")
public MobileElement label_name_data;
// 页面对象识别
@AndroidFindBy(id="label_username_data")
@iOSXCUITFindBy(id="label_username_data")
public MobileElement label_username_data;
// 页面对象识别
@AndroidFindBy(id="label_password_data")
@iOSXCUITFindBy(id="label_password_data")
public MobileElement label_password_data;
// 页面对象识别
@AndroidFindBy(id="label_email_data")
@iOSXCUITFindBy(id="label_email_data")
public MobileElement label_email_data;
// 页面对象识别
@AndroidFindBy(id="label_preferedProgrammingLanguage_data")
@iOSXCUITFindBy(id="label_preferedProgrammingLanguage_data")
public MobileElement label_preferedProgrammingLanguage_data;
// 页面对象识别
@AndroidFindBy(id="label_acceptAdds_data")
@iOSXCUITFindBy(id="label_acceptAdds_data")
public MobileElement label_acceptAdds_data;
// 增加验证项,返回验证值
public String get_name_value(){
return this.label_name_data.getTagName().toString();
}
// 增加验证项,返回验证值
public String get_username_value(){
return this.label_username_data.getTagName().toString();
}
// 增加验证项,返回验证值
public String get_password_value(){
return this.label_password_data.getTagName().toString();
}
// 增加验证项,返回验证值
public String get_email_value(){
return this.label_email_data.getTagName().toString();
}
// 增加验证项,返回验证值
public String get_preferedProgrammingLanguage_value(){
return this.label_preferedProgrammingLanguage_data.getTagName().toString();
}
// 增加验证项,返回验证值
public String get_acceptAdds_value(){
return this.label_acceptAdds_data.getTagName().toString();
}
}
数据驱动
测试数据I
新增util
package,用于放置公共函数,在该包下新建Constants类,并在该类中新增如下代码
package com.davieyang.util;
public class Constants {
public class 注册页面{
public static final String username = "davieyang";
public static final String eMail = "davieyang@qq.com";
public static final String password = "111111";
public static final String name = "davieyang";
public static final String proLanguage = "PHP";
public static final String accept = "true";
}
}
测试代码
基于Page Factory模式和测试数据分离后的测试代码如下
package org.davieyang.testscripts;
import io.appium.java_client.android.AndroidDriver;
import java.io.File;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import io.appium.java_client.android.AndroidElement;
import org.davieyang.pages.HomePage_PF;
import org.davieyang.pages.RegisterPage;
import org.davieyang.pages.RegisterPage_PF;
import org.davieyang.pages.RegisterVerifyPage_PF;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import io.appium.java_client.remote.AndroidMobileCapabilityType;
import io.appium.java_client.remote.AutomationName;
import io.appium.java_client.remote.MobileCapabilityType;
import com.davieyang.util.Constants;
public class TestRegister
{
AndroidDriver<AndroidElement> driver;
@BeforeMethod
public void setUp() throws Exception{
DesiredCapabilities capabilities = new DesiredCapabilities();
// File appDir = new File("E:\\");
// File app = new File(appDir, "selendroid-test-app-0.17.0.apk");
capabilities.setCapability("deviceName", "Redmi 3");
capabilities.setCapability("udid", "claeae297d72");
capabilities.setCapability("platformVersion", "5.1.1");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("app", "app.getAbsolutePath()");
// capabilities.setCapability("automationName", AutomationName.APPIUM);
capabilities.setCapability("appPackage", "io.selendroid.testapp");
capabilities.setCapability("appActivity", ".HomeScreenActivity");
capabilities.setCapability("unicodeKeyboard", "True");
capabilities.setCapability("noSign", "True");
driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void test_Register_success(){
HomePage_PF homePage_pf = new HomePage_PF(driver);
RegisterPage_PF registerPage_pf = homePage_pf.navigate_register_page();
RegisterVerifyPage_PF registerVerifyPage_pf = registerPage_pf.register_sucess(Constants.注册页面.username,Constants.注册页面.eMail,
Constants.注册页面.password, Constants.注册页面.name, Constants.注册页面.proLanguage, Constants.注册页面.accept);
Assert.assertEquals(registerVerifyPage_pf.get_name_value(), "Daveiyang.D.Y");
}
@AfterMethod
public void tearDown(){
driver.quit();
}
}
测试数据II
修改Constans类
package com.davieyang.util;
public class Constants {
public class 注册页面{
public static final String filepath = "F:\\datafilefolder";
public static final String filename = "register.csv";
}
}
在项目的根目录下新增data文件夹,并在文件夹内放置register.csv文件存入测试数据,如下所示
no | testName | username | password | name | prolanguage | accept | verifyusername | verifyeMail | |
---|---|---|---|---|---|---|---|---|---|
1 | register | davieyang | davieyang@qq.com | 111111 | davieyang | PHP | TRUE | register | davieyang |
pom中新增依赖
<dependency>
<groupId>net.sourceforge.javacsv</groupId>
<artifactId>javacsv</artifactId>
<version>2.0</version>
</dependency>
在util中新增DataProviderFromCsv类文件,并在文件中写入
package com.davieyang.util;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import com.csvreader.CsvReader;
import com.csvreader.CsvWriter;
public class DataProviderFromCsv {
public static Object[][] getTestData(String FileNameroot) throws IOException{
List<Object []> records = new ArrayList<Object[]>();
String record;
BufferedReader file = new BufferedReader(new InputStreamReader(new FileInputStream(FileNameroot), "GBK"));
file.readLine();
while((record = file.readLine())!=null){
String fields[]=record.split(",");
records.add(fields);
}
file.close();
Object[][] results = new Object[records.size()][];
for(int i=0; i<records.size(); i++){
results[i] = records.get(i);
}
return results;
}
}
测试代码
package org.davieyang.testscripts;
import com.davieyang.util.DataProviderFromCsv;
import io.appium.java_client.android.AndroidDriver;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import io.appium.java_client.android.AndroidElement;
import org.davieyang.pages.HomePage_PF;
import org.davieyang.pages.RegisterPage_PF;
import org.davieyang.pages.RegisterVerifyPage_PF;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import com.davieyang.util.Constants;
public class TestRegisterII
{
AndroidDriver<AndroidElement> driver;
@BeforeMethod
public void setUp() throws Exception{
DesiredCapabilities capabilities = new DesiredCapabilities();
// File appDir = new File("E:\\");
// File app = new File(appDir, "selendroid-test-app-0.17.0.apk");
capabilities.setCapability("deviceName", "Redmi 3");
capabilities.setCapability("udid", "claeae297d72");
capabilities.setCapability("platformVersion", "5.1.1");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("app", "app.getAbsolutePath()");
// capabilities.setCapability("automationName", AutomationName.APPIUM);
capabilities.setCapability("appPackage", "io.selendroid.testapp");
capabilities.setCapability("appActivity", ".HomeScreenActivity");
capabilities.setCapability("unicodeKeyboard", "True");
capabilities.setCapability("noSign", "True");
driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@DataProvider(name="RegisterData", parallel = true)
public static Object[][] getRegisterData() throws Exception{
return DataProviderFromCsv.getTestData(Constants.注册页面.filepath+ "/" +Constants.注册页面.filename);
}
@Test(dataProvider = "RegisterData")
public void test_Register_success(String no, String testName, String username, String eMail, String password, String name,
String prolanguage, String accept, String verifyusername, String verifyeMail, String verifypassword,
String verifyname, String verifyprolanguage, String verifyaccept) throws IllegalArgumentException, Exception{
HomePage_PF homePage_pf = null;
RegisterPage_PF registerPage_pf = null;
RegisterVerifyPage_PF registerVerifyPage_pf = null;
try{
homePage_pf = new HomePage_PF(driver);
registerPage_pf = homePage_pf.navigate_register_page();
registerVerifyPage_pf = registerPage_pf.register_sucess(username, eMail, password, name, prolanguage, accept);
}catch (AssertionError error){
Assert.fail("调用注册成功方法失败");
}
try{
Assert.assertEquals(registerVerifyPage_pf.get_name_value(), name);
}catch (AssertionError nameerror){
Assert.fail("name断言失败,查看name值是否正确");
}
try{
Assert.assertEquals(registerVerifyPage_pf.get_username_value(), username);
}catch (AssertionError usernameerror){
Assert.fail("username断言失败,查看username值是否正确");
}
try{
Assert.assertEquals(registerVerifyPage_pf.get_password_value(), password);
}catch (AssertionError passworderror){
Assert.fail("password断言失败,查看password值是否正确");
}
try{
Assert.assertEquals(registerVerifyPage_pf.get_preferedProgrammingLanguage_value(), prolanguage);
}catch (AssertionError prolanguageerror){
Assert.fail("prolanguage断言失败,查看prolanguage值是否正确");
}
try{
Assert.assertEquals(registerVerifyPage_pf.get_email_value(), eMail);
}catch (AssertionError eMailerror){
Assert.fail("eMail断言失败,查看eMail值是否正确");
}
try{
Assert.assertEquals(registerVerifyPage_pf.get_acceptAdds_value(), accept);
}catch (AssertionError accepterror){
Assert.fail("accept断言失败,查看accept值是否正确");
}
}
@AfterMethod
public void tearDown(){
driver.quit();
}
}