[Appium] Elements of Appium Automation Introduction

I went online today and found that someone was rightappiumIf you are interested, thank you for your attention and collection.Your collection is my greatest support
Tonight finally have time to water at the article again, this time to speak in detailElement positioning in appium

The book is connected to the above [appium] API for getting started with appium automation (below)-a 20,000-character API long article, it is recommended to collect the
environment setup article [appium] getting started with appium automation (on)

2.7 appium element positioning

Preface

appium locates the elements on the app, which can be located through the attributes of ==id,name.class ==

2.7.1 id positioning

  1. The id attribute of appium is also the resource-id attribute viewed through the == UI Automator == tool
    2.
  2. As shown in the figure above, you can locate the format by id:

driver.find_element_by_id(" #here is resource-id")

driver.find_element_by_id("#com.taobao.taobao:id/home_searchedit")

2.7.2 name positioning

  1. The name positioning is viewed through the UI Automator tool == text attribute == (Note: If the text attribute is empty, it cannot be located by the name attribute
    Insert picture description here

  2. As shown in the figure above, you can locate the format by name: driver.find_element_by_name("#here is text") driver.find_element_by_name("#Can not be missing with new pet")

2.7.3 class attribute

  1. Class positioning is the class attribute viewed through the UI Automator tool
    Insert picture description here

  2. As shown in the above figure, you can locate it by class
    Format: driver.find_element_by_class_name("class attribute") driver.find_element_by_class_name("android.widget.EditText")
    (Note:Generally, the class attribute on a page is not unique. If the element is not unique, the positioning will be wrong.

2.7.4 accessibility_id

  1. The accessibility_id positioning is the content-desc attribute viewed through the UI Automator tool (of course the following figureIf the content-desc attribute of this element is empty, it cannot be positioned by this attribute, Here is just an example)
    Insert picture description here

  2. As shown in the above figure, if you see that the content-desc attribute has a value, you can locate the format by accessibility_id: driver.find_element_by_accessibility_id(“#content-desc value”) driver.find_element_by_accessibility_id(“#xxx”)
    (Note: generally one The class attribute on the page is not unique,If the element is not unique, the positioning will report an error

Generally speaking, the above positioning is relatively basic.Suitable for some students who have no code foundation and rely on tools for positioning

For more complicated ones, you can also use == xpath and CSS positioning ==. Among the eight elements, I personally think that these two are the most comprehensive and useful positioning methods.
But it’s a lot to talk about, so I won’t go into details here. If you need a video tutorial in this area, you can click and enter the password: CSDN or look at your collection. If the collection is high, you can have a column of eight elements positioning.

2.8 Switch between native and webview

Preface

Now most apps are hybrid native+webview mode, The corresponding elements on the native can be easily located through uiautomatorviewer, and the elements on the webview cannot be identified.

2.8.1 Identify webview

  1. Use the positioning tool to view the page and find thatSome areas cannot be located, The red area on the left side of the figure below can only be located in this large frame, and the elements in the red frame cannot be recognized.

  2. At this time, you can view the element attributes, such as the class attribute on the right, and it says WebView, so there is no doubt that this page is a webview.
    Insert picture description here

2.8.2 contexts

  1. Context means Chinese translation means context and environment. Of course, students who have studied selenium can also be understood as handles. They are actually the same thing. Anyway, just know that they are two different environments.

  2. First get the page is the contexts environment, as shown in the red area, get a list list:
    NATIVE_APP: This is native, that is, native WEBVIEW_com.xxxx: This is webview

  3. When you see the following two pictures printed, it means that you have obtained the context of the webview (of course, some apps have pits. There may be a webview, but it cannot be obtained through contexts. This requires special treatment)
    Insert picture description here

2.8.3 Switch to webview

1. To manipulate the elements on the webview, the first step is to switch the environment (selenium's switch iframe, switch handle is the same idea)
2. Switch method: switch_to.context (parameter is the webview context) because the second step has been obtained To contexts is a list object, just take the second parameter of this list, which is contexts[1]
Insert picture description here

2.8.4 Switch back to native

  1. After the operation on the webview, if you want to return to the native operation, you need to switch back first. There are two ways to switch back to native:

method one

driver.switch_to.context("NATIVE_APP")
 # 这个 NATIVE_APP 是固定的参数 

Method Two:

driver.switch_to.context(contexts[0]) 
 # 从 contexts 里取第一个参数 

2.8.5 Reference code

#coding:utf-8 
from appium import webdriver 
import time 
desired_caps = {
    
    'platformName': 'Android', 
'deviceName': '30d4e606', 
'platformVersion': '6.0', 
'appPackage': 'com.baidu.yuedu', 
'appActivity': 
'com.baidu.yuedu.splash.SplashActivity'} 
driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub',desired_caps) 
time.sleep(30) 
# 点图书按钮
driver.find_element_by_id("com.baidu.yuedu:id/righttitle").click() 
time.sleep(3) 
# 切换到图书界面后获取所有的环境 
contexts = driver.contexts 
print contexts 
# 切 换 到 webview 
driver.switch_to.context(contexts[1]) 
# 获取当前的环境,看是否切换成功 
now = driver.current_context 
print now 

# 切 回 native 
driver.switch_to.context(contexts[0])

#driver.switch_to.context("NATIVE_APP") 这样也是可以的
#获取当前的环境,看是否切换成功 
now = driver.current_context 
print now 

2.9 View webview elements

Preface

The webview page on the app is actually loaded by the kernel of the enabled chrome browser. How to load the mobile web page on the computer? There is a development mode DevTools on the chrome browser of the computer, which is convenient for debugging.

Environmental preparation

1. Install a chrome browser on the phone
2. Install the chrome browser on the
computer 3. Connect the phone to the computer

2.9.1 Start adb service

  1. Open the chrome browser on the computer and enter: chrome://inspect/#devices
  2. If it is not loaded on the mobile device, start the adb service first, cmd input: >adb devices
    Insert picture description here

2.9.2 DevTools

1. Open the chrome browser on the computer and enter: chrome://inspect/#devices

2. Discover USB devices This should be checked, you can check the mobile device

3. Discover network targets check this box to check the network

4. The arrow in the figure below points to the device name of the phone

5. Webview in com.baidu.yuedu (39.0.00) This is the kernel version number 39 of the browser on the mobile phone (generally, the chrome version number on the computer should be greater than or equal to this version number)
Insert picture description here

2.9.3 View elements

  1. The two addresses in the red frame above are the webpage addresses of the loaded webview, click the inspect button under the address to enter the debugging interface (Note: this requires a ladder)

  2. It doesn't matter if you can't climb the ladder. Copy the corresponding url address, reopen a browser tab, enter the address and press F12 to enter the debugging interface.

  3. The next element positioning is the same as selenium's positioning.
    Insert picture description here
    If you don’t know the positioning of the selenium element, you can click and enter the password: CSDN ask about the video of the selenium element positioning, or wait until I finish the appium article, and then write appium ( digging holes )

2.9.4 Mobile version debugging

  1. The above browser opens the web version page, how to make it into a mobile version for debugging?
    Click the arrow button below to switch to the mobile version
    Insert picture description here

Afterword

Finally, if there is an error in the above code, you can first check it on Baidu according to the error, or if it can’t be solved, you can click and enter the password: CSDN , post a screenshot of your problem, and the boss will take youInsert picture description here

Guess you like

Origin blog.csdn.net/Chaqian/article/details/109262791