How to use Playwright Framework in aiTest

Summary

This How-To provides detailed instructions on how to integrate and use the Playwright Framework for automation testing within aiTest. It covers the setup process, configuration, execution steps, and provides code snippets for reference in JavaScript, Python, and Java.

Supported Languages

  • JavaScript(Node.js)
  • Python
  • Java

How Playwright Framework is used in aiTest

The Playwright Framework enables comprehensive test coverage across different application layers. It focuses on end-to-end testing and supports multiple browsers, allowing you to validate your application’s functionality and user experience.

Steps to Use Playwright Framework in aiTest

  1. Sign Up:
    • Register using your email or Google account.
  2. Navigate to Dashboard:
    • Once signed in, go to the MBT Test dashboard.
    • Select Multi-Browser Test from the left-side menu.
  3. Create a Test:
    • Click the Create Test button.
  4. Configure Your Test:
    • Name the Testrun: Assign a descriptive name to the testrun, e.g., test-example.com, test-demo.com.
    • Project:
      • Select an existing project or create a new one by clicking the green plus sign (+) button.
    • Testrun Description:
      • Write a description to make your testrun more informative.
    • AUT URL:
      • Enter the Application Under Test URL, e.g., https://example.com.
    • Choose Repo Details or Your Automation Code:
      • Repo Details:
        • Provide User’s Git Repository URL, Git Username, Git Token or Password, Report File Location, and Testrun Command.
      • Your Automation Code:
        • Upload your automation code in a zip file.
    • Testrun Command:
      • JavaScript:
        npx playwright test automation.spec.js --project chromium -- --reporter mochawesome --reporter-options reportDir=reports,reportFilename=test-report
        
      • Java:
        mvn clean test
        
      • Python:
        • Headed mode:
          pytest -v -s --json-report --json-report-indent=4 --json-report-file=report/report.json --html-report=./report/report.html --headed .\test_my_application.py
          
        • Headless mode:
          pytest -v -s --json-report --json-report-indent=4 --json-report-file=report/report.json --html-report=./report/report.html .\test_my_application.py
          
    • Report Location:
      • Specify where to store the testrun report files, e.g., Report/Test_Report.html, Report/Test_Report.json, Report/Test_Report.xml.
  5. Select Browsers:
    • Choose the browser(s) for running your test. Options include Google Chrome, Mozilla Firefox, and Microsoft Edge with different versions.
  6. Run or Save the Test:
    • Run It Now:
      • Immediately run the automation code to test browser compatibility.
    • Save:
      • Save the configuration for a later run as a draft testrun.
    • Cancel:
      • Reset and go back to the previous page.
    • Update:
      • Update the configurations and run the new test.

Notes

  • In the Testrun command, specify your file name, e.g., automation.spec.js, which is the file you want to run.
  • AUT code can be accessed in aiTest through:
    • GIT
    • .zip file

Code Snippets for Reference

Refer the code snippet below:

JavaScript

  
  import { test, expect } from '@playwright/test'

test('Login with valid credential', async ({ page }) => {

    await page.goto('https://app.dev.marxeed.com')
    //await page.pause()
    await page.getByRole('banner').filter({ hasText: 'ServicesSupportCredits(...)Site Guide' }).getByRole('button').nth(1).click();
    await page.getByRole('textbox', { name: 'name@host.com' }).click();
    await page.getByRole('textbox', { name: 'name@host.com' }).fill('rohan.thakre@appliedaiconsulting.com');
    await page.getByRole('textbox', { name: 'Password' }).click();
    await page.getByRole('textbox', { name: 'Password' }).fill('Sulochana@1997');
    await page.getByRole('button', { name: 'submit' }).click();
})
 
test('login with invalid password', async ({ page }) => {
    await page.goto('https://app.dev.marxeed.com')
    //await page.pause()
    await page.getByRole('banner').filter({ hasText: 'ServicesSupportCredits(...)Site Guide' }).getByRole('button').nth(1).click();
    await page.getByRole('textbox', { name: 'name@host.com' }).click();
    await page.getByRole('textbox', { name: 'name@host.com' }).fill('rohan.thakre@appliedaiconsulting.com');
    await page.getByRole('textbox', { name: 'Password' }).click();
    await page.getByRole('textbox', { name: 'Password' }).fill('Asdhdjs@1997');
    await page.getByRole('button', { name: 'submit' }).click();
    await page.getByRole('paragraph').filter({ hasText: 'The username or password you entered is invalid' }).click();
  
})
  

Python

  
import playwright
from playwright.sync_api import Page, expect

