Appium 如何能够使用一台电脑控制两个手机同时跑不同脚本呢

xy · February 17, 2017 · Last by yhui replied at February 21, 2017 · 2223 hits

我有两个脚本,脚本A一个是链接手机A(进入某个应用,然后按菜单键,把这个应用杀掉,然后再进入这个应用,不停循环);脚本B(是控制一个Android设备B,然后隔一段时间判断元素,然后进行截屏),现在想要用一台电脑同时控制手机A跑脚本A,设备B跑脚本B,该怎么做呢?求解答
脚本A
#! /usr/bin/env python
#coding=utf-8
import os
import time
import unittest
from appium import webdriver
import selenium
import subprocess
PATH = lambda p: os.path.abspath(os.path.join(os.path.dirname(file), p))
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '6.0.1'
desired_caps['deviceName'] = 'f0eb68f9'
desired_caps['appPackage'] = 'com.tencent.tws.gdevicemanager'
desired_caps['appActivity'] = 'com.tencent.tws.phoneside.SplashScreenActivity'
print"启动DM"
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

print"长按MENU"
driver.press_keycode(82)
time.sleep(5)
print"kill"
driver.swipe(768, 1802, 768, 400, 500)
time.sleep(5)
print"调起"
driver.start_activity('com.tencent.tws.gdevicemanager', 'com.tencent.tws.phoneside.SplashScreenActivity')
time.sleep(5)

脚本B
#! /usr/bin/env python
#coding=utf-8
import os
import time
import unittest
from appium import webdriver
import selenium
import subprocess
PATH = lambda p: os.path.abspath(os.path.join(os.path.dirname(file), p))
desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '5.1'
desired_caps['deviceName'] = 'PPW0443P6B0100755'
desired_caps['appPackage'] = 'com.android.settings'
desired_caps['appActivity'] = 'MainActivity'
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

print"BACK"
driver.press_keycode(3)
time.sleep(5)
print"判断"
d=0
for i in range(0,10):
try:
driver.find_element_by_id('com.tencent.tws.launcher:id/watch_face_home_status_icon_disconnect')
d=d+1
time.sleep(5)
print(d)
except Exception:
d=d
time.sleep(5)
print(d)
i+=1

共收到 8 条回复 时间 点赞

设置不同的端口号就行了

xy #3 · February 20, 2017 作者

#! /usr/bin/env python
#coding=utf-8
import os
import time
import unittest
from appium import webdriver
import selenium
import subprocess
import threading
def killdm():
PATH = lambda p: os.path.abspath(os.path.join(os.path.dirname(file), p))
desired_caps = {}
desired_caps['udid'] = 'f0eb68f9'
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '6.0.1'
desired_caps['deviceName'] = 'f0eb68f9'
desired_caps['appPackage'] = 'com.tencent.tws.gdevicemanager'
desired_caps['appActivity'] = 'com.tencent.tws.phoneside.SplashScreenActivity'
print"启动DM"
driver = webdriver.Remote('http://localhost:4724/wd/hub', desired_caps)
for a in range(0,10):
print"长按MENU"
driver.press_keycode(82)
time.sleep(5)
print"kill"
driver.swipe(768, 1802, 768, 400, 500)
time.sleep(5)
print"调起"
driver.start_activity('com.tencent.tws.gdevicemanager', 'com.tencent.tws.phoneside.SplashScreenActivity')
time.sleep(5)

def screen():
PATH = lambda p: os.path.abspath(os.path.join(os.path.dirname(file), p))
desired_caps = {}
desired_caps['udid'] = 'PPW0443P6B0100755'
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '5.1'
desired_caps['deviceName'] = 'PPW0443P6B0100755'
desired_caps['appPackage'] = 'com.android.settings'
desired_caps['appActivity'] = 'MainActivity'
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
time.sleep(2)
print"BACK"
driver.swipe(22, 205, 289, 205, 500)
time.sleep(5)
print"判断"
d = 0
for i in range(0, 10):
try:
driver.find_element_by_id('com.tencent.tws.launcher:id/watch_face_home_status_icon_disconnect')
d = d + 1
time.sleep(5)
print(d)
except Exception:
d = d
time.sleep(5)
print(d)
i=i+1
threads = []
t1 = threading.Thread(target=killdm())
threads.append(t1)
t2 = threading.Thread(target=screen())
threads.append(t2)
for t in threads:
#t1.start()
# t2.start()
#t.setDaemon(True)
t.start()
for t in threads:
t.join()
目前改成这样了,发现是先运行了killdm函数执行完后才执行screen函数,但是希望能同时运行这两个函数,该怎么做呢

adb指定手机device即可

—— 来自TesterHome官方 安卓客户端

#2楼 @xiaoafeizt 端口号是并发执行的,两个不同的脚本,实现信息交互,比如说一个打电话,一个接电话怎么实现呢?

#3楼 @xy5201314xy
print"长按MENU"
driver.press_keycode(82)
time.sleep(5)
这个不是长按吧

#5楼 @xukaili 你两个脚本写在一个脚本文件里执行,当然有先后啊,你写在两个脚本文件,同时执行两个呢?

用adb -s deviceID 来指定特定的设备

需要 Sign In 后方可回复, 如果你还没有账号请点击这里 Sign Up