IntelliJ IDEA 2024.1 Help

Selenium Page Objects

Page Object is a design pattern in Selenium that establishes an object repository for storing web elements. Its purpose is to minimize code repetition and enhance the maintenance of test cases.

The typical workflow starts with creating a Page Object file and then populating it with the web elements.

Create Page Object

  1. In the Project tool window, right-click the directory where you want to store the Page Object and select New | Selenium Page Object.

  2. Select the implementation language, framework, and give the Page Object a name.

  3. (Optional) Provide a URL of the web page for which you are creating a Page Object.

    New Selenium Page Object

    If needed, you can customize the Page Object template. For more information, refer to Customize Page Object template.

  4. Click OK.

As a result, a new Page Object is created.

Add elements to the Page Object

  1. Click the Web Inspector button on the right-hand sidebar to open the Web Inspector toolbar window.

  2. Specify the URL of the page in the address bar. This step is optional if you have provided the URL during the previous step.

    Address bar controls
  3. Click The Select Element in Page button and select the element that you want to add to the Page Object in the Web Inspector.

    Select element in page
  4. Once the element is selected, click Add Element to Code to add the element to the code.

    The Add Element to Code button

    To add a specific type of the selector (ID, Name, Tag with classes, and so on), click Add Element to Code By and select the required option.

    Add elements to code by

    If you want to switch specifically between CSS and XPath locators, click and select the required locator type.

    Select locator type

As a result, a piece of code is generated and added to the code editor. Repeat the steps to populate the Page Object with other elements.

Change URL in an existing Page Object

If you want to specify a different web page URL for the Page Object, update the page address in the // page_url comment.

Editing the Page Object

Customize Page Object template

If you want to customize the default contents of the Page Object file (for example, import additional classes), modify the Page Object template:

  1. Press Ctrl+Alt+S, then select Editor | File and Code Templates.

    The File and Code Templates settings
  2. Select the Other tab.

  3. Click Selenium | Page Object Templates | <Required language>

  4. Select the required Page Object template from the list.

    List of Page Object templates
  5. Modify the template.

  6. Click Apply to save changes.

As a result, all newly created Page Objects will contain the provided changes.

Last modified: 11 February 2024