Appium [已解决] 连接 2 个设备测试的时候每次都只运行一个设备?

梁慧玲 · December 08, 2014 · Last by yunmenzhe replied at August 01, 2017 · 3301 hits

我写了2个测试类 , 测试连接2个设备
adb devices里面能查到两个设备,但是每次运行的时候总是只运行一个设备

 info: [debug] 2 device(s) connected
> info: Found device 2b7a4e3f
> info: [debug] Setting device id to 2b7a4e3f
> info: [debug] Waiting for device to be ready and to respond to shell commands (timeout = 5)
> info: [debug] executing cmd: D:\sdk\platform-tools\adb.exe -s 2b7a4e3f wait-for-device

appium启动了2个,端口分别设置为了4723 4725,

在测试类中也分别指定了设备和端口,还需要设置什么么?怎么才能分别运行两个设备

测试类如下:

第一个测试类如下:

protected void setUp() throws Exception {
// TODO Auto-generated method stub
super.setUp();

File classpathRoot = new File(System.getProperty("user.dir"));
File appDir = new File(classpathRoot, "/apps/teacher");
File app = new File(appDir, "teacher-3.1.3-callnames--ceshi-2014-12-08-1457.apk");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("deviceName","2b7a4e3f");
capabilities.setCapability("platformVersion", "4.4");
capabilities.setCapability("app", app.getAbsolutePath());
capabilities.setCapability("appPackage", "air.com.xueersi.ICSClient3");
capabilities.setCapability("appActivity", "air.com.xueersi.ICSClient3.LoginActivity");
//设置中文输入的属性
capabilities.setCapability("unicodeKeyboard", "True");
capabilities.setCapability("resetKeyboard", "True");
driver = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);

}

@Override
protected void tearDown() throws Exception {
// TODO Auto-generated method stub
driver.quit();
}

/**
* Rigourous Test :-)
* @throws InterruptedException
*/

public void testLogin() throws InterruptedException
{
List<WebElement> textFieldsList = driver.findElementsByClassName("android.widget.EditText");


textFieldsList.get(0).sendKeys("滕俊龙");
Thread.sleep(2000);

textFieldsList.get(1).sendKeys("020639");
Thread.sleep(2000);


driver.findElementByName("登录").click();
Thread.sleep(2000);
}

第二个测试类如下:

protected void setUp() throws Exception {
// TODO Auto-generated method stub
super.setUp();

File classpathRoot = new File(System.getProperty("user.dir"));
File appDir = new File(classpathRoot, "/apps/student");
File app = new File(appDir, "test_stu.apk");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("deviceName","430072fe470da099");
capabilities.setCapability("platformVersion", "4.3");
capabilities.setCapability("app", app.getAbsolutePath());
capabilities.setCapability("appPackage", "air.ICS.StudentClient");
capabilities.setCapability("appActivity", "air.ICS.StudentClient.LoginActivity");
//设置中文输入的属性
capabilities.setCapability("unicodeKeyboard", "True");
capabilities.setCapability("resetKeyboard", "True");
driver = new AppiumDriver(new URL("http://127.0.0.1:4725/wd/hub"), capabilities);

}

@Override
protected void tearDown() throws Exception {
// TODO Auto-generated method stub
driver.quit();
}


/**
* Rigourous Test :-)
* @throws InterruptedException
*/

public void testLogin() throws InterruptedException
{


WebElement login_with_guest_button = driver.findElement(By.id("login_with_guest"));
login_with_guest_button.click();
Thread.sleep(2000);
List<WebElement> textFieldsList = driver.findElementsByClassName("android.widget.EditText");


textFieldsList.get(0).sendKeys("aaaaa");
Thread.sleep(2000);


driver.findElementByName("登录").click();

}

为什么执行的时候 4725这个端口的appium不执行测试?
log提示找不到activity 我确定我的activity设置没有错。

 error: Failed to start an Appium session, err was: Error: Activity used to start app doesn't exist or cannot be launched! Make sure it exists and is a launchable activity
> info: [debug] Cleaning up android objects
> info: [debug] Cleaning up appium session
> info: [debug] Error: Activity used to start app doesn't exist or cannot be launched! Make sure it exists and is a launchable activity
> at null.<anonymous> (E:\study\xuexi\Appium\Appium\node_modules\appium\node_modules\appium-adb\lib\adb.js:1130:21)
> at null.<anonymous> (E:\study\xuexi\Appium\Appium\node_modules\appium\node_modules\appium-adb\lib\adb.js:172:9)
> at ChildProcess.exithandler (child_process.js:635:7)
> at ChildProcess.EventEmitter.emit (events.js:98:17)
> at maybeClose (child_process.js:743:16)
> at Process.ChildProcess._handle.onexit (child_process.js:810:5)
> info: [debug] Responding to client with error: {"status":33,"value":{"message":"A new session could not be created. (Original error: Activity used to start app doesn't exist or cannot be launched! Make sure it exists and is a launchable activity)","origValue":"Activity used to start app doesn't exist or cannot be launched! Make sure it exists and is a launchable activity"},"sessionId":null}
> info: <-- POST /wd/hub/session 500 27197.867 ms - 346

共收到 35 条回复 时间 点赞

我也想知道怎么同时控制两台设备,求高人指点

http://testerhome.com/topics/1639
测试类加udid,deviceName虽然必须写但没用
server还要指定bootstrapPort

可以开两个appium.exe,分别监听两个不同的端口。代码里不一样的设备初始化driver端口区别开。

