In the previous section I showed how to improve the maintainability of your test suite by reducing logic duplication. In this section, you will see how to reduce duplication of locators.

In most test automation efforts, there will be elements in the target application that are frequently touched by tests, such as navigation links and commonly-used buttons. If the locator for the element is duplicated in each test, then each time the locator required for that element changes, there could be many tests that need to be updated. So reduce duplication of locators, we can pull locators out of the tests and group them together with other element locators on the page.

Create a Web Page

I start by creating a new Web Page to store the element locator in.

test1-page-home.png

Create the element locator in the page

Next, copy the element locator into the page and give the element an id.

test1-page-home-edit.png

Finally, replace the locator in the test with a reference to the element on the page that I just defined. The expression format for this kind of locator is

<page:"{pageid}.{elementid}">
test1-page-element-reference.png

Now I can reference this element from many different tests and there is only one place to change when I need to update the locator :)

I would repeat this for the search button locator used in the next step.

Return to Tutorials page