用python同时启动多个appium,并让多个手机同时执行脚本

  1. 首先你要连接好多个手机
  2. 设置好已经连接好的手机的ip列表ipp = ['192xxx','192xxx']
  3. 杀掉之前所有appium进程subprocess.Popen('killall node',shell = True)
  4. 声明一个方法def start(ip):调用时需要把ipp传入
  5. 在方法中写for循环,循环内循环内用传入的ip来启动appium服务,注意要用不同的端口号启动,相差2以上
  6. 在循环中调用webdriver.Remote方法创建driver对象
  7. 在循环中创建脚本类的实例时,请在循环中创建,并且传入driver
    8.声明多线程列表
  8. 在循环中用多线程创建该实例的方法的对象,并加入多线程列表
  9. 在main函数中,先调用start方法,填充好多线程的列表。然后再用for循环启动多线程列表。
  10. 下面上start方法代码:

    def start(ip):
    for i in range(len(ip)):
    subprocess.Popen(‘/Applications/Appium.app/Contents/Resources/node/bin/node /Applications/Appium.app/Contents/Resources/node_modules/appium/build/lib/main.js –address “127.0.0.1” -p “’+str(4723+2*i)+’” –command-timeout “100” –automation-name “Appium” -U “’+ip[i]+’:’+str(5555+i)+’” >/tmp/1.txt’,shell=True)
    time.sleep(3.5)
    wzj = webdriver.Remote(‘http://localhost:’+str(4723+2*i)+’/wd/hub’, desired_caps)
    dingwei = DingWei(wzj, name)
    t = threading.Thread(target=dingwei.begin)
    tt.append(t)

  11. 上面的DingWei 是我的脚本类,其中的begin是类中的启动脚本的方法。下面上主函数的代码

    if name == “main“:
    start(ipp)
    time.sleep(2)
    for i in tt:
    i.start()


下面是全套脚本:拿去用吧,自己改改。

# -*- coding:utf-8 -*-
from __future__ import division
import time
import datetime,subprocess
import unittest
import threading
from appium import webdriver
import os
import xlrd,xlwt,xlutils
from xlutils.copy import copy
import wx
from HTMLTestRunner import HTMLTestRunner
from appium.webdriver.common.touch_action import TouchAction
from appium.webdriver.common.touch_action import TouchAction

desired_caps = {}
desired_caps['platformName'] = 'Android'
desired_caps['platformVersion'] = '5.1.1'
desired_caps['deviceName'] = 'hahaha'
desired_caps['appPackage'] = 'com.........blehunter.debug'
desired_caps['appActivity'] = 'com.........blehunter.ui.WelcomeActivity'
# desired_caps["unicodeKeyboard"] = "True"
# desired_caps["resetKeyboard"] = "True"
subprocess.Popen('killall node',shell=True)
ipp = ['192.168.0.171','192.168.0.175']
tt = []   #多线程对象
def server(ip):
    for i in range(len(ip)):
        name = "./new/"+str(ipp[i])+"*****"+time.ctime()+".xls"
        SZ = xlrd.open_workbook('./new/0.xls')
        ww = copy(SZ)
        ww.save(name)
        subprocess.Popen('/Applications/Appium.app/Contents/Resources/node/bin/node /Applications/Appium.app/Contents/Resources/node_modules/appium/build/lib/main.js --address "127.0.0.1" -p "'+str(4723+2*i)+'" --command-timeout "100"  --automation-name "Appium" -U "'+ip[i]+':'+str(5555+i)+'" >/tmp/1.txt',shell=True)
        time.sleep(3.5)
        wzj = webdriver.Remote('http://localhost:'+str(4723+2*i)+'/wd/hub', desired_caps)
        dingwei = DingWei(wzj, name)
        t = threading.Thread(target=dingwei.begin)
        tt.append(t)

class DingWei():
    def __init__(self,www,fname):
        self.wzj = www
        self.num = 0
        self.fname = fname
        self.SZ = xlrd.open_workbook(self.fname)
        self.sz = self.SZ.sheet_by_name("sheet1")
        self.ww = copy(self.SZ)
        self.nrows = self.sz.nrows
    def id(self,s):
        return self.wzj.find_element_by_id(s)
    ################
    def first_start(self):
        time.sleep(4)
        for i in range(7):
            try:
                self.wzj.swipe(1000, 1500, 180, 1500)
            except:
                self.wzj.swipe(400,706,90,706)
            time.sleep(0.2)
        time.sleep(3)
        try:
            self.wzj.find_element_by_id("com.........blehunter.debug:id/bt_guide_login").click()
        except:
            pass
        time.sleep(4)
        self.id('com.........blehunter.debug:id/et_phone').send_keys('18810437161')
        self.id('com.........blehunter.debug:id/et_phone_password').send_keys('qwerty')
        self.id('com.........blehunter.debug:id/btn_login').click()
        time.sleep(5)
    def chushihua(self):
        while 1:
            if self.m.is_enabled():
                try:
                    self.id('com.........blehunter.debug:id/tv_location_failed')
                except:
                    pass
                break
            else:
                pass
    def panduan(self,no):
        while 1:
            if self.m.is_enabled():
                try :
                    self.id('com.........blehunter.debug:id/tv_location_failed')
                    self.ww.get_sheet(0).write(no+1, 1, "failed")
                    os.remove(self.fname)
                    self.ww.save(self.fname)
                except :
                    self.ww.get_sheet(0).write(no+1, 1, "success")
                    os.remove(self.fname)
                    self.ww.save(self.fname)
                    self.num += 1
                break
            else:
                pass
    def begin(self):
        #self.first_start()
        self.wzj.implicitly_wait(10)
        b = self.wzj.find_element_by_xpath('//android.support.v7.widget.RecyclerView/android.widget.FrameLayout[2]/android.widget.ImageView')
        b.click()
        print self.wzj,u"初始次... "
        self.m = self.id('com.........blehunter.debug:id/iv_refresh_friend_location')
        self.chushihua()
        j = 0
        time0 = datetime.datetime.now()
        #总次数
        n = input(u'请输入测试次数,必须为整数!')
        for i in range(n):
            j += 1
            time.sleep(1)
            time1 = datetime.datetime.now()
            self.id('com.........blehunter.debug:id/iv_refresh_friend_location').click()
            self.panduan(i)
            time2 = datetime.datetime.now()
            self.ww.get_sheet(0).write(i+1, 2, "j")
            self.ww.get_sheet(0).write(i+1, 3, str(self.num))
            self.ww.get_sheet(0).write(i+1, 4, str(j-self.num))
            self.ww.get_sheet(0).write(i+1, 5, str((time2-time1).seconds))
            self.ww.get_sheet(0).write(i+1, 6, str(int((time2-time0).seconds)-j))
            os.remove(self.fname)
            self.ww.save(self.fname)
        self.ww.get_sheet(0).write(n+2, 0,str(((self.num)/n)*100)[:6]+'%' )
        os.remove(self.fname)
        self.ww.save(self.fname)


if __name__ == "__main__":
    server(ipp)
    time.sleep(2)
    for i in tt:
        i.start()
可以通过以下步骤实现Python Appium多机运行: 1. 启动多个Appium Server。可以使用Appium Desktop或终端启动多个Appium Server。例如,使用终端命令`appium -p 4723 -bp 4724 --udid device1` 和 `appium -p 4725 -bp 4726 --udid device2` 启动两个Appium Server。 2. 在Python脚本中使用多个Appium Server。可以使用Appium-Python-Client库在Python脚本中连接多个Appium Server。在启动多个Appium Server时,需要指定不同的端口和设备UDID。例如: ``` from appium import webdriver desired_caps1 = { 'platformName': 'Android', 'deviceName': 'device1', 'appPackage': 'com.example.app', 'appActivity': 'MainActivity', 'udid': 'device1', 'automationName': 'UiAutomator2', 'noReset': 'true' } driver1 = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps1) desired_caps2 = { 'platformName': 'Android', 'deviceName': 'device2', 'appPackage': 'com.example.app', 'appActivity': 'MainActivity', 'udid': 'device2', 'automationName': 'UiAutomator2', 'noReset': 'true' } driver2 = webdriver.Remote('http://localhost:4725/wd/hub', desired_caps2) ``` 以上代码中,通过指定不同的端口和设备UDID连接两个Appium Server,并创建两个WebDriver实例。 3. 运行测试用例。在Python脚本中编写测试用例,并使用两个WebDriver实例分别执行测试用例。例如: ``` # test case 1: device1 element1 = driver1.find_element_by_id('com.example.app:id/element1') element1.click() # test case 2: device2 element2 = driver2.find_element_by_id('com.example.app:id/element2') element2.click() ``` 以上代码中,分别使用两个WebDriver实例执行不同的测试用例。 通过以上步骤可以实现Python Appium多机运行。
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我去热饭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值