其他分享
首页 > 其他分享> > Android Hybrid (混合页面)自动化测试

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

作者:互联网

定义

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

前提条件

元素定位

 

处理上下文

个人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,
            "chromedriverExecutable": "D:/BrowserDriver/chromedriver_win32_2.39/chromedriver.exe"
        }
        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(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

 

标签:Hybrid,self,driver,element,context,MobileBy,Android,find,页面
来源: https://www.cnblogs.com/fengyudeleishui/p/15688953.html