Modern Web Development: Locators and Playwright Test Automation

Zeynep Ece Ozcan
Orion Innovation techClub
3 min readMar 22, 2024

The world of web development is rapidly evolving, with new tools and technologies emerging every day. Among these innovations, locators play a crucial role in finding and interacting with web elements. In this article, we’ll delve deeper into the importance of locators and explore how tools like Playwright are used for test automation.

What are Locators?

Locators are tools used to find specific web elements (HTML, CSS, JavaScript, etc.) on web pages. These elements can include buttons, text boxes, links, and other interactive components. Developers use locators to access and interact with these elements, performing actions such as clicking buttons, entering text into input fields, or navigating to links.

Figure : Locator Techniques [4]
Figure 1: Locator Techniques [4]

Types:

1. CSS Locators: Find elements using CSS classes, IDs, tag names, and other CSS properties.

2. XPath Locators: Find elements using XPath, a language for navigating XML documents. XPath specifies element hierarchies and properties.

3. ID Locators: Find a specific element using the “id” attribute in HTML.

4. Name Locators: Find a specific element using the “name” attribute in HTML.

5. Link Text and Partial Link Text Locators: Find links using their complete or partial text content.

Figure 2: Playwright Logo[5]

Playwright Test Automation:

Playwright is a library used for test automation on modern web browsers. It supports popular browsers like Chromium, Firefox, and WebKit, and can run on multiple platforms (Windows, MacOS, Linux). Playwright provides developers with tools to automate interactions performed in the browser, enabling the testing of web applications’ functionality and behavior.

Features of Playwright:

· Strong Locator Support: Playwright supports CSS selectors, XPath expressions, and other locator types, allowing developers to reliably locate elements on web pages and interact with them.

· Cross-Platform Support: Playwright works seamlessly across different operating systems and browsers, ensuring consistent testing environments and reliable results.

· Ease of Use: Playwright offers a simple and intuitive API for writing and managing test scenarios. It provides various functions for configuring browser settings, navigating between pages, and capturing screenshots.

Why are they important?

· Automation Testing: Playwright simplifies automation testing of web applications by leveraging locators to interact with elements. This allows for comprehensive testing of application functionality and performance.

· Continuous Integration and Deployment (CI/CD): Playwright tests can be integrated into CI/CD pipelines, accelerating the development process and improving application quality.

· Debugging: Playwright facilitates rapid detection of errors in web applications by using locators to pinpoint issues. This helps developers ensure the reliability of their applications.

Working with Web Elements in Playwright Test Automation

These are the recommended built-in locators:

•page.getByRole() to locate by explicit and implicit accessibility attributes.

•page.getByText() to locate by text content.

•page.getByLabel() to locate a form control by associated label’s text.

•page.getByPlaceholder() to locate an input by a placeholder.

•page.getByAltText() to locate an element, usually an image, by its text alternative.

•page.getByTitle() to locate an element by its title attribute.

  • page.getByTestId() to locate an element based on its data- namely the testID attribute (other attributes can be configured)
Figure 3: How to use locators in playwright

Conclusion:

Locators and tools like Playwright play a vital role in modern web development and test automation. Using the right locators enables developers to reliably locate and interact with web elements, leading to more effective and efficient test scenarios. Tools like Playwright provide developers with powerful test automation solutions, ultimately enhancing application quality.

References:

1. Playwright Documentation, https://playwright.dev/

2. SeleniumHQ, “Introduction to Selenium Locators”, https://www.selenium.dev/documentation/en/getting_started_webdriver/locating_elements/

3. MDN Web Docs, “Locating DOM Elements Using Selectors”, https://developer.mozilla.org/en-US/docs/Web/API/Document_object_model/Locating_DOM_elements_using_selectors

4. Figure 1, https://www.edureka.co/blog/webelement-in-selenium

5. Figure 2, Playwright logo — retrieved from Google

6. Figure 3, https://playwright.dev/docs/locators

--

--