From APP Android end automation test beginner’s notes, write wrong place we a lot of advice oh
Start the Appium server
1. Start a single Appium server on the CLI
Appium -- Open the default4723Port number appium -p4723-- Use -p to start appium server with fixed port numberCopy the code
2. Start multiple Appium servers on the CLI
appium -p 4723
appium -p 4726
Copy the code
2. Start multiple devices
1. Set 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. 01.
Copy the code
Note:
- The mobile phone system version number is in the string format and needs to be quoted
- The url is the ADDRESS of the APpium server
- To start multiple devices, you need to start multiple Appium services, so no port number is 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 during actual operation
if __name__ == '__main__':
multi_app_start(devices_list[0].4723)
multi_app_start(devices_list[1].4725)
Copy the code
Note:
- Two Appium services must be enabled and the port numbers must be different
- 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 arbitrarily
- Ctime () indicates the current time
- In the preceding example, only after one device is successfully started will the other device be started
The final running result is:
Wrap the above code into a class:
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, in the actual run can be annotated
if __name__ == '__main__':
mas1 = MultiDevices()
mas2 = MultiDevices()
mas1.appium_desire(MultiDevices.devices_list[0].4723)
mas2.appium_desire(MultiDevices.devices_list[1].4725)
Copy the code
3. Multiple processes start the device concurrently
- In multiple processes, one 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 change the content in disorder
So I’m using multiple processes to start the device concurrently
Yaml file is the same as above, 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
The desired process group is built
desired_process = []
The desired process is loaded
for i in range(len(devices_list)):
port = 4723 + 2 * i
# target=" method called ",args=" parameter passed"
desired = multiprocessing.Process(target=multi_app_start, args=(devices_list[i], port))
desired_process.append(desired)
if __name__ == '__main__':
Start multiple devices to perform tests
for desired in desired_process:
desired.start()
Close after all processes are complete
for desired in desired_process:
desired.join()
Copy the code
The result is the same as above, but the time in the log output by the console is the same
Wrap the above code into a class
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
The desired process group is built
desired_process = []
The desired process is loaded
for i in range(len(devices_list)):
port = 4723 + 2 * i
# target=" method called ",args=" parameter passed"
desired = multiprocessing.Process(target=multi_devices_sync, args=(devices_list[i], port))
desired_process.append(desired)
if __name__ == '__main__':
multi_devices_sync = MultiDevicesSync()
Start multiple devices to perform tests
for desired in multi_devices_sync.desired_process:
desired.start()
Close after all processes are complete
for desired in multi_devices_sync.desired_process:
desired.join()
Copy the code
Supplement:
1. What is the difference between a process and a thread?
Process is a computer program about a data set on a running activity, the system is the basic unit of resource allocation and scheduling, is the basis of the operating system structure.
Threads, sometimes referred to as lightweight processes, are the smallest unit of a program’s execution flow. A thread is an entity in a process. A process can contain more than one thread, but a thread cannot contain more than one process. Threads do not own system resources, in a single program to run multiple threads at the same time to complete different work, called multithreading.
The difference between:
Data space allocation, child process and parent process have different code and data space, while multiple threads share data space, each thread has its own execution stack and program counter for its execution context.
You can think of a process as a factory, and multiple processes are multiple factories; Think of threads as an assembly line in a factory, where there can be more than one assembly line at a time.