十二、Android WebView测试

Appium多架构支持

image.png

环境准备

  • 手机端

    • 被测浏览器:(不可以是第三方浏览器)‘Safari' for iOS and 'Chrome', Chromium', or 'Browser' for Android
  • PC端

  • 客户端代码:

    • desirecapability.
      • "chromedriverExecutable”=“指定driver地址”
      • "browser"="Browser”或者"browser"= "Chrome"
  • Chromedriver默认地址

    • Mac系统:/Applications/Appium.app/Contents/Resourcs/app/node_modules/appium/node_modules/appium-chromedriver/chromedriver/mac
    • Windows系统:C:\Users\{主机名}\AppData\Local\Programs\Appium\resources\app\node_modules\appium\node_modules\appium-chromedriver\chromedriver\win
  • 查看手机端浏览器版本:

    • 获取浏览器包名:adb shell pm list package | grep browser,Windows将grep换为findstr
C:\Users\insan>adb shell pm list package | findstr browser
package:com.android.browser
  • 获取浏览器版本:adb shell pm dump com.android.browser | grep version,Windows将grep换为findstr
C:\Users\insan>adb shell pm dump com.android.browser | findstr version
      versionCode=23 targetSdk=23
      versionName=6.0.1
  • 获取手机端Chrome浏览器版本:adb shell pm dump com.android.chrome | grep version,Windows将grep换为findstr

连接chrome的inspect

  • 连接后就能和web页面定位一样进行selenium的定位


    image.png

    image.png

实例

  • 打开:http://m.baidu.com
  • 定位搜索框输入“appium”
  • 点击“百度一下”进行搜索
from time import sleep
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(self):
        des_caps = {
            'platformName': 'android',
            'platfomVersion': '6.0 ',
            'browserName': 'Browser',
            'noReset ': True,
            'deviceName ': '127.0.0.1:7555',
            # 'chromedriverExecutable ' : '/Users/juanxu/Documents/chromed`
        }

        self.driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub",des_caps)
        self.driver.implicitly_wait(10)

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

    def test_browser(self):
        self.driver.get("http://m.baidu.com")
        sleep(5)
        self.driver.find_element_by_id("index-kw").click()
        self.driver.find_element_by_id("index-kw").send_keys("appium")
        search_locator = (By.ID, "index-bn")
        # 显示等待
        WebDriverWait(self.driver, 10).until(expected_conditions.visibility_of_element_located(search_locator))
        self.driver.find_element(*search_locator).click()

Android 混合页面测试

  • 如何判断页面是webview
    • 断网查看
    • 看加载条
    • 看顶部是否有关闭按钮
    • 下拉刷新页面是否刷新
    • 下拉刷新的时候是否有网页提供方
    • 用工具查看
image.png

webview

  • WebView
    • android系统提供能显示网页的系统控件(特殊的view)
    • <Android 4.4 WebView底层实现WebKit内核
    • >= Android 4.4 Google采用chromium作为系统WebView底层支持,API没变,支持HTML5,CSS3,,JavaScript
image.png

测试前提条件

定位元素方式

  1. chrome://inspect
  2. 获取url在浏览器里查看

实战

  • 定位app中的webview需要切换到对应的上下文 driver.contexts
    • driver.switch_to.context(driver.contexts[-1])
      image.png
from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy


class TestBrowser:

    def setup(self):
        des_caps = {
            'platformName': 'android',
            'platfomVersion': '6.0 ',
            'appPackage': 'io.appium.android.apis',
            'appActivity': 'io.appium.android.apis.ApiDemos',
            # 'browserName': 'Browser',
            'noReset ': True,
            'deviceName ': '127.0.0.1:7555',
            'chromedriverExecutable': '/Users/juanxu/Documents/chromed' # chromedriver路径
        }

        self.driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub",des_caps)
        self.driver.implicitly_wait(10)

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

    def test_webview(self):
        self.driver.find_element_by_accessibility_id("Views").click()
        webview = "WebView"
        print(self.driver.contexts)
        self.driver.find_element_by_android_uiautomator('new UiScrollable(new UiSelector().'
                                                         'scrollable(true).instance(0)).'
                                                         f'scrollIntoView(new UiSelector().text("{webview}").'
                                                         'instance(0));')
        """
        通过Accessbility_id定位针对不同的手机端可能定位不同
        """
        # self.driver.find_element(MobileBy.ACCESSIBILITY_ID, 'i has no focus').send_keys("this is a test string")
        # self.driver.find_element(MobileBy.ACCESSIBILITY_ID, 'i am a link').click()
        # # 打印页面源码
        # print(self.driver.page_source)
        """
        使用inspect定位,需要先切换上下文
        """
        print(self.driver.contexts)
        self.driver.switch_to.context(self.driver.contexts[-1])
        self.driver.find_element(MobileBy.ID, "i_am_a_textbox").send_keys("this is a test string use chrome inspect")
        self.driver.find_element(MobileBy.ID, "i am a link").click()
        print(self.driver.page_source)

