Appium automated testing of mixed application processing

appium testing hybrid applications

1. Introduction to native, web, hybrid

At present, mainstream applications are roughly divided into three categories: web APP (web application), hybrid APP (hybrid application), and native APP (native application).

native:

Advantages: Relying directly on the operating system, with the strongest interaction and the best performance; the most powerful function, especially in the interaction with the system, almost all functions can be realized.

Disadvantages: high development cost, unable to cross-platform, separate development on different platforms Android and iOS; high threshold, native personnel have a certain entry threshold, compared with the majority of front-end personnel, the update is slow, especially After the application store is released, it needs to wait until the review cycle; maintenance costs are high.

web:
Advantages: low development cost, available platform, easy to call; low maintenance cost; updates do not need to notify users, do not need to manually upgrade; do not need to install APP, will not occupy the phone memory.

Disadvantages: Unable to get system-level notifications, reminders, motion effects, etc., low user retention rate; many restrictions on design; poor experience.

hybrid:

Advantages: Webview is embedded in the application, and the webpage is accessed through webview; the development cost is low, it can be cross-platform, and the debugging is convenient; the maintenance cost is low, the function can be used; the function is more complete, the performance and experience are much better than the web APP ; Update is more free.

Disadvantages: Compared with the original, there is still a large loss in performance; it is not suitable for apps with strong interaction.

Switching method:

print(self.driver.contexts)

#View all context

#Return result['NATIVE_APP','WEBVIEW_com.huawei.browser']

#NATIVE_APP is native, and the beginning of WEBVIEW represents H5# and then use the following method to switch to H5

self.driver.switch_to.context("WEBVIEW_com.huawei.browser")

#Use the following method to confirm whether the switch is successful

print(self.driver.current_context)

2. How to distinguish whether an APP page is native or web.

In the phone/emulator, click on the version number in the phone under 5, and the developer options will appear.

Check the display layout boundary in the developer options and then return to the APP interface.

If the APP is a web page, the interface will not have a layout boundary display, if there is, it means that it is a native interface.

Now through the positioning tool:

The APIs we used before are all for native applications. If it is a web page, these APIs cannot be used to locate elements, and many applications are hybrid applications.

3. Hybrid automation script development

Identify webview view

We can judge according to the method described above.

Get all contexts: driver.contexts

Understand the context:

Context context, question This scenario is the process of interaction between the user and the operating system. The native interface and the webview interface belong to different contexts. The native default is "NATIVE_APP", and the webview defaults to "WEBVIEW_tested process name".

By switching the context object, appium can recognize which state it is currently in.

4. Switch to webview view

Example:

  1. # 2、切换-Window窗口

  2. # context - 混合应用 native_app

  3. time.sleep(5)

  4. # 1、获取所有的context 确保你的APP能够让代码识别到webview

  5. cons = driver.contexts

  6. print(cons)

  7. # 2、根据context名字,切换到webview # WEBVIEW_com.lemon.lemonban

  8. # driver.switch_to.context(cons[-1])

  9. driver.switch_to.context('WEBVIEW_com.lemon.lemonban')

  10. # 打印web内容

  11. webinfo = driver.page_source

5. Locate the elements in the webview

Having switched to HTML, the follow-up will be web automation operations --- native webview, basically Chromedriver.

(1) Enter in Chrome browser: chrome://inspect

This method can only be used after the inspect is operated (not recommended).

(2) Use uc-devtools

For details, please refer to the official document: https://dev.ucweb.com/docs/pwa/docs-zh/xy3whu

6. Back to native_app

driver.switch_to.context(None)

in conclusion:

1. The webview in the APP must be recognized, and the elements to be operated are in the webview.

2. Use appium's context switch to switch from NATIVE_APP to webview.

3. Use uc-devtools to locate and operate web page elements.

4. For web pages, you need to download Chromedriver that matches the Android system webview version

7. There are two flaws that may prevent you from getting webview

First of all, most applications will not open the debug mode of webview.

Second, the situation of switching webview will be affected by the network status. If it is an internal proxy network, you will not be able to operate chromedriver. When switching to webview mode, you will not be able to pass in commands and cause a timeout, so this method does not affect it. Make it happen.

Guess you like

Origin blog.csdn.net/grl18840839630/article/details/109632489