We are back with another 1hour tutorial ๐. This time we will be creating Playwright and Typescript page object model framework. For this we will be using Visual Studio Code editor. Hope you have a basic idea on Playwright. There are plenty of resources you can refer to get the basic idea of the Playwright. In this tutorial we will be focussing more on how to implement Page Object model in Playwright. So let's begin.
Before all of this, you must need to have node js installed. I hope you have already done that.
1. Download the relevant nodejs packages by using following command
npm init playwright@latest
2. Lets create our project structure. Unlike Selenium 1 hour lesson for this we will be creating just two folders. To write page objects we use "pages" folder and to write tests we create "tests" folder. Just for now, run the default tests which Playwright automatically create with its installation.
use npx playwright test command in your CMD.
By default every tests run in headless mode. So if you want to run in head full mode, you have to change the playwright.config.ts config file.
add headless: false
inside use: {} playwright.config.ts file.
3. After step 2, you should have following like folder structure. Make sure to delete the tests/example.spec.ts file since we don't need it anymore.
So we include all of our locators as variables and its functional implementation in the bottom. Now you may be wondering unlike Selenium, Playwright locators are different. So how did i catch these locators ๐คจ. It's really simple.
There is an already inbuilt feature come with Playwright which allows us to record all the actions we do, this is really helpful for the users who do Web Scraping. So from that you can catch all the locators easily. I'll show you how,
first you need to run the following command.
npx playwright codegen
It will open a separate simple web browser with a widget, what you have to do is just enter your url and do all the operations. Then this small widget kind of application will record all your steps and gives you a script.
This is one of the sample auto generated scripts
So I have used page.locator('[data-test="username"]') inside a variable.
This is faster and much more easier than selenium right ๐ค?
Same way we need to create a separate page class for the Dashboard page since we are independent from one class to another class. So lets create it.
5. Now lets create a tests file.
In here we have implemented two methods to check a happy path and a error scenario. Unlike Selenium we don't need any third party testing framework like Testng. It has it's own way to assert. You can learn more on Asserts with Playwright documentation.
Now for the moment go to your playwright.config.ts file and comment all the browsers other than chrome. Now when you run the Playwirght tests, it will only get executed in Chrome browser only. This is just to focus on one browser for now. If you want you can run on all these browsers.
Now lets run the tests by command npx playwright test in the terminal/cmd.
There will be automatically generated report opened in your browser after the execution but we will add Allure report in few minutes.
Basically thats it. So guys. We have implemented our framework.
Now lets focus on other inbuilt features which comes with Playwright.
- Taking screenshots.
This is really easy to implement. Just put screenshot: "only-on-failure", inside use. You want to to get it on failure right? Then you have to use "only-on-failure". There are other times you can use it, you can find them in the Playwright documentation.use: {/* Base URL to use in actions like `await page.goto('/')`. */// baseURL: 'http://127.0.0.1:3000',/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */trace: 'on-first-retry',/* Headful mode execution */headless: false,/* Taking screenshots on failure */screenshot: "only-on-failure",},
Just test this out, by creating an assertion error by your self.
As you can see, failed scenario will be saved in a screen shot under test-results folder.
- Taking Video on failures.
Unlike Selenium you can easily record the failures, having failures record like that makes easy to debug without even running the automation suite.
So lets implement that. This happens too in the playwright.config.ts file. You have to add video: "retain-on-failure" inside use. You don't want to record successful scenarios right? It will take so much space on your computer or server. So with this retain-on-failure it will just keep the recording only on failure.use: {/* Base URL to use in actions like `await page.goto('/')`. */// baseURL: 'http://127.0.0.1:3000',/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */trace: 'on-first-retry',/* Headful mode execution */headless: false,/* Taking screenshots on failure */screenshot: "only-on-failure",/*Saving video on failure */video: "retain-on-failure"},
Github link : https://github.com/iroshanAV/simple-pom-pw
Comments
Post a Comment