Web applications have become a mainstay in today’s technological landscape. Whether it’s for e-commerce, communication, or information retrieval, we rely on these web-based platforms for various tasks. Ensuring that these web applications function seamlessly and reliably is an important step in software development. This is where web automation and testing come into play. In this article, we are going to see how Robot Framework, a popular open-source test automation framework, and Playwright, a powerful browser automation library, create a dynamic duo for efficient web testing and browser interaction.
We’ll explore the setup, syntax, and capabilities of this combination, and learn how to create comprehensive test cases for web applications. Whether you’re a seasoned tester looking to enhance your automation toolkit or a developer seeking to streamline testing processes, the combination of Robot Framework and Playwright holds the potential to reinvent your web automation endeavors.
Robot Framework is an open-source and extensible test automation framework used to automate a wide range of tasks, from functional testing of web applications to acceptance test-driven development (ATDD) and it boasts a rich ecosystem of libraries, such as Selenium and Playwright. At its core, Robot Framework employs a keyword-driven approach, making it accessible to individuals with minimal programming experience. Test cases are constructed using plain text files in a tabular format, with a strong emphasis on natural language and readability. Its simplicity allows the collaboration between technical and non-technical team members, as the latter can help in testing endeavors even if they lack the technical knowledge.
Robot Framework encourages practices such as data-driven testing and the use of variables, tags, and reusable keywords to help create well-structured and scalable test suites. The framework also possesses reporting and logging capabilities to ensure the testing process is transparent and easily traceable, aiding in the identification and resolution of issues. Robot Framework is also equipped with comprehensive documentation on its official website and allows the creation of custom libraries tailored to specific project requirements.
Playwright is a browser automation library developed by Microsoft that provides a powerful, cross-browser automation solution for modern web applications, enabling developers and testers to interact with web pages, perform browser-based testing, and extract data with ease and flexibility. Playwright supports automation in major web browsers, including Google Chrome, Mozilla Firefox, and Microsoft Edge. What sets this framework apart is its unified and consistent API that allows testers to write browser automation scripts that work seamlessly across different browser engines.
One of the standout features of Playwright is its ability to interact with web pages in ways that mimic user interactions such as clicking buttons, filling forms, and navigating pages. Also, it can interact with iframes, perform actions within shadow DOM elements, and manage popup dialogs, providing a holistic approach to browser automation. Like Robot Framework, it is compatible with a wide range of programming languages, such as JavaScript, Python, and C#. Its documentation is comprehensive and user-friendly, making it accessible to developers and testers of varying skill levels, while also having an active community and frequent updates to ensure that developers and testers are always equipped with the latest features and fixes.
Before we can start developing tests with Robot Framework and its Playwright library (Browser), Node 18 is required to be previously installed. The framework and the library can then be installed by using pip:
After installing the Playwright library, named Browser Library, it can be initialized by using:
After the installation, create a new file with extension “.robot” using any text editor, like Sublime or VSCode.
The first step when creating a test suite with Robot Framework is to define its settings in the *** Settings *** section:
Library: Specifies the library used in the test. In this case, it’s a Browser for browser automation.
Suite Setup: Used to specify actions that should be performed before the execution of the test cases.
Suite Teardown: Used to specify actions that should be performed after the execution of the test cases.
Documentation: Describes the purpose of the test suite.
The *** Test Cases *** section in Robot Framework is where individual test cases are defined along with the steps or actions that should be executed as part of each test case. This section is a critical part of a test suite, as it outlines the specific scenarios that are to be tested. Let’s take a look at the example below:
The elements present in the test are the following:
Handling Assertions: This is a user-friendly name that describes the purpose of the test case. It also helps identify the test case when the test suite is executed.
[Documentation]: This is an optional section that allows you to provide additional information or a description of the test case.
[Tags]: Tags are optional labels that you can assign to a test case. They are useful for categorizing and organizing different types of test cases.
Test Case Setting: These are the actions that define the steps of your test case. They use keywords to represent operations or interactions with your system, and they are provided by libraries (such as PlaywrightLibrary for browser automation). In this test, we use keywords such as New Page, Type Text, Click, and Take Screenshot.
See the documentation about Browser library keywords (currently 131), here.
We can also take another approach to testing using arguments. To do so, we can add the setting [Arguments]. Then we proceed to define what we want to use as parameters in our test, as follows:
Instead of having the values already defined in the test, we use placeholders in their place (e.g. ${username}) to make the test more flexible and reusable. Then, we can create a new test that uses the previous on as a keyword and customize which values we pass as arguments as we can see below:
Identifying elements is a crucial aspect of web automation, and in the context of the Robot Framework with Playwright, it involves specifying how to locate and interact with HTML elements on a web page. In the previous test, several ways of identifying elements on a web page were displayed:
Example: Type Text input#username ${username}
Example: ${result}= Get Text //td[contains(@class, ‘sales-result’)]
Example: Type Secret id:password ${password}
Example: Click css=.btn.btn-primary:contains(‘Submit’)
After writing the two parts, the test can be executed through a terminal with the following command:
Whether testers or developers choose to use Playwright with Robot Framework or not in their testing procedures, here are some of the most important practices and considerations to have when creating test suites:
Throughout this article, we saw that the integration of Robot Framework with Playwright provides a reliable solution for efficient and readable web automation. The concise syntax of Robot Framework, combined with Playwright’s robust capabilities allows easy cross-browser testing, seamless element identification, and effective synchronization. This integration promotes collaboration and accelerates the development of reliable automated tests, ensuring the delivery of high-quality web applications. While both are powerful frameworks in their own right, complementing the strengths of each one of them provides testers with another useful tool in the ever-growing software testing world.