Method of Python3+Appium to Realize Operation of Multiple Mobile Devices

  • 2021-07-10 20:12:10
  • OfStack

Requirements:

Connect the local (two Android phones) or the local installation (two Android simulators) to install the local apk package at the same time.

demon.py

Special note: udid must be written to realize the operation of more than two mobile devices at the same time. After modifying the value in deviceName, it is impossible to distinguish mobile devices. If only value of deviceName is modified, it can only be installed on the same device.

The following code is only a basic introduction, which needs to be done according to your own needs when extended to the framework;


import os
import threading
import multiprocessing
from appium import webdriver
class ConcurrentExecution:
   """
    Multi-process concurrent installation local 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):
     """
         Clean up appium Environment , Kill node.exe The process of 
      :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):
     """
       Start appium Server 
      :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): # Startup service 
      th = threading.Thread(target=obj.start_appium_server,args=(j,))
            th.start()

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

Related articles: