Working with locators

🚧

TDK Feature

This is not supported in Testim's visual editor.

Smart LocatorsSmart Locators

With static locators (e.g. CSS-Selector/XPath), we use only one attribute of an element to uniquely identify it on a page and if this changes, the test breaks and as testers, we end up spending a considerable amount of time troubleshooting the problem and fixing it. Based on research, about 30% of testers’ time is consumed in just maintaining tests. Can you imagine the opportunity cost associated with this effort?

Testim Smart Locators are a matrix of a selectors for selecting a specific item so that when the web-page changes the tests for it do not break unless they should.

This enables multiple teams to work on the same application and for a different developer than the developer working on the application to write the tests.

Using Locators

In order to use Smart Locators simply record any interaction and tick the Smart Locators checkbox. This will generate code using Smart Locators automatically and you can use that code directly in your tests.

The Syntax

When exporting tests - select the "Export smart locators" option which will create code like this:

await click(l('.my-login-button'));

The l calls the smart locator function which will run a matrix of selectors on the page and figure out the right element to click on. You can see those locators either in the created Locator.set line created below but do not try to edit it. If you need to edit a locator it is recommended you record that interaction again - edit the locator from the Testim UI and then use that locator in your code.

Architecture

We believe it is a good idea to separate your locators from your business logic code and to reuse them across test files. You are encouraged to move the Locator.set and locator definition call to a separate file and import it. For example:

Locators.js:

export loginButton = l('.my-login-button');

test.js

const { loginButton } = require('./locators');
await click(loginButton)

Can't I just use data attributes that don't change

You certainly can and using smart locators is not mandatory. If you can always guarantee that you and all current and future maintainers of the code will be well disciplined and will always create special test attributes and if you can guarantee that this also holds for all outsourced code than you really don't need to use smart locators.

Auto-Improve (coming soon)

Auto Improve in TDK is expected to come out soon. If you are one of our early-access customers for TDK self-improve please contact your customer success engineer directly for support questions regarding this features.