Android-多指截图压力测试

2021-08-30  本文已影响0人  崔某
# -*- coding: utf-8 -*-
# @Time    : 2021/7/22 18:01
# @File    : ThreeScreenShot.py
"""
    三指截图-压力测试
"""

from time import sleep
from appium.webdriver.common.multi_action import MultiAction
from appium.webdriver.common.touch_action import TouchAction
from appium import webdriver
from selenium.common.exceptions import TimeoutException, NoSuchElementException
from selenium.webdriver.support.wait import WebDriverWait


class ThreeScreenShot(object):

    def __init__(self, android_version, package_name, activity_name):
        """
        :param android_version: 安卓版本
        :param package_name:    启动包名
        :param activity_name:   启动Activity名
        """
        desired_caps = {
            'platformName': 'Android',  # 平台名称
            'deviceName': 'test',  # 设备名称(任写)
            'platformVersion': android_version,  # 安卓版本
            'appPackage': package_name,  # 启动包名
            'appActivity': activity_name,  # 启动 Acclivity
            'noReset': True,  # 重置(不会保留之前的启动数据)
            'newCommandTimeout': 60000  # 超时时间(一分钟)
        }
        if android_version == "11":
            # Android 11 添加此参数
            desired_caps["automationName"] = "uiautomator2"
        # 开始初始化
        print("Appium 正在初始化...")
        # 启动服务/通过4723端口来建立一个会话
        self.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)
        # 隐式等待15s
        self.driver.implicitly_wait(15)
        # 获取屏幕宽度
        self.window_width = self.driver.get_window_size()['width']
        # 获取屏幕高度
        self.window_height = self.driver.get_window_size()['height']
        print("Appium 初始化完成...")

    # 等待元素
    def wait_element(self, way, value, timeout=10, poll_frequency=0.5):
        """
        :param way:         定位方式
        :param value:       值
        :param timeout:     超时时长(s)
        :param poll_frequency: 刷新频率(s)
        :return:
        """
        try:
            if way == "text":
                WebDriverWait(self.driver, timeout=timeout, poll_frequency=poll_frequency).until(
                    lambda x: x.find_element_by_android_uiautomator('new UiSelector().text("%s")' % value),
                    message=f'【text:{value}】等待超时')
            elif way == "id":
                WebDriverWait(self.driver, timeout=timeout, poll_frequency=poll_frequency).until(
                    lambda x: x.find_element_by_id(value), message=f'【id:{value}】等待超时')
            elif way == "desc":
                WebDriverWait(self.driver, timeout=timeout, poll_frequency=poll_frequency).until(
                    lambda x: x.find_element_by_accessibility_id(value), message=f'【desc:{value}】等待超时')
            # xpath参数示例://*[@text="xxx"]
            elif way == "xpath":
                WebDriverWait(self.driver, timeout=timeout, poll_frequency=poll_frequency).until(
                    lambda x: x.find_element_by_xpath(value), message=f'【xpath:{value}】等待超时')
            else:
                raise TypeError(f"无此定位元素方式:{way},定位方式支持:text/id/desc/xpath")
        except TimeoutException:
            raise

        # 查找元素

    def find_element(self, way, value):
        """
        :param way:     定位方式
        :param value:   值
        :return:    返回元素
        """
        try:
            if way == "text":
                # return self.driver.find_element_by_name(value)   已经凉了
                return self.driver.find_element_by_android_uiautomator('new UiSelector().text("%s")' % value)
            elif way == "id":
                return self.driver.find_element_by_id(value)
            elif way == "desc":
                return self.driver.find_element_by_accessibility_id(value)
            # xpath参数示例://*[@text="xxx"]
            elif way == "xpath":
                return self.driver.find_element_by_xpath(value)
            else:
                raise TypeError(f"无此定位元素方式:{way},定位方式支持:text/id/desc/xpath")
        except NoSuchElementException:
            raise

    def scr(self):
        action1 = TouchAction(self.driver)  # 第一个手势
        action2 = TouchAction(self.driver)  # 第二个手势
        action3 = TouchAction(self.driver)  # 第三个手势
        zoom_action = MultiAction(self.driver)
        action1.press(x=self.window_width/4, y=self.window_height/5).move_to(x=self.window_width/4, y=self.window_height*3/5).wait(200).release()
        action2.press(x=self.window_width*2/4, y=self.window_height/5).move_to(x=self.window_width*2/4, y=self.window_height*3/5).wait(200).release()
        action3.press(x=self.window_width*3/4, y=self.window_height/5).move_to(x=self.window_width*3/4  , y=self.window_height*3/5).wait(200).release()
        zoom_action.add(action1, action2, action3)  # 加载
        zoom_action.perform()  # 执行
        sleep(1)


if __name__ == '__main__':
    threeScreenShot = ThreeScreenShot("11", "com.android.settings", "com.android.settings.Settings")
    driver = threeScreenShot.driver
    driver.keyevent(3)
    for i in range(10):
        threeScreenShot.scr()
        try:
            # 由于无法截取图片的缩略图,这里采用坐标点
            driver.tap([(750, 100)])
            threeScreenShot.wait_element("text", "保存")
            threeScreenShot.find_element("text", "保存").click()
            print(f"第{1+i}次截屏成功!")
        except TimeoutException:
            print(f"第{1+i}次截屏失败!")
            driver.keyevent(3)
    threeScreenShot.wait_element("text", "电话")
    threeScreenShot.driver.quit()

上一篇 下一篇

猜你喜欢

热点阅读