Locating Elements

Locating UI Elements

Automated UI tests (either web or mobile), are mostly focused on performing certain actions on page's or screen's elements. One of the key challenges in writing UI tests is correctly identifying elements on the screen. Accurate element identification is very important to have stable automation tests.

If the elements are incorrectly identified, the test will misbehave and perform actions on wrong elements or just fail, not able to find any. This is called test flakiness. To prevent or minimize flaky tests, you have to make sure the UI elements are uniquely identified.

Selenium and Oxygen use locators to identify UI elements. Most of Oxygen commands in web and mob modules require a locator to perform various actions on the element (such as find an element, assert element's value, etc.). Oxygen provides a wide variety of locator types (strategies) which help to identify elements by different attributes and hierarchical position.

Oxygen allows you to either generate locators automatically or write them manually using various element inspection tools. Here is what you can do:

  • Use Oxygen IDE Recorder - Oxygen IDE can record Chrome browser based user interactions and generate all possible locators for the selected element. Oxygen IDE will also pre-select the most suitable locator for the specific object. See Web Recording Guide for more details.

  • Insert Locators Manually - choose and insert element locators manually in your Oxygen script, using UI element inspection tools like Chrome DevTools, Appium Desktop, etc.

Note: In order to record mobile application actions and locators, you will need to use a special version of Appium Desktop that supports generating Oxygen code. See Mobile Recording Guide for more details.

Locator Types

Oxygen provides different locator types to help to identify UI elements of web and mobile applications. To indicate which locator type to use, the locator parameter shall start with type prefix. Below are various locator types and prefixes for web and mobile applications.

Web Locator Types

Locator

Prefix

Usage

ID

id=

Locates elements by the value of the id attribute.

Name

name=

Locates elements by the value of the name attribute.

Link

link=

Locates elements by the text in A (anchor) HTML tag.

CSS Selector

css=

Locates elements via the driver’s underlying W3 CSS Selector engine.

XPath

xpath=

Locates elements by XPath.

See Web Module (web) for more locator types and additional details.

Mobile Locator Types

Locator

Prefix

Usage

ID

id=

Locates elements by their unique identifier - resource-id attribute for android and name attribute for iOS.

Class

class=

Locates elements by the full name of UI element's class.

Text

text=

Locates elements by their visible text.

XPath

xpath=

Locates elements by XPath.

See Mobile Module (mob) for more locator types and additional details.

Here are some usage examples:

web.click('id=submit') // ID

web.type('name=firstNameInput', 'first name') // Name

web.click('https://link.com') // Link

web.click('div > button.login') // CSS Selector

web.select('//select[@id="selectGender"]', 'label=female') // XPath

Last updated