在app自动化的过程中,可能会需要通过自动化来做一些手机的兼容性测试,但是需要同时在多个手机启动app
环境搭建
1、模拟器选择的是雷电模拟器多开器。
2、两台设备都安装淘宝
3、安装appium命令行
配置手机信息
需要把手机信息,就是通常我们启动手机的desired_caps配置信息。这里通过yaml的方式进行配置
- devices: emulator-5554 port: 4730 desired_caps: platformName: Android deviceName: emulator-5554 appPackage: com.taobao.taobao noReset: !!bool True appActivity: com.ali.user.mobile.login.ui.UserLoginActivity - devices: emulator-5556 port: 4740 desired_caps: platformName: Android deviceName: emulator-5556 appPackage: com.taobao.taobao noReset: !!bool True appActivity: com.ali.user.mobile.login.ui.UserLoginActivity automationName: uiautomator2
多线程启动appium
这里和前面写的多线程进行启动,其实类型的,只是单独的把启动appium封装起来,其中也包括了一些读取设备信息,以及获取设备的端口号和设备信息
# coding:utf-8 # coding:utf-8 import time from common.dos_cmd import Dos_adb from common.read_yaml import GetYaml import os from appium import webdriver import threading path =os.path.dirname(os.getcwd()) app = os.path.join(os.path.join(path,‘config‘),‘app.yaml‘) class Server(): def __init__(self): self.adb = Dos_adb() self.yaml = GetYaml(app) def get_driver(self): ‘‘‘获取设备列表‘‘‘ devices_list = self.adb.get_device() return devices_list def get_dos(self): ‘‘‘命令行启动appium‘‘‘ result = self.yaml.get_data() dos_list = [] android_list = [] for j in result: port = j[‘port‘] devices = j[‘desired_caps‘][‘deviceName‘] android_list.append(j[‘desired_caps‘]) for i in range(1): a = ‘appium -p %s -U %s‘ %(port,devices) dos_list.append(a) return dos_list def get_commad(self): ‘‘‘执行appium启动‘‘‘ self.dos_list = self.get_dos() for i in self.dos_list: self.adb.get_adb(i) def get_android(self,devicesName): ‘‘‘获取port和设备信息‘‘‘ desired_caps = self.yaml.get_data() for i in desired_caps: if devicesName in i[‘devices‘]: port = i[‘port‘] desired_cap = i[‘desired_caps‘] return (port,desired_cap) def main(self): ‘‘‘多线程启动‘‘‘ thread_list = [] for i in range(2): appium_server = threading.Thread(target=self.get_commad) thread_list.append(appium_server) for j in thread_list: j.start() time.sleep(5) def android_start(self,deviceName): ‘‘‘启动webdriver‘‘‘ android = self.get_android(deviceName) driver = webdriver.Remote(‘http://127.0.0.1:%s/wd/hub‘ % android[0], android[1]) return driver if __name__ == ‘__main__‘: xx = Server() devices = xx.get_driver() print(devices) for i in devices: xx.main() time.sleep(5) xx.android_start(i)
编写测试用例
用例还是用的以前的那个用例,然后直接进行通过设备的方式进行启动appium
# coding:utf-8 from pages.login_page import LoginPage import time import os from common.aa import Server path =os.path.dirname(os.getcwd()) # 获取到yaml文件路径 yaml_path = os.path.join(os.path.join(path,‘config‘),‘element.yaml‘) class BaseDriver: def login(self,driver): page =LoginPage(driver) time.sleep(8) # 获取用户名元素以及输入内容 use = page.get_username_element(yaml_path) use.send_keys(‘1111‘) # 获取密码元素以及输入内容 pas = page.get_password_element(yaml_path) pas.send_keys(‘22222‘) # 进行点击登录 button = page.get_login_button_element(yaml_path) button.click() if __name__ == ‘__main__‘: x = BaseDriver() server = Server() server.main() drivers = server.get_driver() for i in drivers: driver = server.android_start(i) x.login(driver)
这里虽然用到了多线程,但是还是上一个用例结束,才会去执行下一台机器。
注:这里代码只上传了部分代码,完整代码可以去前面定位封装中进行查看。