#3楼 @yangyajie_002 你看我代码里也指定了啊 我也是启动了两个appium 都设置过了
每次还是都只能发现一个设备

#2楼 @sanlengjingvv udid是啥?

我现在能跑两个类了,但是为什么只能发现一个设备?

#5楼 @nancy2896
capabilities.setCapability("udid", "这里填adb devices得到的");

http://appium.io/slate/en/v1.3.4/?ruby#server-flags

#7楼 @sanlengjingvv
我是Android设备 应该设置名字就行了吧
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("deviceName","430072fe4758a07f");
capabilities.setCapability("platformVersion", "4.3");

每个类我都指定了设备 可还是发现2个 执行用例只用一个

#8楼 @nancy2896
看2楼,deviceName虽然必须写但没用

On Android this capability is currently ignored,你可以随便填看有没有效果

#9楼 @sanlengjingvv 恩 是没用 关键是连接2个设备的时候 怎么才能指定设备?没有办法了么?

我看官网上有这个命令
If we had two devices with the ID’s 43364 and 32456, we would start two different Appium servers with the following commands:

node . -p 4492 -bp 2251 -U 32456

node . -p 4491 -bp 2252 -U 43364

可是我执行的时候提示我

C:\Users\Administrator>node . -p 4492 -bp 2251 -U 32456

module.js:340
throw err;
^
Error: Cannot find module 'C:\Users\Administrator'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:902:3

appium -p 4492 -bp 2251 -U 32456

你是要同时并行两个设备进行测试吗?据我所知,目前appium还不支持,求打脸。。

#11楼 @sanlengjingvv 我通过这个启动了 还是不行
C:\Users\Administrator>appium -p 4492 -bp 2251 -U 430072fe4758a07f
C:\Users\Administrator>appium -p 4491 -bp 2252 -U 430072fe470da099

端口我都设置一样了 也是每次都只用一个设备执行 你用testNG并行的时候 确实两个机器都跑了?

#12楼 @sziitash
以下是官方文档说明

Parallel Android Tests
Appium provides a way for users to automate multiple Android sessions on a single machine. All it involves is starting multiple Appium servers with different flags.

The important flags for automating multiple Android sessions are:

-p the main Appium port
-U the device id
-bp the Appium bootstrap port
--chromedriver-port the chromedriver port (if using webviews or chrome)
--selendroid-port the selendroid port (if using selendroid)
More information on these flags can be found here.

If we had two devices with the ID’s 43364 and 32456, we would start two different Appium servers with the following commands:

node . -p 4492 -bp 2251 -U 32456

node . -p 4491 -bp 2252 -U 43364

As long as your Appium and Appium bootstrap ports are between 0 and 65536, all they have to be is different so that two Appium servers aren’t trying to listen on the same port. Be sure that your -u flag corresponds with the correct device ID. This is how Appium knows which device to communicate with, so it must be accurate.

If you are using chromedriver or selendroid, set a different port for each server.

#14楼 @nancy2896 谢谢你的回复。。

#14楼 @nancy2896 你试试用如下代码:
self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
指定服务端口时,4723改成4725看看

#16楼 @sziitash 我一个是4723 另一个是4725

#17楼 @nancy2896 要把udid和和端口号对应。我配置如下:
1、appium -p 4492 -bp 2251 -U 7b6312(这是udid哦)
2、appium -p 4493 -bp 2252 -U 1832596f(这是udid哦)

然后是跑两个脚本(我是用python的)。我亲自测试可以了。
感谢你们的提问和反馈啊,把一个一直困扰我的问题解决了。。

#18楼 @sziitash 只有ios设备才是udid吧 Android设备就是设备名吧 我用adb devices得到的名字 不对么?
appium里面要设置端口和bp端口么?

问题解决,初始化的时候多加一行
capabilities.setCapability("udid","430072fe470da099");
就能指定设备了

感谢楼上,终于找到正解了!

#18楼 @sziitash 你的Python是运行2个脚本吗?还是一个脚本里面实现两个设备启动!

#18楼 @sziitash 可以详细的解答一下不?你是怎么实现的?谢谢!

梁慧玲 #24 · March 31, 2015 作者

#23楼 @test20150331 capabilities.setCapability("udid","430072fe470da099");
关键就是这个 我是两个测试类里指定两个设备

你是一个脚本运行两个设备还是两个脚本分开,各自实现一个设备脚本运行?

#14楼 @nancy2896 哥们帮我一个大忙咯,谢了

梁慧玲 #27 · June 08, 2015 作者

#26楼 @lbxoqy 哈哈 我是姐们儿

楼主隔了这么久我才看到这个帖子,有个疑问希望帮忙解答,你写的这个脚本,设备的配置,做的操作。相当于有几台就要重复写几个,因为我的操作都一样,所以可不可以写多个设备的配置,只写一个操作,这样并行测试呢

#28楼 @enumerate 可以 用testng并行 用xml指定设备 有这个帖子https://testerhome.com/topics/1639

谢谢楼主

31Floor has been deleted

@nancy2896 lz,怎么解决的

现在解决了吗?

#18楼 @sziitash 请问你怎么用python实现的同时运行。一个类driver 还是写2个类不同的driver

benlee 回复

参考这里解决的。。。千辛万苦

启动appium时指定 udid 即可

xdlhy Appium 并发和主从远程控制 中提及了此贴 18 Sep 18:35
需要 Sign In 后方可回复, 如果你还没有账号请点击这里 Sign Up