Playwright with TestNG+bonus QMetry Integration

What is Playwright?

Playwright is a solid, open-source framework for automating websites. It has support for Chromium, Firefox and Webkit within a single API. Some of the advantages of Playwright and why did we choose it, you can read in this article

How to create TestNG tests with Playwright

First of all, we need to install the necessary components. We do this by adding the Maven dependencies in the pom.xml file, in our Maven project:














Playwright Factory class

After the dependencies are imported, we can proceed by creating our PlaywrightFactory class (similar with SingletonDriver for Selenium). 

To do this, we need to take into consideration that the Playwright uses BrowserContexts to operate multiple independent browser sessions and Pages are used to interact with a single tab in the browser. One browser instance can have multiple Page instances. 

Below you can find the PlaywrightFactory class. We declare an init method with a String parameter called ‘browserName’ which we’ll later use it to tell Playwright which browser should our tests run. 

Before we start a new instance, we need to check whether there are any instances still open, and we do this by adding an ‘if’ condition to browser and page. Afterwards, with the use of ‘switch’, we go through cases when the chosen browser is chromium, firefox etc. 

As you can see, when we initialise the browser, we ‘forced’ it to run into non-headless mode, so we can debug our tests and track progress, by calling LaunchOptions().setHeadless(false).

public static Page  initBrowser(String browserName) {

if (browser == null ) {

   if (page == null) {

       switch (browserName.toLowerCase()) {

           case "chromium":

               browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));


           case "firefox":

               browser = playwright.firefox().launch(new BrowserType.LaunchOptions().setHeadless(false));


           case "safari":

               browser = playwright.webkit().launch(new BrowserType.LaunchOptions().setHeadless(false));


           case "chrome":

               browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setChannel("chrome").setHeadless(false));



               System.out.println("Please add the right browser name: chromium, firefox, safari OR chrome ");



       browserContext = browser.newContext();

       page = browserContext.newPage();



Runner xml file

Next, we need to tell Playwright on what browser should our tests run. More than that, we will use this to easily integrate Qmetry for Jira and CI/CD pipelines, on Jenkins / Azure.

To do this, we need to create a Runner.xml file, where we use the parameter declared above.

!DOCTYPE suite SYSTEM "" >
<suite thread-count="2" verbose="10" name="testSuite" parallel="none">

    <test verbose="2" name="ChromeTests" group-by-instances="true">
        <parameter name="browserName" value="chrome" />
            <class name="runners.General" />
           <class name="runners.Discover" />
          <class name="runners.Campaigns" />

    <<test verbose="2" name="FirefoxTests" group-by-instances="true">>
    <       <parameter name="browser" value="firefox" />
    <       <classes>
    <!--            <class name="runners.General" />
           <class name="runners.Discover" />
          <class name="runners.Campaigns" />
    <    </test>

Test runners

Hang on, we are nearly there. For the final touch, we need to create the test runners, where we actually run the tests and generate the reports. To do this, we’ll start creating new java classes under src/test/java. I’ll call mine General, since it contains only the browserName value where tests should run and the login method:


    public void startRun(String browserName) {

        page = PlaywrightFactory.initBrowser(browserName);

    @Test(priority = 1)

    public void login() {

//your tests

As you can see, I’ve added the @BeforeTest to assign a value to the browserName parameter, since we don’t really want that to be included in the final reports, than proceed with initializing the browser.

Bonus integration with Qmetry for Jira

Before we start, we need a couple of things. Log in to your Jira account, go to Apps -> Qmetry, and select Automation tab, like this:

Here, we’ll switch to Maven tab and we need to select our automation framework, then follow the steps provided:

At the end, you should have new dependencies in your pom.xml file + an additional file, as provided.

Don’t forget to update your API key in the file! 🙂

Until next time, happy testing & coding!

Previous post Next post

Step-by-Step Guide: Setting Up Cypress & Testing Login

Read More

Selenium 4 cool new features and highlights

Read More

Comments are closed.