Playwright Interview Questions



1.Is Playwright better than Selenium?
Playwright is generally considered better than Selenium for modern web application testing due to its speed, performance, and ease of use, particularly when testing modern web applications. However, Selenium remains a strong contender due to its wider browser and language support, larger community, and established ecosystem. The “better” choice depends on the specific project’s needs and priorities.
Playwright’s Advantages:
• Faster Performance:
Playwright is designed for speed and efficiency, offering faster test execution and reduced flakiness.
• Modern Features:
Playwright’s architecture is more aligned with modern web development practices, including support for asynchronous programming and easier debugging.
• Unified API:
Playwright provides a unified API across different browsers, simplifying testing and making it easier for beginners.
• Built-in Waits:
Playwright automatically waits for elements to be ready before performing actions, reducing the need for manual waits and improving test reliability.
• Debugging Tools:
Playwright offers powerful debugging tools, including an Inspector and the ability to pause execution and inspect the browser during a test.
Selenium’s Advantages:
• Wider Browser Support:
Selenium supports a wider range of browsers, including legacy browsers, which might be necessary for some projects.
• Larger Community and Resources:
Selenium has a larger and more active community, with extensive documentation and support resources.
• Mature Ecosystem:
Selenium has been around for a longer time and has a more mature ecosystem, with a wide variety of add-ons, plugins, and integrations.
• Multiple Language Support:
Selenium supports a wider range of programming languages, making it suitable for teams using diverse languages.
• Cross-Platform Support:
Selenium supports testing on various operating systems and platforms.
Which to Choose:
• For modern web applications:
If you’re testing modern web applications that rely on JavaScript frameworks and asynchronous programming, Playwright is a strong choice due to its speed, performance, and modern features.
• For projects with legacy browsers or diverse teams:
If you need to support legacy browsers or have a team using a variety of programming languages, Selenium’s wider browser and language support might be more advantageous.
• For projects where community support and documentation are crucial:
If you value a large and active community and extensive documentation, Selenium’s mature ecosystem and resources might be a better fit.

1. What is Playwright?
Playwright is an open-source testing tool, which supports functional, API, and component testing. The playwright is managed by Microsoft.
2. What is the difference between Selenium and Playwright?
The playwright is ready to use a framework one can install and start using. Selenium Provides API/Libraries you need to build the framework
Playwright shipped with in-built assertion libraries Selenium doesn’t provide any assertions, we need to integrate using JUnit or TestNG
The playwright uses the WebSocket connection to communicate with the browser Selenium uses the Webdriver API/HTTP to communicate with the browser
The playwright is faster compared to Selenium Selenium comparatively slower
The playwright doesn’t support the safari stock browser rather it uses the open-source, Webkit browser to test safari Selenium supports Safari
Playwright officially supports Java, Python, .NET C#, TypeScript, and JavaScript. Selenium officially supports Java, Python, C#, Ruby, Perl, PHP, and JavaScript

3.What are the advantages of a Playwright?
Compared to any other framework Playwright has a lot of advantages, as it is a modern solution it’s built on top of the limitation of another framework
• The playwright is easy to install and learn
• Playwright supports Java, Python, .NET C#, TypeScript, and JavaScript.
• It supports both API and end-to-end testing
• Playwright supports Chromium-based browsers, Firefox, Safari(Webkit)
• As Playwright doesn’t use the webdriver API the execution is comparatively faster
• Playwright automatically waits before making any actions where a user doesn’t have to provide implicit or explicit waits
• Playwright allows Network traffic control. Mocks etc.
• Edge case scenarios like File upload and download can be handled easily in playwright
4. Name some disadvantages of Playwright.
• Playwright doesn’t support Mobile automation (They might introduce it in the future)
• Playwright doesn’t support legacy IE Automation
• Playwright doesn’t support Safari stock browser
• Some of the build tools like Teamcity is not directly support
• Some of the features like Ordering, Priority, and Dependancy tests which are available in TestNG are not available in Playwright yet.
5. What are the different testing types the Playwright supports?
Playwright supports functional testing, API testing, and Component level testing.
6. What are the programming languages that the playwright supports
Playwright supports Java, Python, .NET C#, TypeScript, and JavaScript. However, the Typescript/Javascript version of Playwright is more stable and most used.
7. Briefly describe the commands that are used for Playwright installation and Execution of tests
As Playwright supports many programming languages each programming language follows its own installation process.
In this context we are using the Playwright and Javascript we need to use the following commands
Before installation, we need to ensure that NodeJS binaries are installed in our machine and then we can use
npm init playwright@latest
The above command will install the required packages and configurations. Once done we are ready to write the test cases.
npx playwright test
The command is used for executing playwright tests. By default, the playwright executes all the tests that are available for the project.

