• 【App自动化测试】(十五)手机浏览器(webview)自动化测试



    1. 手机浏览器自动化前提

    五大前提

    1. 安装chromedriver
    2. 安装对chromedriver版本
    3. 配置capability
    4. 设置chromedriver相关配置
    5. 使用浏览器的inspect工具远程调试

    1.1 安装chromedriver

    帖子:chromedriver下载地址与webview自动化关键代码

    在以下文章中有说明,可以用于参考:
    【App自动化测试】(十四)Android WebView测试方法中的 2.3.1 前提一:chromedriver安装

    1.2 安装对chromedriver版本

    在以下文章中有说明,可以用于参考:
    【App自动化测试】(十四)Android WebView测试方法中的 2.3.2 前提二:chromedriver版本选择正确

    1.3 配置capability

    需要配置的capability参数

    1. browserName:chrome(新版本)/ browser(老版本)

    注意:还可使用标准的appPackageappActivity,写明浏览器的包名和activity的名字。但是使用browserName是最简单的

    1. chromedriverExecutableDir:指定 chromedriver 可执行文件集合的目录

    chromedriverExecutableDir的示例图:可使用不同名字的后缀进行chromedriver的版本区分
    在这里插入图片描述

    1. showChromedriverLog:让appium 日志展示 chromedriver 的日志方便排查

    1.4 设置chromedriver相关配置

    appium chromedriver 自动发现机制中常用的appium capability参数参数:

    • chromedriverExecutableDir: 指定 chromedriver 可执行文件集合的目录
      • 将可用的chromedriver放在此目录下,当appium发现此配置项后,会去目录中寻找合适的chromedriver。
    • chromedriverChromeMappingFile: 允许显式指定版本对应关系
      • 强行指定一个版本的chromedriver去测特定的webview版本
    • showChromedriverLog: 让appium 日志展示 chromedriver 的日志方便排查

    appium capability参数设置 ——python:

    def setup(self):
            desired_caps = {
            "platformName":"Android",
            "deviceName":"emulator-5554",
            "appPackage": "com.example.android.apis",
            "appActivity":".ApiDemos",
            "chromedriverExecutableDir":"E:\chromedriver_webview_test",
            "chromedriverChromeMappingFile":"E:\chromedriver_webview_test\mapping.json",
            "showChromedriverLog":True
            }
            self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
            # 使用隐式等待
            self.driver.implicitly_wait(10)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    1.5 使用浏览器的inspect工具远程调试

    说明:

    练习的时候使用了三款模拟器:Avd Android6.0、Avd Android7.0、MUMU.
    Avd Android6.0、Avd Android7.0在使用edge inspect远程调试的时候,出现解析混乱,dom无法正常加载的情况。不知道是不是因为webview版本较低导致的。MUMU模拟器则正常。

    2. 手机浏览器测试代码python版本

    """
    模拟器:MUMU
    浏览器:MUMU自带浏览器
    备注:
    使用脚本访问百度并进行搜索的话会导致无法打开网址;但手动访问并搜索正常
    相关问题解释帖子:https://ceshiren.com/t/topic/16195/4
    """
    
    from appium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions
    from selenium.webdriver.support.wait import WebDriverWait
    
    
    class TestBrowser:
        def setup_class(self):
            desired_caps = {}
            desired_caps['platformName'] = 'Android'
            desired_caps['platformVersion'] = '6.0'
            desired_caps['deviceName'] = '127.0.0.1:7555'
            #python appium client 2.x 会默认使用w3c,因此此处使用chromeOptions参数吧w3c
            desired_caps['chromeOptions'] = {'w3c': False}
      
    	#方式一:
    	desired_caps['browserName']= 'browser'
    
    	#方式二:
            #还可使用标准的`appPackage`和`appActivity`,写明浏览器的包名和activity的名字。
            #desired_caps['appPackage'] = 'com.android.browser'
            #desired_caps['appActivity'] = 'com.android.browser.BrowserActivity'
    
            #设置chromedriver所在路径
            desired_caps['chromedriverExecutableDir']='E:\\chromedriver_webview_test'
            #开启chromedriverlog
            desired_caps['showChromedriverLog']=True
            desired_caps['noReset'] = 'true'
            self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
            # 使用隐式等待
            self.driver.implicitly_wait(10)
    
        def teardown(self):
            self.driver.quit()
    
        def test_browser(self):
            self.driver.get('https://www.sogou.com/')
      
            #使用方式二`appPackage`和`appActivity`话,需要切换上下文,要不然会报错
            #print(self.driver.contexts)
            #WebDriverWait(self.driver, 5).until(lambda driver: len(self.driver.contexts) > 1)
            #self.driver.switch_to.context('WEBVIEW_com.android.browser')
    
            self.driver.find_element(By.ID,'keyword').click()
            self.driver.find_element(By.ID,'keyword').send_keys("webview")
            self.driver.find_element(By.CLASS_NAME,'qbtn').click()
            assert_text=self.driver.find_element(By.XPATH,'//*[@class="vrResult"]//a/em').text
            assert assert_text=="WebView"
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
  • 相关阅读:
    【docker desktop】创建node:18 server
    javaweb 之 HTML快速入门 常用标签 转义字符
    【unity2023打包安卓工程】踩坑记录
    AtCoder Beginner Contest 278「A」「B」「C」「D」「E」「F 对抗博弈」
    [Mac] 安装paddle-pipelines出现 ERROR: Failed building wheel for lmdb
    Maven 安装配置
    【解决obsidian无法跳转到zotero的问题】
    Effectively Learning Spatial Indices(VLDB)
    UX设计VSUI设计
    C语言第十八课:初阶结构体
  • 原文地址:https://blog.csdn.net/gjj920318/article/details/128075328