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
-
Appium = = 1.21.0
-
JDK version = = 1.8
-
The android SDK – = = Version 29.0.4-5871666
-
Python = = 3.7.9
Second, the steps
-
Identify whether H5 is embedded in app
-
Enable the WebView debugging mode
-
Install the H5 element locating tool
-
Get Obtains all Contexts
-
Switch to the corresponding contexts
-
Download the WebView driver
-
Element location and manipulation in H5
-
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
-
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
-
Use the positioning tool: if the className elements: android. Its. WebView is h5 content
-
Ask development (not recommended, you are unprofessional)
Step 2: Enable WebView debugging mode
-
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
-
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
-
contexts = driver.contexts
-
[‘NATIVE_APP’, ‘WEBVIEW_com.lemon.lemonban’]
-
Note: NATIVE_APP is native to Android, and webview_com.lemon. Lemonban is H5
Step 5: Switch to the corresponding Contexts
-
driver.switch_to.context(contexts[1])
-
Lemonban (” webview_com.lemon. lemonban”); driver. Switch_to. context(” webview_com.lemon. lemonban”)
Step 6: Download the WebView driver
-
Download address (I am using chrome) : npm.taobao.org/mirrors/chr…
-
Obtain the WebView version information using the location tool
-
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