8. What is a Configuration File in Playwright explain?
As the name indicates the configuration file is the place where we configure the execution behavior of the playwright. The configuration file is typically named playwright.config.ts(/js).
Using the configuration file we can configure headless mode, fullscreen, screenshot options, baseURL, browser options, etc.
9. What is @playwright/test package in Playwright?
The Playwright can be used with different test runners such as Mocha, Jasmine, Jest, etc. Similar way playwright has its own test runner called the playwright test. The playwright test is the default test runner for the playwright.
10. What is Page class in Playwright?
The Page class in playwright is the main class, which provides the methods to interact with the browser. It also provides a set of event handlers that helps to execute a set of actions as soon as the event triggers.
11. How to navigate to specific URLs in Playwright explain with sample tests
const { test, expect } = require(“@playwright/test”);
test.describe(“navigation”, () => {
test(“navigation”, async ({ page }) => {
await page.goto(“https://playwright.dev/”);
});
});
The page.goto() is used for navigating to a specific URL.
The test.describe() hook is used for grouping the set of tests
The test() contains actual tests with playwright commands.
12. What are the different types of reporters that the playwright supports?
The playwright supports different types of reports
• Dot reporter
• Line reporter
• HTML reporter
• JSON reporter
• JUnit reporter
• Custom reporter
In addition to the above playwright also supports allure reporters using third-party plugins.
13. What are the locators in the Playwright list of any five
Locators help to find the elements on the page uniquely at any point in time.
The page class provides the locator function.
• page.getByText() : Find the element that matches the given text
• page.getByRole(): Find the element that matches the role attribute
• page.getByLabel(): Find the element that matches the label
• page.getByTestId(): Find the element that matches the data-testid attribute
• page.getByTitle(): Find the element that matches the title attribute
• page.locator( or ): Find the element by using the CSS or XPath
14. What are the different types of text selectors available in Playwright?
Text-based locators in Playwright are a unique feature, that helps to locate the element which is having specific text
locator.(

44. How to capture Network logs in Playwright?
The playwright provides a way to monitor browser network logs. You can capture all the request and response network logs and their status. Using the listener
page.on(‘request’, request =>
console.log(‘>>’, request.method(), request.url()));
page.on(‘response’, response =>
console.log(‘<<', response.status(), response.url())); await page.goto('https://example.com'); 45. How to capture screenshots in PLaywright? • The Playwright allows taking the screenshot. the page.screenshot() function is provided by Playwright to the screenshot. You can place the screenshot() command anywhere in the code to save the screenshot. • Take the full page screenshot await page.screenshot({ path: 'screenshot.png', fullPage: true }); • Take the Element level screenshot await page.locator('.header').screenshot({ path: 'screenshot.png' }); 46. Does Playwright support API testing? If so how can we perform API testing? Yes, Playwright supports API Testing. We can perform any HTTP API method calls such as GET, POST etc. using the playwright and validate the status and responses. Example: test("Get users", async ({ request, baseURL }) => {
const apiResponse = await request.get(`${baseURL}public/v2/users/`);
expect(apiResponse.ok()).toBeTruthy();
expect(apiResponse.status()).toBe(200);
});
47. What is Visual Testing? Why do we need it?
• Visual Testing is also known as visual comparisons, where two screenshots will be compared. The first screenshot is called the reference or base image, the subsequent run will compare the recent screenshot with reference images and produce the results.
• Visual comparison testing is helpful for UI testing. Using functional testing we will not be able to validate the fonts, styles, typography, alignment, etc. but using the visual comparison we can validate everything related to the application User interface.
48. Write a simple code to Test Visually
For example, if we need to compare the home page we need to write the below code in Playwright.
test(‘Visual test homepage’, async ({ page }) => {
await page.goto(‘https://playwright.dev’);
await expect(page).toHaveScreenshot();
});
• During the first run, the playwright stores the reference image of the homepage, and the next run will be compared against the reference image.
• Optionally we can pass the pixel differences if we need to ignore the minor differences in the image.
test(‘example test’, async ({ page }) => {
await page.goto(‘https://playwright.dev’);
await expect(page).toHaveScreenshot({ maxDiffPixels: 100 });
});
49. How to configure multiple reporters in Playwright?
The playwright allows configuring multiple reporters. The reporter option is available on the playwright.config.js, you can specify the reporter types to configure multiple reporters.
Example:
// playwright.config.js
const config = {
reporter: [
[‘list’],
[‘line’],
[‘json’, { outputFile: ‘test-results.json’ }]
],
};
module.exports = config;
50. What is the serial mode in Playwright?
In some scenarios, tests may be inter dependent. The second test might need the output of the first one. Running tests parallelly in such cases will create the test cases to fail and it’s like a false failure. The serial mode allows running the tests serially one after the another. If one test fails all remaining tests are skipped and can be retried as a group again.
Example:
test.describe.configure({ mode: ‘serial’ });
let page;
test.beforeAll(async ({ browser }) => {
page = await browser.newPage();
});
test.afterAll(async () => {
await page.close();
});
test(‘runs first’, async () => {
await page.goto(‘https://playwright.dev/’);
});
test(‘runs second’, async () => {
await page.getByText(‘Get Started’).click();
});
51. How to perform parallel execution in PLaywright?
• The playwright supports parallel execution. Parallel execution can be achieved at the global level or test level in the playwright.
• Parallel in test file level
• The mode: ‘parallel’ can be passed to describe.configure() function to achieve parallelism.
Example:
test.describe.configure({ mode: ‘parallel’ });
test(‘runs in parallel 1’, async ({ page }) => { /* … */ });
test(‘runs in parallel 2’, async ({ page }) => { /* … */ });
• Parallel option in the playwright config file
• We can mention the fullyParallel option in the configuration file, this makes the tests run parallelly to all tests.
//playwright.config.js
const config = {
fullyParallel: true,
};
module.exports = config;
52. How to perform mobile device emulation in Playwright?
• The emulation features allow testing the application in mobile mode or tablet mode by changing the properties of the browser such as screensize, useragent, geolocation etc.
• For example, if we need to test the mobile safari we can specify the option in the playwright config file like below.
const config = {
projects: [
{
name: ‘Mobile Safari’,
use: {
…devices[‘iPhone 12’],
},
},
],
};
module.exports = config;
• Similarly, we can set the viewport to match the mobile or tablet screen size
const config = {
use: {
viewport: { width: 580, height: 720 },
},
};
module.exports = config;
53. Mention some of the helpful ways to debug Playwright tests.
• The playwright provides multiple ways to debug.
• Using the debug option in the command line.
npx playwright test –debug
• Debug single test
npx playwright test example.spec.ts –debug
VSCode extension
• Apart from the command line debugging option Playwright also provides the VSCode extension “Playwright Test for VSCode”
• Trace on option
• You can also force the Playwright to record the trace by passing the –trace on option.
• Example:
npx playwright test –trace on
Pause option
page.pause() can also be used inside the test script to pause the test and do some debugging.
54. What is actionability in Playwright? Explain in detail
• Playwright architecture has a special type of checks before performing any actions on elements. The checks are called actionability checks.
• For example when you do click operation page.click()
• It will perform many checks internally such as
• Element is attached to DOM
• Element is Visible
• Element is Stable and animation is completed(if any)
• Element is ready to receive the events
• Element is enabled.
This mechanism is also called automatic waiting in the Playwright. Since the Playwright performs all the above checks by default one is no need to perform the above checks manually.
55. Mention some of the advantages of Playwright compared to Cypress
• The Cypress and Playwright share a lot of similarities and Playwright overcomes a lot of limitations that cypress has
• The playwright supports iFrame, Cypress doesn’t
• Playwright supports multiple windows/tabs, Cypress doesn’t
• A playwright can test the cross-domain URLs whereas Cypress doesn’t support
• Playwright supports Safari Webkit browser Cypress doesn’t support Safari
• Playwright supports multiple languages such as Java, Javascript, Python, and C#, the Cypress supports only Javascript/Typescript

Tags: ,

Subscribe now

Receive weekly newsletter with educational materials, new courses, most popular posts, popular books and much more!