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:

<dependencies>

   <dependency>

       <groupId>com.microsoft.playwright</groupId>

       <artifactId>playwright</artifactId>

       <version>1.25.0</version>

   </dependency>

<dependency>

   <groupId>org.testng</groupId>

   <artifactId>testng</artifactId>

   <version>7.6.1</version>

   <scope>test</scope>

</dependency>

</dependencies>

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));

               break;

           case "firefox":

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

               break;

           case "safari":

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

               break;

           case "chrome":

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

               break;

           default:

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

               break;

       }

       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 "http://testng.org/testng-1.0.dtd" >
<suite thread-count="2" verbose="10" name="testSuite" parallel="none">

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

        </classes>
    </test>
    <<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>
</suite>

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:

@BeforeTest
    @Parameters("browserName")

    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 qmetry.properties file, as provided.

Don’t forget to update your API key in the qmetry.properties 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.