Continuation of Scripting with the PageObject File — Playwright Automation Scripting — Part 07
The PageObject file and the PageObjectManager file were created in Part 06. In today’s article, we will use the helper class we created to encapsulate common operations on the page that will be automated. Synchronous vs. asynchronous is also discussed at the end. To access the past content, check this Medium List.
Open Webpage
Before automating the page, it should be opened in the browser. The playwright provides the “goto()” method to open a URL in the browser.
async goto() {
await this.page.goto('https://www.google.com/');
}
Pass Text into Google Search Box
To test the Google search flow, we need to do some searching. The playwright has the “fill()” method, which allows us to send values to text fields. The element SEARCH _BOX, which we declared with XPaths, is used here.
async enterSearchKeyword() {
await this.page.locator(this.SEARCH_BOX).fill("Medium");
}
Click Search Button
Once the keywords have been entered into the search box, the search button should be clicked to perform the search. The method “click()” can be used to click on an element.
async clickSearchButton() {
await this.page.locator(this.SEARCH_BTN).click();
}
Verification of the Search Results Page
Finally, the search results page should be verified in our automation flow. To do this, we can use several methods, but let’s use one of the simplest methods, which is to check the page title. The playwright provides the method “toHaveTitle()” to check the page's title. As you can see, the search results page has “Medium — Google Search” as its title.
async verifyPageIsOpened() {
await expect(this.page).toHaveTitle("Medium - Google Search");
}
Why do we use Async and Await?
Almost all Playwright scripts use the async keyword before each function and await before the statement. It is necessary to use these two keywords in PlayWright to write a test because any tool based on NodeJs is asynchronous, and we have chosen the Playwright Node version.
Synchronous vs. Asynchronous
If the code is synchronous, the program executes the second line as soon as the first line is successfully executed. If the programming is asynchronous, the second line may even be executed before the first line. Suppose we didn’t use the above keywords. Then most of the automation script will fail because of the disordered execution. Therefore, using these keywords in a NodeJs-based framework is a must to ensure execution flow.
Conclusion
We have developed a set of operations in the Google search flow. Here we use some action methods that Playwright provides. We will cover these topics in more depth later, but until then, you can use the following links if you want to master them.
What’s next?
Write Test Specs — Playwright Automation Scripting — Part 08