使用Appium自动化测试app(五)------实战篇3(线程)

在很多时候,我们需要对多台设备进行操控测试!

import os
import unittest
import threading
import re
from time import sleep
from appium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


def start_server(serial, port):
    cmd = 'appium --address 127.0.0.1 --port ' + str(port) + ' --bootstrap-port ' + str(port-2000) + ' -U ' + serial + ' --session-override'
    os.system(cmd)


class Test(object):
    def setup(self, serial, newPort, systemPort):
        desired_caps = {
            'platformName': 'Android',
            'deviceName': 'emulator-5556',
            'appPackage': 'com.facebook.katana',
            'appActivity': '.LoginActivity',
            'autoAcceptAlerts': 'True',
            'noReset': 'True',
            # 连接物理设备的唯一标志符
            'udid': serial,
            'systemPort': systemPort,
        }

        self.driver = webdriver.Remote('http://127.0.0.1:' + str(newPort) + '/wd/hub', desired_caps)
        self.test_start()
        self.tear_down()

    def tear_down(self):
        self.driver.quit()

    def test_start(self):
        sleep(8)
        x = self.driver.get_window_size()['width']
        y = self.driver.get_window_size()['height']
        wait = WebDriverWait(self.driver, 30)
        while True:
            if 'Like button. Double tap and hold to react.' in self.driver.page_source:
                sleep(2)
                wait.until(EC.presence_of_element_located((By.ID, 'Like button. Double tap and hold to react.'))).click()
                sleep(2)
            self.driver.swipe(x * 0.5, y * 0.75, x * 0.5, y * 0.25, 500)
            sleep(2)


if __name__ == '__main__':
    # server线程池
    server_list = []
    # driver线程池
    driver_list = []
    # 初始化默认端口
    port = 4723
    a = 0
    info_out = os.popen('adb devices')
    for info in info_out.readlines():
        if 'List of devices' in info or 'adb' in info or 'daemon' in info or 'offline' in info or 'unauthorized' in info or len(info) < 5:
            pass
        else:
            newPort = port + a
            systemPort = port + a + 4000
            test = Test()
            a = a + 1
            serial_list = re.findall(r'(.*?)	device', info)
            server_list.append(threading.Thread(target=start_server, args=(serial_list[0], newPort)))
            driver_list.append(threading.Thread(target=test.setup, args=(serial_list[0], newPort, systemPort)))
    for server in server_list:
        server.start()
    sleep(30)
    for driver in driver_list:
        driver.start()

在这里需要着重介绍一下这几个参数:

appium -a 127.0.0.1 -p xxxx -bp xxxx --chromedriver-port xxxx -U xxxx --session-override

-a:是指定监听的ip,也可以写成–address,后面127.0.0.1可以改为你需要的ip地址
-p:是指定监听的端口,也可以写成–port,也可以修改为你需要的端口
-bq:是连接Android设备bootstrap的端口号,默认是4724,也可写成–bootstrap-port
–chromedriver-port:是chromedriver运行需要指定的端口号,默认是9515
-U:是连接的设备名称,就是adb devices获取的设备标识

你可能感兴趣的:(Appium)