Android Hybrid (混合页面)自动化测试

定义

个人理解:原生app中嵌入了H5页面

前提条件

  • PC端
    • 浏览器可访问:https://www.google.com/
    • 下载与app对应版本的chromedriver
  • 手机端
    • 应用代码需要打开webview开关
    • 如果是线上,需要开发配合留后门,或者定义个其他字段
  • 代码
    • 需要appPackage、appActivity信息
    • desirecapability中添加"chromedriverExecutable"配置项,指定chromedriver的执行路径
  • 如果是真机,一定要打开WebView调试开关,否则获取不到webview的context。模拟器不需要的。

元素定位

  • 通过uiautomatorviewer渲染后的页面进行定位:该方法不确定性较大,换个机器可能就不适用,一般不建议使用这个方法。
  • 用浏览器通过inspect调试窗口定位:该方式正规可靠,推荐使用该方法。

 

处理上下文

  • 获取当前所有的context:context_list=driver.contexts
  • 从native切换到webview:driver.switch_to.context(context_list[-1])
  • 然后如果后续还要操作native页面,还需要切换回native:driver.switch_to.context(context_list[0])或者driver.switch_to.context("Native_APP")

个人Demo

#!/usr/bin/python3.8.9
# -*- coding: utf-8 -*-

from time import sleep

# @Author  : Tina Yu
# @Time    : 2021-12-14 15:37
from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy


class TestWebviewHybridDemo:

    def setup(self):
        desire_cap = {
            "platformName": "Android",
            "deviceName": "127.0.0.1:7555",
            "appPackage": "io.appium.android.apis",
            "appActivity": ".ApiDemos",
            "noReset": True,
            # 指定单个的chromedriver版本
            # "chromedriverExecutable": "D:/BrowserDriver/chromedriver_win32_2.39/chromedriver.exe",
            # 如果有多个浏览器,则可以放到一个路径下,配置chromedriver所在路径,然后通过json格式的mapping文件配置对应关系
            "chromedriverExecutableDir": "D:/BrowserDriver/all",
            "chromedriverChromeMappingFile": "D:/python_workspace/autotest/demo_app/mapping.json"
        }
        self.driver = webdriver.Remote("http://localhost:4723/wd/hub", desire_cap)
        self.driver.implicitly_wait(10)

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

    def test_hybrid_webview_api_demo(self):
        self.driver.find_element(MobileBy.ACCESSIBILITY_ID, "Views").click()
        text_view = "WebView"
        self.driver.find_element(MobileBy.ANDROID_UIAUTOMATOR,
                                 'new UiScrollable(new UiSelector().scrollable(true))'
                                 f'.scrollIntoView(new UiSelector().text("{text_view}"));').click()
        # 通过uiautomator定位WebView元素:原理是工具获取到信息后渲染到工具页面上,一些渲染是不准确的,所以不建议。
        # self.driver.find_element(MobileBy.XPATH, "//*[@text='i has no focus']").send_keys("这是测试字符串!")
        # self.driver.find_element(MobileBy.XPATH, "//*[@text='i am a link']").click()

        # 获取contexts
        print(self.driver.contexts)

        # 切换上下文,从native切换到WebView,类似于selenium切换iframe
        self.driver.switch_to.context(self.driver.contexts[-1])

        # 通过浏览器的inspect调试工具定位WebView元素,正规可靠
        self.driver.find_element(MobileBy.ID, "i_am_a_textbox").send_keys("Test string!")
        self.driver.find_element(MobileBy.ID, "i am a link").click()

        # 操作完WebView之后还要继续操作原生页面,则需要切换回native
        self.driver.switch_to.context(self.driver.contexts[0])  # 方式一
        # self.driver.switch_to.context("Native_APP")  # 方式二、Native_APP是固定参数

        sleep(3)

更好的参考资料:https://www.cnblogs.com/123blog/p/12624015.html

 

posted @ 2021-12-14 21:13  于慧妃  阅读(157)  评论(0编辑  收藏  举报