Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the common element positioning skills in appium?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces the relevant knowledge of "what are the common element positioning skills of appium". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Appium uses uiautomatorviewer.bat as a tool to obtain element positioning directly for native android applications.

Before moving on to the topic of appium element positioning, let's take a look at some of the basics of appium:

1. Test object

Appium is an open source automated testing tool that supports native applications, web applications and hybrid applications on iOS and Android platforms.

Mobile native applications: applications written solely in ios or android development language for a specific type of mobile device that can be installed directly into the device, generally available through the App Store

Mobile web applications: applications accessed using mobile browsers (appium supports Safari on iOS and Chrome on Android), do not need to be downloaded to the device, but accessed directly through the browser

Hybrid applications: developed using both web language and programming language, distinguishing the distribution of mobile operating systems through the app store, users need to install and use mobile applications.

2. Support platform and language

Appium is cross-platform and supports OSX,Windows and Linux systems. It allows testers to write automated test scripts using the same set of API on different platforms (iOS,Android), which greatly increases the reusability of code between iOS and Android test suites

Appium supports multiple languages and adopts Cramp S design mode, as long as it meets client's ability to send http requests to server.

III. Element positioning

1. Format: find_element_by_ location method (value)

I explain it according to the calculator that comes with my phone.

Positioning through id

Get the resource-id of the button through the tool

Driver.find_element_by_id ('resource-id')

Example: driver.find_element_by_id ('com.android.bbkcalculator:id/clear')

Positioning through class

Get the class of the button through the tool

Driver.find_element_by_class_name ('class')

Example: driver.find_element_by_class_name ('android.widget.ImageButton')

Positioning through text

Appium1.5 and later versions discarded the name attribute (such as name= Baidu, you need to use the location method of uiautomator and the content of text)

Driver.find_element_by_android_uiautomator ("text (\ Chinese\)")

Example: driver.find_element_by_android_uiautomator ("text (\" Baidu\ "))

Note: you need to make sure that this Chinese is the only value on the current page, otherwise it still can't be located, pay attention to the writing, not directly copy the Chinese into it.

Positioning through xpath

Driver.find_element_by_xpath ("xpath content")

Example: driver.find_element_by_xpath ("/ / android.widget.FrameLayout/android.widget.ImageButton [4]")

The following methods are not used for the time being, but will be changed later if there is a problem.

Through accessibility_id

This field is not found on the UI Automator Viewer interface. The corresponding field is content-desc.

Driver.find_element_by_accessibility_id ("accessibility_id")

No example was found. If you encounter an automated APP by yourself, you can use the

Positioning via css_selector (webview)

Only applicable to html pages of webview, inherited from webdriver and consistent with the pc version of the UI test

Driver.find_element_by_css_selector ()

Positioning via link_text (webview)

Only applicable to html pages in the webview container, inherited from webdriver and consistent with the pc version of the UI test

Driver.find_element_by_link_text ()

Positioning through name

Only applicable to html pages in the webview container, inherited from webdriver and consistent with the pc version of the UI test

Driver.find_element_by_name ()

two。 Another way to write positioning elements: find_element (by,value)

Find_element_by_ mode (value) actually calls find_element (by,value)

You need to import this package: from selenium.webdriver.common.by import By

For example: locate the element whose id is ag2

Method 1: driver.find_element_by_id ("ag2")

Method 2: driver.find_element (By.ID, "ag2")

The advantage of this operation is that you can directly put the by and value of the operation into a tuple, and then call the general method to pass parameters to get the element result.

Cateid= (By.ID, "ag2")

Driver.find_element (* cateid) .click ()

The operation of by can be:

By.ID is equivalent to by_id.

By.CLASS_NAME is equivalent to by_class_name.

By.XPATH is equivalent to by_xpath.

By.NAME is equivalent to by_name.

By.TAG_NAME is equivalent to by_tag_name.

By.CSS_SELECTOR is equivalent to by_css_selector.

By.LINK_TEXT is equivalent to by_link_text.

3. Value returns an array of elements

The usage is the same as in find_element_by_ mode (value), but returns an array. You can access a specific result through the index of the array.

For example: navigate to multiple elements through class_name, I want to click on the first element

Driver.find_elements_by_class_name ("android.widget.RelativeLayout") [0] .click ()

4. Another way to write an array of returned elements: find_elements (by,value)

The usage is the same as find_element (by,value), but returns an array. You can access a specific result through the index of the array.

For example: navigate to multiple elements through class_name, I want to click on the first element

Driver.find_elements (By.CLASS_NAME, "android.widget.RelativeLayout") [0] .click ()

5. Locate an element by an element

You can find an element first, and then further locate the element

Find_element_by_class_xpath ("xxx") .find_element_by_name ("yyy")

IV. Element operation

What you can do with the element after you find it, such as further locating the element as described above

1.click ()

/ / Click action

You can also use tab to implement click operations.

Driver.find_element_by_id (com.wuba.zhuanzhuan:id/ae8) .click ()

2.clear ()

/ / clear the contents of the input box

Driver.find_element_by_id (com.wuba.zhuanzhuan:id/ij) .clear ()

3.send (xx)

/ / enter the content in the input box

Driver.find_element_by_id ("com.wuba.zhuanzhuan:id/ij") .send_keys ("test content")

4.text

/ / get the text content of the element

Print (driver.find_element_by_xpath ("/ / android.widget.LinearLayout [1] / / xxx") .text)

This is the end of the content of "what are the common element positioning skills of appium". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report