Welcome back! You've come a long way, and your skills in handling web automation with Playwright are growing. So far, we've navigated web pages and performed simple click actions. Now, let’s dive into another crucial functionality in web automation: filling and submitting forms.
In the previous lesson, you learned to simulate user interactions by clicking a button. Building on that, we'll now focus on automating form interactions, which is essential for tasks like logging in, registering users, or filling out search forms.
In this lesson, you will master the following:
- How to use CSS selectors to target elements.
- Filling out form fields automatically.
- Submitting the form and validating the result.
Before you can fill out and submit a form, you need to identify the input
elements you want to interact with. The input
elements are elements on the page on which you can type text. We already learned how to find elements by their texts, or by mentioning the element tag (we used button
, for instance). But what if there are two fields of type input
not containing any texts? There, we can use CSS selectors, such as id
.
Elements such as input fields often have unique attributes like id
, name
, or class
that help you target them.
In some cases, it is even hard to differentiate if the content is a text or uses some other displayable mechanism. The type of an element can visually seem a button, but it can be another element. That's why it is crucial to understand how to identify properties of elements, such as type, text, or id.
-
Open the Browser's Developer Tools
- Right-click on the page you want to inspect and select "Inspect" or press
F12
.
- Right-click on the page you want to inspect and select "Inspect" or press
-
Navigate to the Elements Tab
- In the Developer Tools, go to the "Elements" tab. Here, you can see the DOM structure of the web page.
-
Inspect the Element
- Hover over the elements in the page or use the element picker tool (a mouse pointer icon) to select the element you’re interested in. This highlights the HTML markup of that element in the Elements panel.
-
Find the ID Attribute
- Look for the
id
attribute in the highlighted HTML markup. If anid
is present, you can use it directly in your Playwright script (e.g.,#username
).
- Look for the
Here’s an example snippet of HTML for a login form:
HTML, XML1<input type="text" id="username" name="username"> 2<input type="password" id="password" name="password"> 3<button type="submit">Login</button>
Let's break down each component:
-
Tags: Tags are the basic building blocks of HTML, used to define elements within the document. In the example,
<input>
and<button>
are tags. The tags define what kind of element you are dealing with; for instance,<input>
is used for input fields, and<button>
is used for buttons. -
Attributes: Attributes provide additional information about HTML elements. For example, the
id
,type
, andname
attributes in the<input>
tags specify unique identifiers for the elements (id="username"
andid="password"
), the type of input (type="text"
andtype="password"
), and the name associated with the input data (name="username"
andname="password"
). -
Text: Text is the content displayed to the user, often found within the opening and closing tags of elements like buttons or headings. For example,
<button type="submit">Login</button>
contains the textLogin
that will be visible to the user on the button.
Using the steps mentioned, you would inspect the input fields and see that they have the id
attributes username
and password
, which you can then use in your Playwright script. Notice that when accessing an element by its id, we add a #
before the id in the selector; for example, #username
will be the selector referring to the element with id username
. This is a requirement for CSS selectors when targeting an element by its id
.
To input text into form fields, we use the fill
method.
Here's the syntax:
TypeScript1await page.fill(selector, value);
selector
: A string representing the CSS selector to identify the element.value
: The string you want to input into the selected element.
Here’s the code example we’ll be working with:
TypeScript1import { test, expect } from '@playwright/test'; 2 3test('fill and submit form', async ({ page }) => { 4 await page.goto('http://localhost:3000/login'); 5 await page.fill('#username', 'user1'); 6 await page.fill('#password', 'pass1'); 7 await page.click('button[type="submit"]'); 8 expect(await page.url()).toContain('/home'); 9});
In this code example, a test is created to automate the process of filling and submitting a form on a login page:
-
Navigates to the login page:
await page.goto('http://localhost:3000/login');
- This line instructs Playwright to open the specified URL in a browser.
-
Fills the username field:
await page.fill('#username', 'user1');
- This line selects the input element with the
id
ofusername
and fills it with the textuser1
.
-
Fills the password field:
await page.fill('#password', 'pass1');
- Similarly, this line selects the input element with the
id
ofpassword
and fills it with the textpass1
.
-
Clicks the submit button:
await page.click('button[type="submit"]');
- This line clicks the button element with the type
submit
to submit the form.
-
Checks the URL after submission:
expect(await page.url()).toContain('/home');
- After the form is submitted, this line verifies that the URL now contains
/home
, indicating that the login was successful and the user has been redirected to the home page.
Forms are everywhere on the web: login forms, search forms, registration forms, and more. Being able to fill and submit forms automatically with Playwright
ensures that you can automate critical user flows in web applications. This skill is invaluable for testing purposes, allowing you to verify that these essential parts of your application work seamlessly.
Imagine running an automated test suite that logs in users across different browsers, ensuring a consistent user experience. By mastering form automation, you'll be well-equipped to build robust tests that handle these common scenarios, giving you confidence that your application behaves as expected.