Appium appium 并行测试,其中一个 server 收不到指令导致中断

· 2017年11月09日 · 最后由 jignde 回复于 2019年08月05日 · 1733 次阅读

@DoctorQ @Lihuazhang

问题概述

启动两个 appium 服务并连接两台 Android 设备,使用 testNg 并行测试,两台设备都能成功获取 driver 启动待测 app,后续其中一个 server 由于在超时时间内未获取到指令而自动关闭

求助问题

appium+testng 并行测试时有没有人遇到过这种问题,怎么解决的?

环境描述

appium 服务

1、appium1 命令行启动参数
(1)appium1 命令行启动参数:
node D:\AutoTest\appium\appium1.4.16.1\Appium\node_modules\appium\lib\server\main.js --address 127.0.0.1 --port 4723 -bp 4724 -U 127.0.0.1:62001
(2)appium2 命令行启动参数:
node D:\AutoTest\appium\appium1.4.16.1\Appium\node_modules\appium\lib\server\main.js --address 127.0.0.1 --port 4725 -bp 4726 -U 127.0.0.1:62025
2、testNg 配置

<?xml version="1.0" encoding="UTF-8"?>
<suite name="Suite1" parallel="tests" thread-count="2">
  <test name="Test1" preserve-order="true" >
    <parameter name="nodeUrl" value="http://127.0.0.1:4723/wd/hub" />
    <classes>       


        <class name="com.cases.C_0001_ClearCache" />

    </classes>

  </test> 

  <test name="Test2" preserve-order="true">
    <parameter name="nodeUrl" value="http://127.0.0.1:4725/wd/hub" />
    <classes>       


        <class name="com.cases.C_0004_FeedBack" />

    </classes>

  </test> 


</suite> 

3、beforeMethod 参数传递代码:

4、driver 获取代码:

public static AndroidDriver<AndroidElement> getDriver(String nodeUrl) throws MalformedURLException {
        log.info("打开客户端");

        DesiredCapabilities cap = new DesiredCapabilities();
        cap.setCapability("automationName", sAutomationName);// 使用的自动化测试引擎Appium
                                                                // (默认)或
                                                                // Selendroid
        cap.setCapability("deviceName", sDeviceName); // 设备名称
        cap.setCapability("platformName", sPlatformName); // 安卓自动化还是IOS自动化
        cap.setCapability("platformVersion", sPlatformVersion); // 安卓操作系统版本
//      cap.setCapability("udid", udid); // 设备的udid (adb devices查看到的)
        cap.setCapability("appPackage", sAppPackage);// 被测app的包名
        cap.setCapability("appActivity", sAppActivity);// 被测app的入口Activity
        cap.setCapability("unicodeKeyboard", sUnicodeKeyboard); // 支持中文输入
        cap.setCapability("resetKeyboard", sResetKeyboard); // 支持中文输入,必须两条都配置
        cap.setCapability("noSign", sNnoSign); // 不重新签名apk
        cap.setCapability("noReset", sNoReset); // 不要在会话前重置应用状态
        cap.setCapability("app", sDir); // apk 位置
        cap.setCapability("newCommandTimeout", sNewCommandTimeout); // 没有新命令,appium20秒退出
        System.out.println("nodeUrl is " + nodeUrl);
        driver = new AndroidDriver<AndroidElement>(new URL(nodeUrl), cap);// 把以上配置传到appium服务端并连接手机
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);// 隐式等待

        log.info("打开客户端成功");

        return driver;
    }

测试执行过程

两台 Android 设备正常连接情况下,启动两个 appium,执行 testng.xml 进行多线程并行测试

期望结果

每一个 appium 连接一台设备执行一个 test 下的案例

测试结果描述

执行 testng.xml 文件后两个 appium 正常获取 driver 且在两个设备上成功启动待测 app,启动后其中一个 appium 正常运行且设备正常执行操作,另一个 appium 由于在超时时间内未收到指令导致 session 中断。

中断的 appium 日志如下:

info: [debug] Didn't get a new command in 60 secs, shutting down...
info: Shutting down appium session
info: [debug] Pressing the HOME button
info: [debug] executing cmd: F:\software\android-sdk\platform-tools\adb.exe -s 192.168.100.117:15033 shell "input keyevent 3"
info: [debug] Resetting IME to 'io.appium.android.ime/.UnicodeIME'
info: [debug] executing cmd: F:\software\android-sdk\platform-tools\adb.exe -s 192.168.100.117:15033 shell "ime set io.appium.android.ime/.UnicodeIME"
info: [debug] Stopping logcat capture
info: [debug] Logcat terminated with code null, signal SIGTERM
info: [debug] Sent shutdown command, waiting for UiAutomator to stop...
info: [debug] [BOOTSTRAP] [debug] Got data from client: {"cmd":"shutdown"}
info: [debug] [BOOTSTRAP] [debug] Got command of type SHUTDOWN
info: [debug] [BOOTSTRAP] [debug] Returning result: {"status":0,"value":"OK, shutting down"}
info: [debug] [BOOTSTRAP] [debug] Closed client connection
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: numtests=1
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: stream=.
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: id=UiAutomatorTestRunner
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: test=testRunServer
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: class=io.appium.android.bootstrap.Bootstrap
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: current=1
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS_CODE: 0
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS: stream=
info: [debug] [UIAUTOMATOR STDOUT] Test results for WatcherResultPrinter=.
info: [debug] [UIAUTOMATOR STDOUT] Time: 154.134
info: [debug] [UIAUTOMATOR STDOUT] OK (1 test)
info: [debug] [UIAUTOMATOR STDOUT] INSTRUMENTATION_STATUS_CODE: -1
info: [debug] UiAutomator shut down normally
info: [debug] Cleaning up android objects

共收到 10 条回复 时间 点赞

appium 接受指令的超时时间是可以配置的,你可以查下官方文档。

#2 · 2017年11月10日 Author
陈恒捷 回复

appium 超时时间改过多次,最长的时间已经设置为 180s,仍然是其中一个 server 会由于没有接收到新的命令而中断。网上找了一些别人做的 appium 并发例子,没发现跟我做的有什么区别

回复

180s 可能还是有点短,我一般是 7200s 。

#4 · 2017年11月10日 Author
陈恒捷 回复

哎,找到问题原因了,testng 多线程共享变量问题,加入锁机制解决问题

回复

赞。

不过为啥会和多线程共享变量有关?

回复

建议你把具体解决办法添加到主楼 方便其他有相同问题的人

我也遇到了相同的问题,这个应该具体怎么解决呢?

#8 · 2018年03月08日 Author
zm 回复

问题解决的时间用点久了,你可以看一下你是不是共享 driver 的问题,尽量不要用静态变量的 driver

9楼 已删除
10楼 已删除
回复

嗯嗯,我把 driver 的静态变量去掉,就可以啦,谢谢😃

请问,具体什么样的是静态 driver 呢?我也遇到了这样的问题,多谢了!
如果加锁,要怎么做?

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册