#Login with Vaid Details
def test_example(page: Page) -> None:
    page.goto("https://app.marxeed.com/")
    page.goto("https://app.marxeed.com/home")
    page.get_by_role("banner").filter(has_text="ServicesSupportCredits(...)Site Guide").get_by_role("button").nth(1).click()
    page.get_by_role("textbox", name="name@host.com").click()
    page.get_by_role("textbox", name="name@host.com").fill("rohan.thakre@appliedaiconsulting.com")
    page.get_by_role("textbox", name="Password").click()
    page.get_by_role("textbox", name="Password").fill("Rohan@1997")
    page.get_by_role("button", name="submit").click()
    page.locator("html").click()

#Login with Invalid details
def test_invalid(page: Page) -> None:
    page.goto("https://app.marxeed.com/")
    page.goto("https://app.marxeed.com/home")
    page.get_by_role("banner").filter(has_text="ServicesSupportCredits(...)Site Guide").get_by_role("button").nth(1).click()
    page.get_by_role("textbox", name="name@host.com").click()
    page.get_by_role("textbox", name="name@host.com").fill("rohan.thakre@appliedaiconsulting.com")
    page.get_by_role("textbox", name="Password").click()
    page.get_by_role("textbox", name="Password").fill("Rhdo@1799")
    page.get_by_role("button", name="submit").click()
    page.locator("html").click()
  

Java

  
package com.java.playwright;

import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import com.aventstack.extentreports.reporter.ExtentSparkReporter;
import com.microsoft.playwright.*;
import com.microsoft.playwright.options.AriaRole;
import org.junit.jupiter.api.*;

public class TestDashboardpage {
    static ExtentReports extent;
    ExtentTest test;
    private static BrowserType.LaunchOptions browserOptions;
    private static String browserName;
    private static Playwright playwright;
    private static Browser browser;
    private BrowserContext context;
    private Page page;

