Appium Android ——利用 TestNG 并行执行用例

一、测试类*注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参数的办法

 本帖已被设为精华帖!
共收到  22 条回复
110
lihuazhang ·  #1 · 6 月前  喜欢 

这可以成为appium并行执行的指导文章。大赞!不过你试过跑ui的用例了么?

110
lihuazhang ·  #2 · 6 月前  喜欢 

第一个:
Port:4723
bootstrapPort:4724

第二个:
Port:4725
bootstrapPort:4726

可以解释下 这两个端口的意思

104
seveniruby ·  #3 · 6 月前  喜欢 

不错, 已经开始用maven了, maven和gradle都集成了一些测试约定流程。 目前貌似没多少人熟悉。

213
luis ·  #4 · 6 月前  喜欢 

@lihuazhang 没记错第一个应该是脚本作为client和appiumserver通信的端口,后者是mobile device和appium通信的端口,也是android上特有的。

29bea2982cf098795915166d7a907376
cosyman ·  #5 · 6 月前  喜欢 

AppiumServer一共用到大概5个端口,4723默认为Bootstrap的socket端口。

示例为suite级别的并行run,如果suite不平衡不能充分利用Device资源,我们现在用case级别的并行run

110
lihuazhang ·  #6 · 6 月前  喜欢 

#5楼 @cosyman 介绍下?

480
sanlengjingvv ·  #7 · 6 月前  喜欢 

#2楼 @lihuazhang 
跑过ui用例,有什么问题吗?
帖子有更新。

1110
ka612nd ·  #8 · 6 月前  喜欢 

看了一下午了,这篇最给力了!赞!

1686
nancy2896 ·  #9 · 6 月前  喜欢 

#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不存在

这个是为什么

480
sanlengjingvv ·  #10 · 6 月前  喜欢 

#9楼 @nancy2896 
这是Maven的问题,去看看Maven的书或资料吧。光看错误只能猜咯,maven配置、pom写错、依赖没下完等等……
完全按这个帖子来的前提是能正确用命令行执行Maven和TestNG。

692
tspring ·  #11 · 5 月前  喜欢 


为什么我的一直不生效? 但在target\test-classes可以找到class文件啊 ?

29bea2982cf098795915166d7a907376
cosyman ·  #12 · 5 月前  喜欢 

#6楼 @lihuazhang 首先是Appium的Wrapper可以重启,有端口分配的功能。然后case的并发级别设置为 parallel="methods" 。 然后在每个case跑之前去获取一个Device和一个可用的Appium,跑完再把Device放回去。这里主要是Device的取和放,要做到阻塞,这样就可以控制并行执行

110
lihuazhang ·  #13 · 5 月前  喜欢 

#12楼 @cosyman Appium的Wrapper 是什么?那这样你就写个afterMethod了。

29bea2982cf098795915166d7a907376
cosyman ·  #14 · 5 月前  喜欢 

#13楼 @lihuazhang 
afterMethod 或 afterClass,Suite 取决于我们对Appium稳定性的把控吧。
Appium Server

1686
nancy2896 ·  #15 · 5 月前  喜欢 

#10楼 @sanlengjingvv 传suitethreadpoolsize参数

suitethreadpoolsize参数是什么意思啊?

480
sanlengjingvv ·  #17 · 4 月前  喜欢 

#15楼 @nancy2896 
http://testng.org/doc/documentation-main.html
这个页面搜suitethreadpoolsize

2113
testly ·  #18 · 4 月前  喜欢 

我试过只能在一个设备跑

19楼 已删除.
1653
jindejava ·  #20 · 4 月前  喜欢 

顶一个。。。

439
jelver ·  #21 · 3 月前  喜欢 

好文章不少啊

581558b0b3cebf34667616fc9cbb29b3
xiaobanli ·  #22 · 3 月前  喜欢 

@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>

23楼 已删除.
480
sanlengjingvv ·  #24 · 3 月前  喜欢 

#22楼 @xiaobanli

这个帖子里没有用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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值