一、测试类*注1
package com.testerhome;
import io.appium.java_client.android.AndroidDriver;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;
public class Suite1 {
public String port;
public String udid;
private AndroidDriver driver;
@Test
public void switches() throws InterruptedException {
WebElement sound = driver.findElementByAndroidUIAutomator("new UiSelector().text(\"Sound\")");
sound.click();
System.out.println("checked");
Thread.sleep(2000);
System.out.println(Thread.currentThread());
}
@BeforeSuite
@Parameters({ "port", "udid" })
public void beforeSuite(String port, String udid) {
this.port = port;
this.udid = udid;
}
@BeforeClass
public void beforeClass() throws MalformedURLException{
System.out.println(port + udid);
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName","device");
capabilities.setCapability("automationName","Appium");
capabilities.setCapability("platformVersion", "4.4");
capabilities.setCapability("udid", udid);
capabilities.setCapability("appPackage", "com.android.settings");
capabilities.setCapability("appActivity", ".Settings");
driver = new AndroidDriver(new URL("http://127.0.0.1:" + port + "/wd/hub"), capabilities);
}
@AfterClass
public void afterClass() {
driver.quit();
}
}
二、连接两个Android设备或启动两个虚拟机
使用
adb devices
获取udid
三、项目路径下新建两个testng.xml
testng1.xml
<?xml version="1.0" encoding="UTF-8"?> <suite name="Suite1"> <parameter name = "port" value = "4723"/> <parameter name = "udid" value = "emulator-5554"/> <test name="Test"> <classes> <class name="com.testerhome.Suite1"/> </classes> </test> </suite>
testng2.xml
<?xml version="1.0" encoding="UTF-8"?> <suite name="Suite2"> <parameter name = "port" value = "4725"/> <parameter name = "udid" value = "emulator-5556"/> <test name="Test"> <classes> <class name="com.testerhome.Suite1"/> </classes> </test> </suite>
四、开启两个appium server*注2、注3
第一个:
Port:4723
bootstrapPort:4724
第二个:
Port:4725
bootstrapPort:4726
五、导出依赖*注4
因为是用maven工程创建的,所以先导出依赖到项目路径下的lib文件夹
mvn dependency:copy-dependencies -DoutputDirectory=lib
六、执行测试
先用Maven串行执行一次以编译出Class文件
mvn clean test
然后
java -classpath ".\target\test-classes" -Djava.ext.dirs=lib org.testng.TestNG -suitethreadpoolsize 2 testng1.xml testng2.xml
如果没有配置TestNG环境变量
java -classpath ".\target\test-classes;D:\Programs\testng-6.8\testng-6.8.jar" -Djava.ext.dirs=lib org.testng.TestNG -suitethreadpoolsize 2 testng1.xml testng2.xml
七、查看报告
默认在项目路径下的test-output文件夹
注1:
这个测试类没有指定app路径,如果指定,同时unzip的时候会冲突。目前是复制了多个apk。
File app = new File(appDir, "AppName"+port+".apk");
并在appium server指定不同的临时文件路径,比如:
--tmp D:\tem1
--tmp D:\tem2
注2:
两个端口的介绍可以看这两个链接:
appium 自动化测试教程 ppt(第二版)
Appium Android Bootstrap源码分析之简介
注3:
如果使用到Selendroid或Chromium,还需要指定其他端口(需要修改测试类)
Selendroid port:8080
Selendroid port:8081
Chromium port:9515
Chromium port:9516
注4:
本来准备直接用mvn test并行执行的,但没试出来传suitethreadpoolsize参数的办法
这可以成为appium并行执行的指导文章。大赞!不过你试过跑ui的用例了么?
第一个:
Port:4723
bootstrapPort:4724
第二个:
Port:4725
bootstrapPort:4726
可以解释下 这两个端口的意思
不错, 已经开始用maven了, maven和gradle都集成了一些测试约定流程。 目前貌似没多少人熟悉。
@lihuazhang 没记错第一个应该是脚本作为client和appiumserver通信的端口,后者是mobile device和appium通信的端口,也是android上特有的。
#2楼 @lihuazhang
跑过ui用例,有什么问题吗?
帖子有更新。
#7楼 @sanlengjingvv 我执行mvn clean test的时候报错
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.
1:testCompile (default-testCompile) on project mytest: Compilation failure: Comp
ilation failure:
[ERROR] /E:/Users/Administrator/workspace/mytest/src/test/java/appiumdemo/mytest
/StuAppTest.java:[13,44] 程序包org.openqa.selenium.browserlaunchers不存在
[ERROR] /E:/Users/Administrator/workspace/mytest/src/test/java/appiumdemo/mytest
/TeaAppTest.java:[13,44] 程序包org.openqa.selenium.browserlaunchers不存在
这个是为什么
#9楼 @nancy2896
这是Maven的问题,去看看Maven的书或资料吧。光看错误只能猜咯,maven配置、pom写错、依赖没下完等等……
完全按这个帖子来的前提是能正确用命令行执行Maven和TestNG。
#6楼 @lihuazhang 首先是Appium的Wrapper可以重启,有端口分配的功能。然后case的并发级别设置为 parallel="methods" 。 然后在每个case跑之前去获取一个Device和一个可用的Appium,跑完再把Device放回去。这里主要是Device的取和放,要做到阻塞,这样就可以控制并行执行
#13楼 @lihuazhang
afterMethod 或 afterClass,Suite 取决于我们对Appium稳定性的把控吧。
Appium Server
#10楼 @sanlengjingvv 传suitethreadpoolsize参数
suitethreadpoolsize参数是什么意思啊?
#15楼 @nancy2896
http://testng.org/doc/documentation-main.html
这个页面搜suitethreadpoolsize
@sanlengjingvv 这样跑的话,生成的测试报告只有一份,testng2.xml会覆盖testng1.xml.
我用maven的pom.xml去配置testng.xml,但是跑下来的结果是,串行跑,一台跑完去跑另外一台,求指教,如何能并行跑。
代码如下:
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
...
<suiteXmlFiles>
<suiteXmlFile>testng2.xml</suiteXmlFile>
<suiteXmlFile>testng1.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
</plugins>
</build>
这个帖子里没有用maven去调用testng,maven只是用来管理依赖,最后是导出依赖用命令行调用testng,在命令行里传入suitethreadpoolsize参数利用testng提供的多线程功能并行执行。
testng提供了test、method、suit等几种并行方式,开始我也是准备用maven调用testng的,后来发现test、method之类都可以,只有suit不能做到并行,但我需要的是suit级的并行,所以就换成导出依赖命令行执行这种方式了。
按你的执行方法报告会不会覆盖我没试,不过报告是可以指定目录的,outputDirectory
http://testng.org/doc/documentation-main.html#logging-xml-reports