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

梁慧玲 · 2014年12月08日 · 最后由 yunmenzhe 回复于 2017年08月01日 · 2389 次阅读

我写了 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 条回复 时间 点赞

#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.

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

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

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

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

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

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 并行的时候 确实两个机器都跑了?

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

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

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

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

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

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

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

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

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

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

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

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

谢谢楼主

@nancy2896 lz,怎么解决的

现在解决了吗?

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

31楼 已删除
benlee 回复

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

xdlhy Appium 并发和主从远程控制 中提及了此贴 09月18日 18:35

启动 appium 时指定 udid 即可

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

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

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