Appium selenium grid 多进程 (同步,异步) 启动多个 appium

测试小书童 · 2016年11月14日 · 最后由 lzc-3232 回复于 2018年03月28日 · 1875 次阅读

首先可以看看上篇帖子基于 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 开源框架

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
共收到 5 条回复 时间 点赞
测试小书童 [該主題已被刪除] 中提及了此贴 11月14日 12:51

python+appium 开源框架还没有玩起来。。。

最多支持几台手机,有尝试过么

#3 楼 @mads 这个没有限制,看电脑配置。。我这破电脑同时启动 3 个,就卡了😂

#4 楼 @lose 你这个还是要开 grid 的吧

您好 我这边使用了一下您提供的代码,但是只能启动一个,另一个就会报 A new session could not be created 。麻烦楼主可知道原因?

需要 登录 後方可回應,如果你還沒有帳號按這裡 注册