首先可以看看上篇帖子基于 selenium grid 连接多个 appium
介绍
- python 基于unittest 参数化
- 多进程(同步,异步)启动多个appium的解决方案
使用
基于上篇帖子,唯一不同的地方是命名运行方式,请加上bp 参数
node D:\\app\Appium\\node_modules\\appium\\bin\\appium.js -p 4723 -bp 4733 -U DU2TAN15AJ049163 --nodeconfig D:\app\appium_study\testRunner\test.json
源代码分析
- 基类testBase unittest 参数化
- 注意param参数,就是常用的webdriver的设置参数
from appium import webdriver
import unittest
import os
PATH = lambda p: os.path.abspath(
os.path.join(os.path.dirname(__file__), p)
)
class ParametrizedTestCase(unittest.TestCase):
def __init__(self, methodName='runTest', param=None):
super(ParametrizedTestCase, self).__init__(methodName)
self.param = param[0]
def setUp(self):
desired_caps = {}
desired_caps['platformName'] = "ANDROID"
desired_caps['platformVersion'] = self.param["platformVersion"]
desired_caps['deviceName'] = self.param["deviceName"]
desired_caps['udid'] = self.param["deviceName"]
desired_caps['appPackage'] = "cn.ibona.t1_beta"
desired_caps['appActivity'] = "cn.ibona.t1.main.ui.activity.SplashActivity"
remote = "http://192.168.1.218:" + "4444" + "/wd/hub" # 这里填 hub的地址
# desired_caps['app'] = PATH('../img/t.apk')
print(self.param["deviceName"])
# time.sleep(5)
desired_caps["unicodeKeyboard"] = "True"
desired_caps["resetKeyboard"] = "True"
self.driver = webdriver.Remote(remote, desired_caps)
def tearDown(self):
self.driver.close_app()
self.driver.quit()
@staticmethod
def parametrize(testcase_klass, param=None):
""" Create a suite containing all tests taken from the given
subclass, passing them the parameter 'param'.
"""
testloader = unittest.TestLoader()
testnames = testloader.getTestCaseNames(testcase_klass)
suite = unittest.TestSuite()
for name in testnames:
suite.addTest(testcase_klass(name, param=param))
return suite
- 看看实际用例
from testRunner.testBase import ParametrizedTestCase as pt
import time
class testHome(pt):
def test_home(self):
time.sleep(5)
self.driver.find_element_by_id("cn.ibona.t1_beta:id/passwordEditText").send_keys("111111")
time.sleep(5)
self.driver.find_element_by_id("cn.ibona.t1_beta:id/phoneNumberEditText").send_keys("136111111")
- 看看代码入口
import sys
sys.path.append("..")
import unittest
from multiprocessing import Pool
from testRunner.testBase import ParametrizedTestCase
from testRunner.test3 import testHome
t = [[{'deviceName': 'DU2TAN15AJ049163', 'platformVersion': '4.4.2'}],
[{'deviceName': 'MSM8926', 'platformVersion': '4.3'}]]
def f():
pool = Pool(2)
# for i in range(2):
# pool.apply_async(sample_request, args=(t[i],)) # 异步
pool.map(sample_request, t) # 并行
pool.close()
pool.join()
def sample_request(devices):
suite = unittest.TestSuite()
suite.addTest(ParametrizedTestCase.parametrize(testHome, param=devices))
# suite.addTest(ParametrizedTestCase.parametrize(testHome, param=devices)
# suite.addTest(TestInterfaceCase.parametrize(testContact))
unittest.TextTestRunner(verbosity=2).run(suite)
if __name__ == '__main__':
f()
运行情况
后续
对接到我的python+appium 开源框架