Python实现去除APP弹窗

倖福魔咒の 提交于 2020-01-17 01:36:16

在进行APP测试的时候,首次启动或者重置应用后启动,均会有一些权限弹窗出现。手动消除在自动化测试的时候就显得有点不入流了。
因此封装一个方法来进行弹窗的自动处理。

Appium继承于selenium,只是在其添加了APP独有的一些方法和功能,实际上对于各种事件的处理还是来源于selenium中的方法。
import os
from appium import webdriver
from selenium.webdriver.support.ui import WebDriverWait

class Handle:

    PLATFORM = 'Android'
    DRIVER_SERVER = 'http://localhost:4723/wd/hub'
    // 查看机型
    deviceName = os.popen('adb shell getprop ro.product.model').read()
    // 查看版本
    platformVersion = os.popen('adb shell getprop ro.build.version.release').read()

    def __init__(self, APP_PACKAGE, APP_ACTIVITY):
        self.desired_caps = {
            'platformName': self.PLATFORM,
            "platformVersion": self.platformVersion
            'deviceName': self.deviceName,
            'appPackage': APP_PACKAGE,
            'appActivity': APP_ACTIVITY,
            'noReset': True,
            'newCommandTimeout': 1000,
        }
        // 初始化appium服务器,并打开app以及对应的界面
        self.driver = webdriver.Remote(self.DRIVER_SERVER, self.desired_caps)
        // 等待10s,等待app响应,app中的操作,建议都加一点时间等待
        self.wait = WebDriverWait(self.driver, 10)

    def handle_windows(self):
            """处理弹窗"""
            try:
                // 返回当前页面所有标签为classname的元素列表
                elements = self.driver.find_elements_by_class_name('android.widget.Button')
                while True:
                    for element in elements:
                        if element.text == '允许':
                            self.driver.find_element_by_android_uiautomator('new UiSelector().text("允许")').click()
                        elif element.text == '始终允许':
                            self.driver.find_element_by_android_uiautomator('new UiSelector().text("始终允许")').click()
                        elif element.text == '确定':
                            self.driver.find_element_by_android_uiautomator('new UiSelector().text("确定")').click()
            except:
                print('未检测到弹窗')
                
AppWindow = HandleWindows('com.xxzb.fenwoo', 'com.xxzb.fenwoo.activity.addition.WelcomeActivity')

if __name__ == '__main__':
	AppWindow.handle_windows()  
AUTO传入的数据也可以来自于文件或者其他格式的数据,可以批量处理多个app的弹框处理。
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!