Before the speech

More and more pages in today’s apps are being done through H5, for the simple reason that users can use the new features without upgrading the app. This is hard for us test engineers, hybrid application automation will not play. Today let’s talk about how to do hybrid application automation in app.

First, environmental preparation

  1. Appium = = 1.21.0

  2. JDK version = = 1.8

  3. The android SDK – = = Version 29.0.4-5871666

  4. Python = = 3.7.9

Second, the steps

  1. Identify whether H5 is embedded in app

  2. Enable the WebView debugging mode

  3. Install the H5 element locating tool

  4. Get Obtains all Contexts

  5. Switch to the corresponding contexts

  6. Download the WebView driver

  7. Element location and manipulation in H5

  8. If necessary, cut back to APP after h5 operation

Three, detailed explanation of the steps

Step 1: Identify whether H5 is embedded in the APP

  1. On the mobile phone, open the developer option, open the layout boundary, open the app under test, the page without layout is the H5 content

  2. Use the positioning tool: if the className elements: android. Its. WebView is h5 content

  3. Ask development (not recommended, you are unprofessional)

Step 2: Enable WebView debugging mode

  1. On the Web side, we can directly press F12 to enter the debugging mode for element positioning, but not on the APP side. We can open the WebView debugging mode of the H5 page embedded in the APP to view elements and write element positioning expressions as on the Web side

  2. Specific way reference curled her teacher 】 【 article: www.cnblogs.com/yyoba/p/111…

Step 3: Install the H5 element locator tool

  • Download and install UC-DevTools (latest version) : dev.ucweb.com/download/?s…

  • Open the UC-DevTools tool. Once the phone is successfully connected to the computer, UC-DevTools automatically connects to your phone

  • Click Settings and select the local Devtools Inspector UI resource

  • If there are H5 resources on the app page, uc-DevTools automatically identifies the H5 resources on the app page on the Home page:

  • Click the inspect to see the familiar Web element location page

Step 4: Fetch Fetch all Contexts

  1. contexts = driver.contexts

  2. [‘NATIVE_APP’, ‘WEBVIEW_com.lemon.lemonban’]

  3. Note: NATIVE_APP is native to Android, and webview_com.lemon. Lemonban is H5

Step 5: Switch to the corresponding Contexts

  1. driver.switch_to.context(contexts[1])

  2. Lemonban (” webview_com.lemon. lemonban”); driver. Switch_to. context(” webview_com.lemon. lemonban”)

Step 6: Download the WebView driver

  1. Download address (I am using chrome) : npm.taobao.org/mirrors/chr…

  2. Obtain the WebView version information using the location tool

  3. Desired_caps [“chromedriverExecutable”]=” just loaded driver position”

Step 7: Element location and manipulation in H5

Element positioning and manipulation on the Web are not covered here

Step 8: Cut back to APP after H5 operation

driver.switch_to.context(‘NATIVE_APP’)

Four, demo code

Lemon community - > locate registered - > point of H5 import time from appium import webdriver from appium.webdriver.com. Mon mobileby import mobileby from selenium.webdriver.support.wait import WebDriverWait from selenium.webdriver.support import expected_conditions as EC desired_CAPS = {'automationName':'UiAutomator2', 'platformName':'Android', 'platformVersion':'7.1.2', 'deviceName': 'xiaomi', 'appPackage':'com.lemon.lemonban', 'appActivity':'.activity.MainActivity', 'noReset':'True', 'chromedriverExecutable':r'D:\chromeDriver\6769\chromedriver.exe' } driver = webdriver.Remote(command_executor='http://localhost:4723/wd/hub', Desired_capabilities =desired_caps) wait = WebDriverWait(driver=driver,timeout=10) time.sleep(2) # Loc1 = (MobileBy ANDROID_UIAUTOMATOR, 'resourceId (" com. Lemon. Lemonban: id/category_title "), text (" lemon community ")') ele1 = Wait. Until (ec.visibility_of_element_located (locator=loc1)) time.sleep(2) ele1.click() time.sleep(2) # Retrieve all Contexts until(ec.visibility_of_element_located (locator=loc1) All_contexts = driver. Contexts Print (' All Contexts: Context (" webview_com.lemon. Lemonban ") print(' Switch succeeded ') # MobileSwitchTo(driver=driver).context(context_name=" webview_com.lemon.lemonban ") time.sleep(2) # locate the registration in H5 Loc2 =(mobileby.xpath,'//a[text()=" register "]') ele2 = wait. Until (ec.visibility_of_element_located (locator=loc2)) time.sleep(2)  ele2.click() time.sleep(20)Copy the code

Comment section

Did you learn today’s lesson?

Is not very practical ~

Anything else you’d like to know leave a comment at the end