启动两个 appium 服务并连接两台 Android 设备,使用 testNg 并行测试,两台设备都能成功获取 driver 启动待测 app,后续其中一个 server 由于在超时时间内未获取到指令而自动关闭
appium+testng 并行测试时有没有人遇到过这种问题,怎么解决的?
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 中断。
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