python+appium automated testing - start appium service in python for appium concurrent testing

Keywords: Interview unit testing software testing

Notes from beginners of APP Android automatic test. Please give us more advice on what is wrong

1, Start appium server

1. Start a single appium server through the command line window

appium  --  Directly open the default 4723 port number
appium -p 4723  --  use-p To start the fixed port number appium The server
 Copy code

2. Start multiple appium servers through the command line window

appium -p 4723
appium -p 4726
 Copy code

2, Start multiple devices

1. Configure the Capability parameter in the yaml file

desired_caps.yaml

platformName: Android
platformVersion: '9'
deviceName: U4AIUKFAL7W4MJLR
appPackage: com.sina.weibo
appActivity: com.sina.weibo.SplashActivity
automationName: UiAutomator2
autoGrantPermissions: True
noReset: True
url: 127.0.0.1
 Copy code

be careful:

  • The mobile phone system version number is in string format and needs quotation marks
  • url is the address of appium server
  • Starting multiple devices requires starting multiple appium services, so the port number is not set here

2. Code implementation

from time import ctime
import yaml
from appium import webdriver

devices_list = ['U4AIUKFAL7W4MJLR', 'U4AIUKFAL7W4MHUHUDS']
with open(r"E:\\study\\Fork\\WeiboDemo\\Weibo\\config\\desired_caps.yaml", 'r') as file:
    data = yaml.load(file, Loader=yaml.FullLoader)

def multi_app_start(udid, port):
    desired_caps = {'platformName': data['platformName'],
                    'platformVersion': data['platformVersion'],
                    'deviceName': data['deviceName'],
                    'udid': udid,
                    'appPackage': data['appPackage'],
                    'appActivity': data['appActivity'],
                    'automationName': data['automationName'],
                    'autoGrantPermissions': data['autoGrantPermissions'],
                    'noReset': data['noReset']
                    }
    print('appium port:%s start run %s at %s' % (port, udid, ctime()))

    driver = webdriver.Remote('http://' + str(data['url']) + ':' + str(port) + '/wd/hub', desired_caps)
    driver.implicitly_wait(10)
    return driver

# The test function can be annotated in the actual running process
if __name__ == '__main__':
    multi_app_start(devices_list[0], 4723)
    multi_app_start(devices_list[1], 4725)
Copy code

be careful:

  • Two appium services need to be started, and the port numbers cannot be the same
  • The connected devices are mainly connected according to the udid, not according to the deviceName in the yaml file, so the deviceName in the yaml file can be set at will
  • ctime() indicates the current time
  • The above is that one device can only be started after the other is successfully started, not two devices can be started synchronously

The final running result is:

Encapsulate the above code into classes:

class MultiDevices:
    driver: webdriver = None
    devices_list = ['U4AIUKFAL7W4MJLR', 'U4AIUKFAL7W4MHUHUDS']

    def appium_desire(self, udid, port):
        with open(r"E:\study\Fork\WeiboDemo\Weibo\config\desired_caps.yaml", 'r') as file:
            data = yaml.load(file, Loader=yaml.FullLoader)

        desired_caps = {'platformName': data['platformName'],
                        'platformVersion': data['platformVersion'],
                        'deviceName': data['deviceName'],
                        'udid': udid,
                        'appPackage': data['appPackage'],
                        'appActivity': data['appActivity'],
                        'automationName': data['automationName'],
                        'autoGrantPermissions': data['autoGrantPermissions'],
                        'noReset': data['noReset']
                        }
        print('appium port:%s start run %s at %s' % (port, udid, ctime()))

        self.driver = webdriver.Remote('http://' + str(data['url']) + ':' + str(port) + '/wd/hub', desired_caps)
        self.driver.implicitly_wait(10)
        return self.driver

# Test function, which can be annotated in actual operation
if __name__ == '__main__':
    mas1 = MultiDevices()
    mas2 = MultiDevices()
    mas1.appium_desire(MultiDevices.devices_list[0], 4723)
    mas2.appium_desire(MultiDevices.devices_list[1], 4725)
Copy code

3, Multi process concurrent startup device

  • In multiple processes, a copy of the same variable exists in each process and does not affect each other
  • In multithreading, all variables are shared by all threads, and any one can be modified by any thread. Therefore, the biggest danger of sharing data between threads is that multiple threads change a variable at the same time, which is easy to confuse the content

So I use a multi process concurrent startup device

The yaml file is the same as above, and the code implementation is as follows:

import multiprocessing
from time import ctime
import yaml
from appium import webdriver

devices_list = ['U4AIUKFAL7W4MJLR', 'U4AIUKFAL7W4MHUHUDS']
with open(r"E:\\study\\Fork\\WeiboDemo\\Weibo\\config\\desired_caps.yaml", 'r') as file:
    data = yaml.load(file, Loader=yaml.FullLoader)