    @BeforeAll
    static void globalSetup() {
        extent = new ExtentReports();
        ExtentSparkReporter htmlReporter = new ExtentSparkReporter("target/extent-report.html");
        extent.attachReporter(htmlReporter);
        ExtentSparkReporter sparkReporter = new ExtentSparkReporter("target/extent-report.json");
        extent.attachReporter(sparkReporter);

        // Set browser options
        browserOptions = new BrowserType.LaunchOptions().setHeadless(true);

        // Read the browser name from the system property
        browserName = System.getProperty("browser", "chromium");
        System.out.println("Browser specified: " + browserName);

        playwright = Playwright.create();
        switch (browserName.toLowerCase()) {
            case "chromium":
            case "chrome":
                browser = playwright.chromium().launch(browserOptions);
                break;
            case "firefox":
                browser = playwright.firefox().launch(browserOptions);
                break;
            case "edge":
                browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setChannel("msedge"));
                break;
            default:
                throw new IllegalArgumentException("Unsupported browser: " + browserName);
        }
    }

    @AfterAll
    static void globalCleanup() {
        if (browser != null) {
            browser.close();
        }
        if (playwright != null) {
            playwright.close();
        }
        extent.flush();
    }

    @BeforeEach
    void setup() {
        context = browser.newContext();
        page = context.newPage();
    }

    @AfterEach
    void cleanup() {
        if (context != null) {
            context.close();
        }
    }

    @Test
    void shouldClickButton() throws InterruptedException {
        test = extent.createTest("Marxeed Logo");
        test.log(Status.INFO, "Test started");

        try {
            page.navigate("https://app.marxeed.com/");
            page.navigate("https://app.marxeed.com/home");
            page.getByRole(AriaRole.BANNER).filter(new Locator.FilterOptions().setHasText("ServicesSupportCredits(...)Site Guide")).getByRole(AriaRole.BUTTON).nth(1).click();
            page.getByRole(AriaRole.TEXTBOX, new Page.GetByRoleOptions().setName("name@host.com")).click();
            page.getByRole(AriaRole.TEXTBOX, new Page.GetByRoleOptions().setName("name@host.com")).fill("omkar.jodh@appliedaiconsulting.com");
            page.getByRole(AriaRole.TEXTBOX, new Page.GetByRoleOptions().setName("Password")).click();
            page.getByRole(AriaRole.TEXTBOX, new Page.GetByRoleOptions().setName("Password")).fill("Omkar@12345");
            page.getByRole(AriaRole.BUTTON, new Page.GetByRoleOptions().setName("submit")).click();

            String currentURL = page.url();
            System.out.println("Current page url: " + currentURL);
            Thread.sleep(2000);
            String currentTitle = page.title();
            System.out.println("Current Page Title: " + currentTitle);

            if (currentTitle.equals("Signin")) {
                test.log(Status.PASS, "Test Passed");
            } else {
                test.log(Status.FAIL, "Test Failed");
            }
        } catch (Exception e) {
            e.printStackTrace();
            test.log(Status.FAIL, "Test Failed");
        }
    }

    @Test
    void validatetitletext() throws InterruptedException {
        test = extent.createTest("Marxeed Title Text");
        test.log(Status.INFO, "Test started");

        try {
            page.navigate("https://app.marxeed.com/");
            page.navigate("https://app.marxeed.com/home");
            Locator logoElement = page.locator("img[alt='Marxeed Logo']");
            logoElement.click();

            String currentURL = page.url();
            System.out.println("Current page url: " + currentURL);
            Thread.sleep(2000);
            String currentTitle = page.title();
            System.out.println("Current Page Title: " + currentTitle);

            if (currentTitle.equals("marXeed")) {
                test.log(Status.PASS, "Test Passed");
            } else {
                test.log(Status.FAIL, "Test Failed");
            }
        } catch (Exception e) {
            e.printStackTrace();
            test.log(Status.FAIL, "Test Failed");
        }
    }

    @Test
    void Click_on_continue_with_google_Button() throws InterruptedException {
        test = extent.createTest("Click on Continue with Google Button");
        test.log(Status.INFO, "Test started");

        try {
            page.navigate("https://app.marxeed.com/");
            page.navigate("https://app.marxeed.com/home");
            page.getByRole(AriaRole.BANNER).filter(new Locator.FilterOptions().setHasText("ServicesSupportCredits(...)Site Guide")).getByRole(AriaRole.BUTTON).nth(1).click();
            Locator googleButton = page.locator("(//button[@name='googleSignUpButton'])[2]");
            googleButton.click();

            String currentURL = page.url();
            System.out.println("Current page url: " + currentURL);
            Thread.sleep(4000);
            String currentTitle = page.title();
            System.out.println("Current Page Title: " + currentTitle);

            if (currentTitle.equals("Sign in - Google Accounts")) {
                test.log(Status.PASS, "Test Passed");
            } else {
                test.log(Status.FAIL, "Test Failed");
            }
        } catch (Exception e) {
            e.printStackTrace();
            test.log(Status.FAIL, "Test Failed");
        }
    }

    @Test
    void Click_on_ai_generated_blog_Button() throws InterruptedException {
        test = extent.createTest("Click on ai generated blog");
        test.log(Status.INFO, "Test started");

        try {
            page.navigate("https://app.marxeed.com/");
            page.navigate("https://app.marxeed.com/home");
            page.getByRole(AriaRole.BANNER).filter(new Locator.FilterOptions().setHasText("ServicesSupportCredits(...)Site Guide")).getByRole(AriaRole.BUTTON).nth(1).click();
            Locator googleButton = page.locator("//*[@id=\"aigeneratedblogs\"]/div");
            googleButton.click();

            String currentURL = page.url();
            System.out.println("Current page url: " + currentURL);
            Thread.sleep(4000);
            String currentTitle = page.title();
            System.out.println("Current Page Title: " + currentTitle);

            if (currentTitle.equals("AI Generated Blogs")) {
                test.log(Status.PASS, "Test Passed");
            } else {
                test.log(Status.FAIL, "Test Failed");
            }
        } catch (Exception e) {
            e.printStackTrace();
            test.log(Status.FAIL, "Test Failed");
        }
    }
    
    @Test
    void Click_on_trending_blog() throws InterruptedException {
        test = extent.createTest("Click on trending blog");
        test.log(Status.INFO, "Test started");

        try {
            page.navigate("https://app.marxeed.com/");
            page.navigate("https://app.marxeed.com/home");
            page.getByRole(AriaRole.BANNER).filter(new Locator.FilterOptions().setHasText("ServicesSupportCredits(...)Site Guide")).getByRole(AriaRole.BUTTON).nth(1).click();
            Locator googleButton = page.locator("//*[@id=\"trendingBlogs\"]/div/p");
            googleButton.click();

            String currentURL = page.url();
            System.out.println("Current page url: " + currentURL);
            Thread.sleep(4000);
            String currentTitle = page.title();
            System.out.println("Current Page Title: " + currentTitle);

            if (currentTitle.equals("marXeed")) {
                test.log(Status.PASS, "Test Passed");
            } else {
                test.log(Status.FAIL, "Test Failed");
            }
        } catch (Exception e) {
            e.printStackTrace();
            test.log(Status.FAIL, "Test Failed");
        }
    }

    public static void main(String[] args) {
        if (System.getProperty("browser") == null) {
            System.setProperty("browser", "chromium");
        } else {
            System.out.println("Browser from system property: " + System.getProperty("browser"));
        }
    }
}
  

Questions Answered

  • How Playwright Framework is going to be used in aiTest?
×

Subscribe

The latest tutorials sent straight to your inbox.