雄阔海 發表於 2021-1-22 07:32:00

Android WebView测试

<p>混合应用中包含 Web 视图的应用,在 Appium 介绍及环境安装 中介绍了appium支持WebView测试,本文将分享Android 混合页面的测试方法。</p>
<h1 id="webview测试环境准备">WebView测试环境准备</h1>
<h2 id="手机端">手机端</h2>
<ul>
<li>
<p>被测浏览器:</p>
<p>iOS:Safari</p>
<p>Android:Chrome,Chromium,Browser (自带浏览器)</p>
</li>
</ul>
<h2 id="pc端">PC端</h2>
<ul>
<li>安装 Chrome浏览器(或chromium),可以访问https://www.google.com</li>
<li>下载手机浏览器webview版本对应的 driver
<ul>
<li>webview和driver版本对应关系:https://raw.githubusercontent.com/appium/appium-chromedriver/master/config/mapping.json</li>
<li>driver下载:https://sites.google.com/a/chromium.org/chromedriver/downloads</li>
<li>国内镜像地址 : https://npm.taobao.org/mirrors/chromedriver/</li>
<li>appium github: https://github.com/appium/appium/blob/master/docs/en/writing-running-appium/web/chromedriver.md</li>
</ul>
</li>
</ul>
<h2 id="查看手机浏览器版本">查看手机浏览器版本</h2>
<pre><code class="language-shell">adb shell pm list package | grep webview
adb shell pm dump com.android.browser | grep version
adb shell pm dump com.android.chrome | grep version
adb shell pm dump com.android.webview | grep version
</code></pre>
<p>查看手机browser和chrome版本</p>
<pre><code class="language-shell">C:\Users\10287&gt;adb shell pm list packages|findstr browser
package:com.android.browser

C:\Users\10287&gt;adb shell pm dump com.android.browser | findstr version
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;versionCode=22 targetSdk=22
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;versionName=5.1.1-500200323

C:\Users\10287&gt;adb shell pm dump com.android.chrome | findstr version
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;versionCode=398713200 targetSdk=29
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;versionName=80.0.3987.132
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;enabled=true targetSdkVersion=29 versionCode=398713200

</code></pre>
<h2 id="查看手机webview版本">查看手机webview版本</h2>
<p>在手机上设置中查看 Android System WebView应用版本</p>
<p><img src="https://img2020.cnblogs.com/blog/2229336/202012/2229336-20201220153906791-181752664.png"></p>
<h2 id="客户端代码">客户端代码</h2>
<ul>
<li>
<p>desirecapability</p>
</li>
<li>
<ul>
<li>“chromedriverExecutable" = "指定driver地址”</li>
<li>"browser" = "Browser" 或者“browser" = ”Chrome“</li>
</ul>
</li>
</ul>
<h1 id="webview元素定位">WebView元素定位</h1>
<p>1、连接手机/模拟器</p>
<p><img src="https://img2020.cnblogs.com/blog/2229336/202012/2229336-20201220153913614-1268531789.png"></p>
<p>手机打开google浏览器,并进入百度网页:https://m.baidu.com</p>
<p>2、Chrome浏览器输入地址: chrome://inspect</p>
<p>可以看到电脑连接的设备名以及打开的手机浏览器</p>
<p><img src="https://img2020.cnblogs.com/blog/2229336/202012/2229336-20201220153919894-227584153.png"></p>
<p>点击inspect</p>
<p><img src="https://img2020.cnblogs.com/blog/2229336/202012/2229336-20201220153936057-915462835.png"></p>
<p>这样就可以定位到浏览器元素。</p>
<h1 id="android混合页面测试">Android混合页面测试</h1>
<p>文档:https://developers.google.com/web/tools/chrome-devtools/remote-debugging/webviews?hl=zh-cn</p>
<p>测试步骤:</p>
<ol>
<li>打开ApiDemos</li>
<li>进入WebView页面</li>
<li>点击"i am a link"</li>
<li>退出应用</li>
</ol>
<p><img src="https://img2020.cnblogs.com/blog/2229336/202012/2229336-20201220154000250-1805821470.png"></p>
<p>下载ApiDemos-debug.apk</p>
<ul>
<li>https://github.com/appium/appium/blob/master/sample-code/apps/ApiDemos-debug.apk</li>
</ul>
<pre><code class="language-python">from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy

class TestBrowser():
    def setup(self):
      desired_caps = {
      'platformName': 'android',      
      'platformVersion': '10',
      'appPackage': 'io.appium.android.apis',
      'appActivity': 'io.appium.android.apis.ApiDemos',      
      'deviceName': 'CUYDU19626004019',
      'noReset': 'true',
      'chromedriverExecutable': 'D:/testing_tools/chromedriver85/chromedriver.exe'
      }
      self.driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)
      self.driver.implicitly_wait(5)
      
    def teardown(self):      
      self.driver.quit()
      
    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));').click()

      print(self.driver.contexts)
      self.driver.switch_to.context(self.driver.contexts)
      print(self.driver.current_context)
      self.driver.find_element(MobileBy.ID, 'i am a link').click()
</code></pre>
<center><b>--THE END--<b></b></b></center><b><b>
<blockquote>
<p>文章标题:Android WebView测试<br>
本文作者:hiyo<br>
本文链接:https://www.cnblogs.com/hiyong/p/14163722.html<br>
欢迎关注公众号:「测试开发小记」及时接收最新技术文章!</p>
</blockquote>
</b></b><br><br>
来源:https://www.cnblogs.com/hiyong/p/14163722.html
頁: [1]
查看完整版本: Android WebView测试