Python3+Appium实现多台移动设备操作的方法

yipeiwu_com6年前Python基础

需求:

连接本机的(两台安卓手机)或者本机安装的(安卓模拟器两个),实现同时安装本地apk包 。

demon.py

特别说明:必须写udid才能实现同时对两台以上的移动设备操作。对deviceName中的值进行修改后是无法区分移动设备的,如果只修改deviceName的value就只能在同一台设备上进行安装了。

下面代码只是基础介绍,扩展到框架中需要根据自己需求做;

import os
import threading
import multiprocessing
from appium import webdriver
class ConcurrentExecution:
   """
   多进程并发安装本地apk
   """
     def __init__(self):
        self.driver_port = [[4700,"127.0.0.1:21503"],[4701,"127.0.0.1:21513"]]


  def android_driver(self,i):
     driver_list = []
     capabilities = {
        "platformName" : "Android",
        "udid"          : self.driver_port[i][1],
        "deviceName"   : self.driver_port[i][1],
        "app"          : "E:\\appiumautocode\\xxxoooox.apk",
        "noReset"       : "True"
         }
     driver = webdriver.Remote("http://127.0.0.1:{0}/wd/hub".format(self.driver_port[i][0]),capabilities)
     driver_list.append(driver)
     return driver_list


   def kill_server(self):
     """
      清理appium环境,杀node.exe的进程
      :return:
      """
     server_list = os.popen('tasklist | find "node.exe"').readlines()
     print(server_list)
     if len(server_list)>0:
      os.system("taskkill -F -PID node.exe")


   def start_appium_server(self,j):
     """
      启动appium服务器
      :return:
     """
     li_port = [4700,4701]
     os.system("appium -p {0}".format(li_port[j]))


if __name__ == '__main__':

   obj = ConcurrentExecution()
   obj.kill_server()
  
   for j in range(2): #启动服务
      th = threading.Thread(target=obj.start_appium_server,args=(j,))
      th.start()

   for i in range(2): #运行
      t = multiprocessing.Process(target=obj.android_driver,args=(i,))
      t.start()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python 调用win32pai 操作cmd的方法

实例如下: #coding=utf-8 import subprocess from time import * import win32api import win32con im...

Python 中导入csv数据的三种方法

Python 中导入csv数据的三种方法,具体内容如下所示: 1、通过标准的Python库导入CSV文件: Python提供了一个标准的类库CSV文件。这个类库中的reader()函数用...

Python通过select实现异步IO的方法

本文实例讲述了Python通过select实现异步IO的方法。分享给大家供大家参考。具体如下: 在Python中使用select与poll比起在C中使用简单得多。select函数的参数是...

Python寻找路径和查找文件路径的示例

Sys.path 指定用于模块搜索路径的字符串列表 也可以通过sys模块的append方法在Python环境中增加搜索路径。 Sys.path.append(‘/usr/bin/') /...

Python遍历zip文件输出名称时出现乱码问题的解决方法

本文实例讲述了Python遍历zip文件输出名称时出现乱码问题的解决方法。分享给大家供大家参考。具体如下: windows中使用python2.7遍历zip文件之后输出文件名等信息,co...