Welcome to the second in the series of Playwright questions. I had published the part 1 of the series earlier and received a lot of requests to publish the part-2 of this series. So, please find below the questions in the second series –
How protractor and playwright are related ?
Ans: Playwright and Protractor are not related at all. Protractor was an extension of the Selenium Webdriver, and used the underlying Webdriver related architecture to handle stuff. Playwright is completely independent of that. Playwright uses the Chrome Developer Protocol and Websockets(CDP) instead.
Playwright has a lot of similarities with Puppeteer. In fact, the same team which developed Puppeteer, was behind the creation of Playwright at Microsoft.
Is it mandatory to have nodeJS for playwright ?
Since Playwright comes in different language bindings, it is not neccessary to have nodeJS on the system if you’re using any other language binding that JavaScript.
If you’re using JavaScript/TypeScript bindings, then only you need nodeJS installed in your system.
Which selector is used in playwright ?
Ans: Playwright uses a lot of different selectors. It supports the normal CSS selector locators, xpaths too. Moreover, there are a lot of intelligent selector mechanisms that allow us to generate less brittle locators for the e2e test cases.
You can read about these on the documentation section.
What is browser context in playwright ?
In Playwright, a browser context is used to create an independent browser session. We can use multiple browser contexts to create separate browser sessions in a single Playwright test.
Browser Context extends the EventEmitter
class of nodeJS.
You can use the newContext()
method to create a new browser context. This created browser context is opened in-cognito.
1 | const context = await browser.newContext(); |
What is page in playwright ?
A page, with a single browser context could mean a single browser tab. When you are testing a web page, in order to interact with elements, you need to create a new page, within that said browser context, and then you can interact with elements on a page.
const context = await browser.newContext(); const page = await context.newPage(); |
When is browser context destroyed in playwright ?
Ans : When ever you call the close
method on a browser context, then it will be destroyed.
Documented here.
Is it difficult to use Xpath with playwright ?
Ans: Absolutely not. You can use xpath with Playwright in the same way as in Selenium tests. You can either specify the xpath
selector engine in the locator or ignore it in most cases.
So
1 | await page.locator( "//div/i[contains(@class,'xpath-search')]" ).click() |
would work as fine as
1 | await page.locator("xpath=//div/i[contains(@class,'xpath-search')]").click() |
However, if you select an absolute xpath, then it is mandatory to give the xpath
engine
So this won’t work –
1 | await page.locator("/html//div/i[contains(@class,'xpath-search')]").click() |
But this will
1 | await page.locator( "xpath=/html//div/i[contains(@class,'xpath-search')]" ).click() |
Can I integrate HTML reports in Playwright?
Ans: Yes. Playwright gives it own very customised and beautiful HTML report. Here is the documentation for that.
Also, if you want to integrate with third party reporting tool like Allure, that it is also possible. You can see this tutorial.
I want a video of the test execution. Can I generate that in Playwright?
Ans: Yes. It is very easy to configure Playwright to record the test execution. You can define the video
key in the config
file, and then set it to the 4 pre-defined options for taking video
off
– Do not take any video of test execution. This is the default setting.on
– Take video of all the tests getting executed.retain-on-failure
– Take video of all tests, and only retain for the failed ones.on-first-retry
– Take video only when retrying the test for the first time.
Sample code block
1234567 | const config = { use: { video: 'on-first-retry' , // or any other options from list above }, }; module.exports = config; |
How can I do debugging in case of failures in Playwright tests?
Ans: Playwright has some very good debugging features available when someone wants to debug the failures –
- Using the
--debug
param when running the tests.
Using --debug
with the test command in the playwright tests will help in the debug of the failures
1 | npx playwright test --debug |
Using this command will open a Playwright Inspector, where user can see the step by step execution.
- Using the
PWDEBUG
param in tests
When running the playwright tests, you can do PWDEBUG=1
for running the tests in debug mode, which will open the Playwright Inspector.
1 | PWDEBUG=1 npx playwright test |
If you want to debug in console, then you can do
1 | PWDEBUG=console npx playwright test |
- Pausing the execution
If you want to pause the execution of the test scripts for some time, you can use page.pause()
method to pause the page execution.
There are other methods for debugging also, like actionability logs, etc. You can see the whole list here and also learn about the above mentioned methods in detail.
These are some questions for Playwright interviews. See you in the next part in couple of days. In that we’ll see questions about setting up Playwright and various configurations.