Appium automated testing foundation - webview operation (emphasis)

All the operations we have mentioned before are operations on native pages.
In the mobile APP, in addition to the native page, there is still a webview page (that is, an H5 page). Let's talk about the operation of the webview page below.

1. First understand what is Hybrid?

A mobile hybrid application is an App that is embedded in a mobile application Webview, Webviewaccessed through a web page.
The mobile application and the Webviewmobile application belong to two different contexts respectively, the mobile application defaults Contextto ”NATIVE_APP”, and Webviewthe default Contextto ”WEBVIEW_被测进程名称”.
When testing Webviewthe content of the web page, you need to switch to the Webviewnext Context. (It is equivalent to opening a new page in the app.)

2. Identify webview

  • Use UI Automator Viewerthe positioning tool to view the page, and find that some areas on the page cannot be located, such as the red area on the left in the figure below, only the large frame can be located, and the elements inside the red frame cannot be recognized.

  • At this time, you can view the element properties, as shown in its classproperties, it says android.webkit.WebView, then there is no doubt that this page is webview.

3, context context

(1) context is the Chinese translation is the context, the environment.
Of course, students who have studied selenium can also understand it as a handle, which is actually the same thing. Anyway, it is enough to know that they are two different environments.
(2) The page to be obtained first is the contexts environment, as shown in the red area in the figure below, and a list is obtained, which includes all pages in the interface, including native pages (native) and webview pages.
NATIVE_APP: This is native, that is, the native page.
WEBVIEW_com.xxxx : This is the webview page.
(3) When you see any information printed out ['NATIVE_APP', 'WEBVIEW_com.baidu.searchbox'], it means that the context of the webview has been obtained (of course, some apps have pitfalls, and there may be a webview, but they cannot be obtained through contexts, which requires special handling)

4. Switch between webview and native page

To operate webviewthe elements on the screen, the first step is to switch the environment (same as selenium's switching iframe and switching handle ideas)

  • Native page switching webview method:switch_to.context(参数是具体webview的context)
  • Webview switch native page method:switch_to.context(原生页面的context)
# 调用方式
# 由于已经获取到contexts了,是一个list对象,取这个list的第二个参数就行,也就是contexts[1]
# contexts = ['NATIVE_APP', 'WEBVIEW_com.baidu.searchbox']
# 1.切换到webview
driver.switch_to.context(contexts[1])

# 2.切换到原生
driver.switch_to.context(contexts[0])

5. Comprehensive exercises

Tip: In the following exercise, I changed the system of the android virtual machine to version 5.1.1, and the android virtual machine of version 7.1.2 could not obtain the context of the webview page, and the reason was not found.

"""
1.学习目标
    必须掌握APP中webview页面的操作方法
2.操作步骤
    2.1 webview页面概念----H5页面

    2.2 识别webview页面
        借助元素定位工具,如果无法获取页面局部元素,只能定位整个页面,
        该页面他的class属性值=android.webkit.webview,则该页面是webview页面

    2.3 操作webview页面---相当于selenium中iframe操作步骤
        1.获取到webview页面的context
            获取所有contexts包括原生页面和webview
                driver.contexts
            获取当前context
                driver.current_context
        2.进入webview
            driver.swith_to.context(具体webview页面的context值)
        3.操作webview页面中的元素
            操作方法和selenium中操作web页面是一致的
            借助chrome://inspect(需要梯子)
            操作过程中,注意chromedriver和手机本身浏览器内核版本匹配
        4.退出webview  等同于进入native
            回到原生页面
            driver.switch_to("NATIVE-APP")

3.需求
    在百度appl操作webview页面:微博登录页面
"""

# 1.导入appium和TouchAction
import time
from appium import webdriver

# 2.创建Desired capabilities对象,添加启动参数
desired_caps = {
    "platformName": "Android",  # 系统名称
    "platformVersion": "5.1.1",  # 系统版本
    "deviceName": "127.0.0.1:21503",  # 设备名称
    "appPackage": "com.baidu.searchbox",  # APP包名
    "appActivity": ".MainActivity"  # APP启动名
}

# 3.启动APP
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", desired_caps)

# 4.操作APP
# 4.1 点击未登录
driver.wait_activity(".MainActivity", 10)
driver.find_element_by_android_uiautomator('new UiSelector().text("未登录")').click()

# 4.2 点击微博登录按钮
driver.wait_activity(".MainActivity", 10)
driver.find_element_by_accessibility_id("微博登录").click()

# 4.3 获取页面所有的context
# 指native(原生)页面和webview页面
driver.wait_activity("com.sina.weibo.sdk.web.WeiboSdkWebActivity", 15)
contexts = driver.contexts
# 输出结果:['NATIVE_APP', 'WEBVIEW_com.baidu.searchbox']
# 'NATIVE_APP' : 代表原生页面
# 'WEBVIEW_com.baidu.searchbox' : 代表webview页面
print(contexts)

# 4.3 进入webview中
driver.switch_to.context(contexts[1])
# 查看当前页面的context
print(driver.current_context)
time.sleep(5)

# 4.4 操作webview页面元素
# 定位微博登录页面的邮箱/手机输入框。并输入内容
# 操作方法和selenium中操作web页面是一致的
username = driver.find_element_by_id("loginName")
# 如果需要输入中文,需要在Desired capabilities对象中添加两个配置项
# "unicodeKeyboard": True,
# "resetKeyboard": True
username.send_keys("123456")

# 这里有几点需要注意:
# 1.在webview页面定位元素需要借助chrome://inspect工具
#   在chrome://inspect中找打当前打开的webview页面(具体操作可以看之前的文章)
#   注意:chrome://inspect的使用需要梯子。
# 2.注意chromedriver和手机本身浏览器内核版本匹配
#   否则定位不到元素,还会报错,如下:
#   WebDriverException : Original error: No Chromedriver found that can automate Chrome '39.0.0'.
#   意思是WebDriver的操作:未找到可自动执行Chrome '39.0.0' 版本的Chromedriver驱动
#   说明一下:在webview中的操作和selenium中的操作是一致的
#       所需webview中操作的执行也是需要Google浏览器的驱动。
#       在Android手机中,浏览器的内核都是Google的,
#       Google浏览器的内核版本是多少,报错中已经给出提示。
#       如上报错信息中提示的是'39.0.0' 版本,
#       我们就需要找一个'39.0.0' 版本对应的的Chromedriver驱动
#       (下载一个2.13版本的Chromedriver驱动就可以对应'39.0.0' 版本的Google浏览器)
#       然后把该Chromedriver驱动放入Appium的安装目录中,具体位置如下:
#       C:\Users\L\AppData\Local\Programs\Appium\resources\app\node_modules\appium\node_modules\appium-chromedriver\chromedriver\win
#       把之前的删除掉,或者备份一下。


# 4.5 退出webview   进入native(原生页面)
driver.switch_to.context(contexts[0])
# 查看当前页面的context
print(driver.current_context)
time.sleep(3)
# 点击关闭微博登陆页面
driver.find_element_by_android_uiautomator('new UiSelector().text("关闭")').click()

# 5.关闭APP
time.sleep(3)
driver.quit()

Focus: Supporting learning materials and video teaching

So here I have also carefully prepared the detailed information of the above outline, which is linked below

Guess you like

Origin blog.csdn.net/m0_59868866/article/details/125812332#comments_27488577