def multi_app_start(udid, port):
    desired_caps = {'platformName': data['platformName'],
                    'platformVersion': data['platformVersion'],
                    'deviceName': data['deviceName'],
                    'udid': udid,
                    'appPackage': data['appPackage'],
                    'appActivity': data['appActivity'],
                    'automationName': data['automationName'],
                    'autoGrantPermissions': data['autoGrantPermissions'],
                    'noReset': data['noReset']
                    }
    print('appium port:%s start run %s at %s' % (port, udid, ctime()))

    driver = webdriver.Remote('http://' + str(data['url']) + ':' + str(port) + '/wd/hub', desired_caps)
    driver.implicitly_wait(10)
    return driver

# Build desired process group
desired_process = []

# Load desired process
for i in range(len(devices_list)):
    port = 4723 + 2 * i
    # target = "called method", args = "passed in parameter"
    desired = multiprocessing.Process(target=multi_app_start, args=(devices_list[i], port))
    desired_process.append(desired)

if __name__ == '__main__':
    # Start multi device execution test
    for desired in desired_process:
        desired.start()
    # Close when all processes are finished
    for desired in desired_process:
        desired.join()
Copy code

The result is the same as above, but it is started at the same time, and the time in the log output from the console is the same

Encapsulate the above code into classes

class MultiDevicesSync:
    driver: webdriver = None
    devices_list = ['U4AIUKFAL7W4MJLR', 'U4AIUKFAL7W4MHUHUDS']

    def multi_devices_sync(udid, port):
        with open(r"E:\study\Fork\WeiboDemo\Weibo\config\desired_caps.yaml", 'r') as file:
            data = yaml.load(file, Loader=yaml.FullLoader)

        desired_caps = {'platformName': data['platformName'],
                        'platformVersion': data['platformVersion'],
                        'deviceName': data['deviceName'],
                        'udid': udid,
                        'appPackage': data['appPackage'],
                        'appActivity': data['appActivity'],
                        'automationName': data['automationName'],
                        'autoGrantPermissions': data['autoGrantPermissions'],
                        'noReset': data['noReset']
                        }
        print('appium port:%s start run %s at %s' % (port, udid, ctime()))

        driver = webdriver.Remote('http://' + str(data['url']) + ':' + str(port) + '/wd/hub', desired_caps)
        driver.implicitly_wait(10)
        return driver

    # Build desired process group
    desired_process = []

    # Load desired process
    for i in range(len(devices_list)):
        port = 4723 + 2 * i
        # target = "called method", args = "passed in parameter"
        desired = multiprocessing.Process(target=multi_devices_sync, args=(devices_list[i], port))
        desired_process.append(desired)

if __name__ == '__main__':
    multi_devices_sync = MultiDevicesSync()
    # Start multi device execution test
    for desired in multi_devices_sync.desired_process:
        desired.start()
    # Close when all processes are finished
    for desired in multi_devices_sync.desired_process:
        desired.join()
Copy code

Supplement:

1. What is the difference between process and thread?

Process is a running activity of a computer program on a data set. It is the basic unit of resource allocation and scheduling of the system and the basis of the structure of the operating system.

Threads, sometimes called lightweight processes, are the smallest unit of program execution flow. A thread is an entity in a process. A process can contain multiple threads, but a thread cannot contain multiple processes. Threads do not own system resources. They run multiple threads at the same time in a single program to complete different work and become multithreaded.

difference:

For the allocation of data space, the child process and parent process have different code and data space, while multiple threads share the data space. Each thread has its own execution stack and program counter as its execution context.

A process can be regarded as a factory, and multiple processes are multiple factories; Thread is regarded as a pipeline in a factory. There can be multiple pipelines in a factory at the same time.

The following is the supporting information. For the friends doing [software testing], it should be the most comprehensive and complete war preparation warehouse. This warehouse has also accompanied me through the most difficult journey. I hope it can also help you!

Finally, it can be in the official account: programmer Hao.  ! Get a 216 page interview document of Software Test Engineer for free. And the corresponding video learning tutorials for free!, It includes basic knowledge, Linux essentials, Shell, Internet program principles, Mysql database, special topics of packet capture tools, interface test tools, test advanced Python programming, Web automation test, APP automation test, interface automation test, advanced continuous test integration, test architecture, development test framework, performance test, security test, etc.

If my blog is helpful to you and you like my blog content, please click "like", "comment" and "collect" for three times! Friends who like software testing can join our testing technology exchange group: 310357728 (there are various software testing resources and technical discussions)

 

Posted by jorgep on Fri, 19 Nov 2021 22:47:33 -0800