How to add a new locator to page object model

SergeiSergei Posts: 30
edited October 2019 in Selenic

Page Object Model is an implementation of the Object Repository design pattern that is used to decouple the test logic from the data used by the test. In this pattern the Web pages are represented by classes and various page elements as class member variables, while user interactions with the page are expressed using class methods. Following this pattern results in cleaner, more maintainable Selenium JUnit test code. Parasoft Recorder (a tool inside the Selenic suite) uses Page Object Model when it creates Selenium JUnit tests from a recording. The Selenic IDE plugin relies on this pattern to apply quick fixes to failed or self-healed tests that use bad locators. Although you can use Parasoft Selenic with Selenium JUnits that do not use this design pattern, following it will allow you to get advantage of the full functionality of Parasoft Selenic and increase your productivity.

Suppose you created a Selenium JUnit test with Parasoft Recorder, you have used this test for some time but now your Web page has a new element that needs to be tested. Of course, you can re-record your test, but if the change is relatively small and the test script is long, you can save time by modifying your test manually. In our example the new element will be the “Email:” field of a “Bill Pay” page:

Since we shall be operating on an existing page, we don’t have to create a new Page class. Follow these steps to add and use a new locator to the Page Object Model:

1. Choose the locator:

In the page class (in our case the ParaBankBillPayPage.java) create a new member variable with an appropriate locator. One way to construct the locator is to select the Web element you would like to test in a browser and look for its attributes that you can use in the @FindBy annotation. In Chrome you can right-click on the element and select Inspect. In Firefox right-click on the element and select Inspect Element. In our case the “Email:” text field has a distinct “name” attribute. The rule of thumb is to avoid the using attributes with seemingly random values, such as id="7f0e75ec-8b44-4e0f-869b-5b2704ac838e". You can improve your locators after you run the test – Selenic will offer you optional locators you can use.

2. Add a field with an appropriate locator to the Page class:

@FindBy(name = "payee.contactInformation.email")
private WebElement payeeContactInformationEmailField;

3. Add field access method to the page class:

public void setPayeeContactInformationEmailField(String text) {
waitUntilClickable(payeeContactInformationEmailField).clear();
payeeContactInformationEmailField.sendKeys(text);
}

4. Use the new field in the JUnit test class:

paraBankBillPayPage.setPayeeContactInformationEmailField("[email protected]");

5. Re-run your test and observe the “Email:” field being initialized with the test data.

6. Get alternative locators from Selenic:

If you are using Eclipse open the Selenic launch configuration and select Generate recommendations for All locators. Run the JUnit from the Selenic launch configuration. At the end of the run inspect the Selenic Recommendations view for the locator you added to get alternative locators you can use for the new element:

Sign In or Register to comment.