程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python+appium automated test -appium concurrent test Python start appium service

編輯:Python

come from APP Android Notes for beginners of end-to-end automated testing , What's wrong? Please give me more advice

One 、 start-up appium The server

1. Start a single... From a command line window appium The server

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... From the command line window appium The server

appium -p 4723
appium -p 4726
Copy code

Two 、 Start multiple devices

1. stay yaml File configuration Capability Parameters

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 , Need to add ’' quotes
url by appium The address of the server
Starting multiple devices requires starting multiple devices appium service , 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
# Test functions , In the actual operation process, you can comment
if __name__ == '__main__':
multi_app_start(devices_list[0], 4723)
multi_app_start(devices_list[1], 4725)
Copy code

Be careful :

You need to turn on two appium service , And the port number cannot be the same
The connected equipment is mainly based on udid Connect , Not based on yaml In the document deviceName, So in yaml In the document deviceName It can be set at will
ctime() Indicates the current time
The above is that only after one is successfully started can another be started , Not start two devices synchronously

The final 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 functions , In actual operation, you can comment
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、 ... and 、 Multi process concurrent startup device

Multi process , The same variable , A copy of each exists in each process , They don't influence each other
In a multithreaded , All variables are shared by all threads , Any one can be modified by any thread , therefore , The biggest danger of sharing data among threads is that multiple threads change a variable at the same time , It's easy to mess up the content
So I use a multi process concurrent startup device

yaml Document ibid , 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
# structure desired Process group
desired_process = []
# load desired process
for i in range(len(devices_list)):
port = 4723 + 2 * i
# target=" Method called ",args=" Incoming parameter "
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()
# When all processes are finished, close
for desired in desired_process:
desired.join()
Copy code

The same as above , But start at the same time , The time in the log output from the console is consistent

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
# structure desired Process group
desired_process = []
# load desired process
for i in range(len(devices_list)):
port = 4723 + 2 * i
# target=" Method called ",args=" Incoming 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 multiple devices to perform tests
for desired in multi_devices_sync.desired_process:
desired.start()
# When all processes are finished, close
for desired in multi_devices_sync.desired_process:
desired.join()
Copy code

Add :

1. Difference between process and thread ?

A process is a running activity of a program on a data set in a computer , Is the system resource allocation and scheduling of the basic unit , Is the foundation of the operating system architecture .

Threads are sometimes referred to as lightweight processes , Is 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 . Thread does not own system resources , Running multiple threads at the same time in a single program to complete different work , Become multithreaded .

difference :

Allocation of data space , Child processes and parent processes have different code and data spaces , 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 , Multiple processes are multiple factories ; Think of a thread as a pipeline in a factory , A factory can have multiple production lines at the same time .

The following is the supporting information , For doing 【 software test 】 For our friends, it should be the most comprehensive and complete war preparation warehouse , This warehouse also accompanied me through the most difficult journey , I hope it can help you !


These materials , For doing 【 software test 】 It should be the most comprehensive and complete war preparation warehouse for those who want to advance , This warehouse also accompanied me through the most difficult journey , I hope it can help you ! Everything should be done as soon as possible , Especially in the technology industry , We must improve our technical skills . I hope that's helpful …….


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved