Automation technology of Appium WeChat webview

Table of contents

Foreword:

WeChat settings

Write test cases using ChromeDriver

Write test cases using appium


Foreword:

Appium is an open source tool for automated mobile application testing, through which automated testing of mobile applications can be performed on real devices or emulators. WeChat Webview refers to the WeChat web interface embedded in the mobile application, such as the interface displayed when opening the WeChat login or sharing function in the application.

WeChat settings

Use WeChat to open debugx5.qq.com, which is a WeChat x5 kernel debugging page. You can enter this URL in any chat window and open it. Check "Whether to enable TBS kernel Inspector debugging function
"

Write test cases using ChromeDriver

First install ChromeDriver
from the official download or find chromedriver from your appium installation path. In fact, the chromedriver path will also be printed in the execution log of appium. Then
start it on the computer and set the port

chromedriver --url-base=wd/hub --port=8000

You can use selenium or appium client to write test cases.
The following is my scalatest test case. You can use other languages ​​to implement

test("test chromedriver weixin") {
  val options = new ChromeOptions()
  options.setExperimentalOption("androidPackage", "com.tencent.mm")
  options.setExperimentalOption("androidUseRunningApp", true)
  options.setExperimentalOption("androidActivity", ".plugin.webview.ui.tools.WebViewUI")
  options.setExperimentalOption("androidProcess", "com.tencent.mm:tools")
  val capability = DesiredCapabilities.chrome()
  capability.setCapability(ChromeOptions.CAPABILITY, options)
  val url = "http://127.0.0.1:8000/wd/hub"
  val driver = new AndroidDriver[WebElement](new URL(url), capability)
  driver.get("https://testerhome.com/topics/6954")
  println(driver.getPageSource)
  driver.quit()
}

Write test cases using appium

Some people often ask why appium on android can’t automate WeChat webview. In fact, it is possible. It is mainly caused by a bug in the current appium. When switching context in appium, it
does not bring a key androidProcess configuration.
He will When appium recognizes the webview, it recognizes the webview of com.tencent.mm:tools as the webview of com.tencent.mm. As a result, the context switch fails.

The correct way to test WeChat h5 with appium is as follows

test("test weixin h5") {
  val capability = new DesiredCapabilities()
  capability.setCapability("app", "")
  capability.setCapability("appPackage", "com.tencent.mm")
  capability.setCapability("appActivity", ".ui.LauncherUI")
  capability.setCapability("deviceName", "emulator-5554")
  capability.setCapability("fastReset", "false")
  capability.setCapability("fullReset", "false")
  capability.setCapability("noReset", "true")
  //capability.setCapability("unicodeKeyboard", "true")
  //capability.setCapability("resetKeyboard", "true")

  //关键是加上这段
  val options = new ChromeOptions()
  options.setExperimentalOption("androidProcess", "com.tencent.mm:tools")
  capability.setCapability(ChromeOptions.CAPABILITY, options)

  val url = "http://127.0.0.1:4723/wd/hub"
  val driver = new AndroidDriver[WebElement](new URL(url), capability)
  println(driver.getPageSource)
  driver.findElementByXPath("//*[@text='我']").click
  driver.findElementByXPath("//*[@text='收藏']").click
  driver.findElementByXPath("//*[contains(@text, '美团外卖')]").click
  println(driver.getPageSource)
  println(driver.getContextHandles)
  driver.context("WEBVIEW_com.tencent.mm:tools")
  println(driver.getPageSource)
}

The most important thing is this sentence

val options = new ChromeOptions()
options.setExperimentalOption("androidProcess", "com.tencent.mm:tools")
capability.setCapability(ChromeOptions.CAPABILITY, options)

  As someone who has been here, I also hope that everyone will avoid some detours

Here I will share with you some necessities of the way forward in automated testing, hoping to help you.

(software testing related materials, automated testing related materials, technical questions and answers, etc.)

I believe it can make you better progress!

Click on the small card below

Guess you like

Origin blog.csdn.net/Free355/article/details/131785568