调试页面

image.png

获取webview版本

  • 查看手机浏览器的版本
    • adb shell pm list package lgrep webview
    • adb shell pm dump com.android.browser l grep version
    • adb shell pm dump com.android.chromel grep version
    • adb shell pm dump com.android.webview l grep version

webview开关

image.png

测试Webview另一种方法——获取地址

  • 获取App的Webview网页地址:adb logcat -v time | grep http
  • 但是此方法针对不同的设备元素可能不一致


    image.png

实战二

  • 雪球案例
    • 打开应用
    • 点击′交易’
    • 点击'A股开户’
    • 在输入用户名和密码
    • 点击'立即开户’
    • 退出应用
  • 代码
from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy


class TestBrowser:

    def setup(self):
        des_caps = {
            'platformName': 'android',
            'platfomVersion': '6.0 ',
            'appPackage': 'com.xueqiu.android ',
            'appActivity': 'com.xueqiu.android.common.MainActivity',
            # 'browserName': 'Browser',
            'noReset ': True,
            'deviceName ': '127.0.0.1:7555',
            'skipServerInstallation': 'true',  # 跳过服务安装
            'chromedriverExecutable': '/Users/juanxu/Documents/chromed'  # chromedriver路径
        }

        self.driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", des_caps)
        self.driver.implicitly_wait(10)

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

    def test_webview(self):
        # 点击交易
        self.driver.find_element(MobileBy.XPATH, '//*[@text="交易"]').click()
        A_locator = (MobileBy.XPATH, '//*[@id="Layout_app_3V4"]/div/div/ul/li[1]/div[2]/h1')
        print(self.driver.contexts)
        # 切换上下文
        self.driver.switch_to.context(self.driver.contexts[-1])
        print(self.driver.window_handles)  # 要切换到不同的窗口,打印切换之前的句柄

        # 点击'A股开户'
        WebDriverwait(self.driver, 10).until(expected_conditions.element_to_be_clickable(A_locator))
        self.driver.find_element(*A_locator).click()

        print(self.driver.window_handles)  # 要切换到不同的窗口,打印切换之前的句柄
        kaihu_window = self.driver.window_handles[-1]  # 一般是在最后一个出现的页面,也可以通过遍历方式查找
        self.driver.switch_to.window(kaihu_window)  # 进行页面切换
        # 显示等待手机号码输入框
        phonenum_locator = (MobileBy.ID, 'phone-number')
        WebDriverwait(self.driver, 60).until(expected_conditions.element_to_be_clickable(phonenum_locator))
        # 输入用户名和验证码,点击立即开户
        self.driver.find_element(*phonenum_locator).send_keys("13810100202")
        self.driver.find_element(MobileBy.ID, 'code ').send_keys("1234")
        self.driver.find_element(NobileBy.CSS_SELECTOR, 'body>div>div>div.form.wrap>div>div.btn-submit').click()

遇到的坑

  • 设备
    • android模拟器6.0默认支持webview操作(mumu不可以,genimotion和sdk自带的emulator可以)
    • 其它模拟器和物理机需要打开app内开关(webview调试开关)
  • PC浏览器定位元素
    • chrome浏览器-Chrome 62才可以更好的看见webview的内部,其它的版本都有bug
    • 也换成chromium浏览器可以避免很多坑﹐展示效果和速度比chrome要快
  • 代码
    • 有的设备可以直接使用find_element_by_accessibility_id(),不同的设备渲染的页面不同,兼容性不适合
    • switch_to.context(),切换上下文,app和webview的相互切换
    • switch_to.window(),webview中切换不同的窗口,当有新的url时就需要切换
  • chromedriver处理
    1. 在代码中指定chromedriver路径,使用desired_compatibility中的chromedriverExecutable参数
    2. (1)可以将chromedriver放到统一的一个文件夹下,然后在desired_compatibility中加入chromedriverExecutableDir 关键字,指定路径;
      (2)然后在代码路径中创建一个映射文件:mapping.json,内容如下:前面未Chromedriver版本,后面为webview版本。然后在desired_compatibility中加入chromedriverChromeMappingFile参数,指定文件路径。配置完成后就会在chromedriverExecutableDir 关键字中配置的路径下去找匹配的Chromedriver
{
  "2.42": "63.0.3239",
  "2.41": "62.0.3202",
  "2,20": "44.0.2403"
}
  • 通过appium日志来查找Chromedriver版本和webview版本


    image.png

下一节:微信小程序测试,对微信小程序进行自动化以及控件定位分析。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容