Zhuan-Appium—Handling the operation of H5 in the hybrid APP

 

normal method

Normally, the normal method should be to use UI Automator Viewer to detect the elements of the page, such as the following:

page element

However, if it is mixed development, there will be only one webview element, and the normal method cannot locate it. For example: 
webview

The conventional method is that there is no way to locate this part of the content, there is only one webview, and the content cannot be obtained like native.

tricky way

If we only need to click on H5 elements, we can do this. For example, on our account opening page above, all we need to do is click on this page to open an account immediately and jump to the next page. We can use this trick. method:

1.self.driver.get_name('立即开户').click()

Such code is also achievable. Of course, I have encapsulated the above get_name. The original code is:

1.def get_name(self, name):
2.element = self.driver.find_element_by_name(name)
3.return element

more optimized processing

Since it is an H5 page, if you can get the HTML code, then you can use the way of manipulating the DOM to operate this H5.


Google Chrome: Version 49.0.2623.87 (64-bit)


The Google Chrome I use is this version, connect the phone to the computer, and enter this code in Google Chrome:

1.chrome://inspect

Click this button:

inspect

The magic happens, all the code is displayed in front of you. Debug H5 in hybrid app just like web.

switch webdriver

Although the problem of html code is solved, another problem arises, how do we manipulate the DOM. The conventional method is no way, only the native one can be operated. At this time, we need to switch the webdriver to H5 instead of native one.

In fact, the principle is very simple, as long as the webdriver is switched to the H5 method. Use the following code:

contexts
contexts(self):
  Returns the contexts within the current session.
  返回当前会话中的上下文,使用后可以识别H5页面的控件
:Usage: driver.contexts 用法 driver.contexts

After calling this method, print the result, you will find a list, the first one in the list is NATIVE_APP, this means that the current webdriver is calling the native function, we can use this command to switch.

def switch_h5(self):
        self.driver.execute(MobileCommand.SWITCH_TO_CONTEXT, {"name": "WEBVIEW_com.weizq"}) def switch_app(self): self.driver.execute(MobileCommand.SWITCH_TO_CONTEXT, {"name": "NATIVE_APP"})

The first function is to encapsulate the method of switching to H5. The thing corresponding to the name is the thing printed in the contexts list. Of course, you can also use other methods to encapsulate it.

After switching to H5's webdriver, try using driver.find_element_by_id? Now it becomes a function of finding DOM related functions, then calling the second function, and switching back to the native webdriver.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324782390&siteId=291194637