Automate Playwright Tests with Command-Line Options

HOME

import { defineConfig, devices } from '@playwright/test';

/**
 * See https://playwright.dev/docs/test-configuration.
 */
export default defineConfig({
  testDir: './tests',

 /* Run tests in files in parallel */
  fullyParallel: true,
 
 /* Reporter to use. See https://playwright.dev/docs/test-reporters */
  reporter: 'html',
 
 /* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
  use: {

    /* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
    trace: 'on-first-retry'

  },

  /* Configure projects for major browsers */
  projects: [
    {
      name: 'chromium',
      use: { ...devices['Desktop Chrome'] },
    },

    {
      name: 'firefox',
      use: { ...devices['Desktop Firefox'] },
    },

    {
      name: 'webkit',
      use: { ...devices['Desktop Safari'] },
    } 

  ]

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

test('has title', async ({ page, browserName }) => {
  await page.goto('https://playwright.dev/');

  // Print the browser name
  console.log(`Running test on browser: ${browserName}`);  

  // Expect a title "to contain" a substring.
  await expect(page).toHaveTitle(/Playwright/);

});

test('get started link', async ({ page, browserName }) => {
  await page.goto('https://playwright.dev/');

  // Print the browser name
  console.log(`Running test on browser: ${browserName}`); 

  // Click the get started link.
  await page.getByRole('link', { name: 'Get started' }).click();

  // Expects page to have a heading with the name of Installation.
  await expect(page.getByRole('heading', { name: 'Installation' })).toBeVisible();
});

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

test('has title', async ({ page, browserName }) => {
  await page.goto('https://opensource-demo.orangehrmlive.com/');

  console.log(`Running test on browser: ${browserName}`);  // Print the browser name

  // Expect a title "to contain" a substring.
  await expect(page).toHaveTitle(/OrangeHRM/);
});


test('valid login', async ({ page, browserName }) => {

  const username = 'Admin';
  const password = 'admin123'

  await page.goto('https://opensource-demo.orangehrmlive.com/');

  // Print the browser name
  console.log(`Running test on browser: ${browserName}`);  

  // Fill in the username
   await page.fill('input[name="username"]', username);

  // Fill in the password
    await page.fill('input[name="password"]', password);

  // Click the login button - Use XPath to locate and click the login button
  const loginButton = page.locator('//button[@type="submit" and contains(@class, "orangehrm-login-button")]');
  await loginButton.click();

  // Check if the page contains text Dashboard - Locate the element using XPath
  const dashboardElement = await page.locator('//h6[contains(@class, "oxd-topbar-header-breadcrumb-module")]');

  // Get the text content from the element
  const dashboardText = await dashboardElement.textContent();
  
  // Print the text
  console.log(`Dashboard text: ${dashboardText}`);
  
  expect(dashboardText).toContain('Dashboard');
});
npx playwright test

npx playwright test login_page.spec.ts

npx playwright test example 

npx playwright test -g "valid login"

npx playwright test --list

How to Run Playwright Tests in UI Mode

HOME

import { defineConfig, devices } from '@playwright/test';

/**
 * See https://playwright.dev/docs/test-configuration.
 */

export default defineConfig({
  testDir: './tests',
  
  /* Run tests in files in parallel */
  fullyParallel: true,
 
  /* Opt out of parallel tests on CI. */
  workers: process.env.CI ? 1 : undefined,
  
  /* Reporter to use. See https://playwright.dev/docs/test-reporters */
  reporter: 'html',
 
 /* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
  use: {

   // Running tests in UI mode
    headless: false
  }

});

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

test('has title', async ({ page, browserName }) => {
  await page.goto('https://playwright.dev/');

   console.log(`Running test on browser: ${browserName}`);  // Print the browser name

  // Expect a title "to contain" a substring.
  await expect(page).toHaveTitle(/Playwright/);
});

test('get started link', async ({ page, browserName }) => {
  await page.goto('https://playwright.dev/');

   console.log(`Running test on browser: ${browserName}`);  // Print the browser nameÓ

  // Click the get started link.
  await page.getByRole('link', { name: 'Get started' }).click();

  // Expects page to have a heading with the name of Installation.
  await expect(page.getByRole('heading', { name: 'Installation' })).toBeVisible();
});

npx playwright test --ui

How to Specify Browsers in Playwright Tests

HOME

import { defineConfig, devices } from '@playwright/test';

/**
 * See https://playwright.dev/docs/test-configuration.
 */
export default defineConfig({
  testDir: './tests',

 /* Run tests in files in parallel */
  fullyParallel: true,
 
 /* Reporter to use. See https://playwright.dev/docs/test-reporters */
  reporter: 'html',
 
 /* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
  use: {

    /* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
    trace: 'on-first-retry'

  },

  /* Configure projects for major browsers */
  projects: [
    {
      name: 'chromium',
      use: { ...devices['Desktop Chrome'] },
    }
  ]

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

test('has title', async ({ page, browserName }) => {
  await page.goto('https://opensource-demo.orangehrmlive.com/');

  console.log(`Running test on browser: ${browserName}`);  // Print the browser name

  await page.waitForTimeout(3000); // Wait for 3 seconds

  // Expect a title "to contain" a substring.
  await expect(page).toHaveTitle(/OrangeHRM/);
});


test('valid login', async ({ page, browserName }) => {

  const username = 'Admin';
  const password = 'admin123'

  await page.goto('https://opensource-demo.orangehrmlive.com/');

  // Print the browser name
  console.log(`Running test on browser: ${browserName}`);  

  // Fill in the username
   await page.fill('input[name="username"]', username);
  
  // Print the username
  console.log(`Logging in with username: ${username}`);

  // Fill in the password
  await page.fill('input[name="password"]', password);

  // Print the password
  console.log(`Logging in with password: ${password}`);

  // Click the login button - Use XPath to locate and click the login button
  const loginButton = await page.locator('//button[@type="submit" and contains(@class, "orangehrm-login-button")]').click();

  // Get the text content from the element
  const dashboardText = await page.locator('//h6[contains(@class, "oxd-topbar-header-breadcrumb-module")]').textContent();
  
  // Print the text
  console.log(`Dashboard text: ${dashboardText}`);
  
  expect(dashboardText).toContain('Dashboard');

});

npx playwright test login_page.spec.ts --project webkit

npx playwright test login_page.spec.ts --project webkit --project firefox

npx playwright test login_page.spec.ts
import { defineConfig, devices } from '@playwright/test';

/**
 * See https://playwright.dev/docs/test-configuration.
 */
export default defineConfig({
  testDir: './tests',

 /* Run tests in files in parallel */
  fullyParallel: true,
 
 /* Reporter to use. See https://playwright.dev/docs/test-reporters */
  reporter: 'html',
 
 /* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
  use: {

    /* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
    trace: 'on-first-retry'

  },

  /* Configure projects for major browsers */
  projects: [
    {
      name: 'chromium',
      use: { ...devices['Desktop Chrome'] },
    },

    {
      name: 'firefox',
      use: { ...devices['Desktop Firefox'] },
    },

    {
      name: 'webkit',
      use: { ...devices['Desktop Safari'] },
    } 
  ]
});

How to install Playwright in Windows?

HOME

How to install node.js on windows 11

npm init playwright@latest

playwright.config.ts         # Test configuration
package.json
package-lock.json            # Or yarn.lock / pnpm-lock.yaml
tests/
  example.spec.ts            # Minimal example test

npx playwright test

npx playwright show-report

Playwright Tutorials

HOME

Advance Selenium Multiple Choice Answers – MCQ1

HOME







Selenium cannot handle security testing on its own. However, it can assist in certain aspects of security testing when integrated with specialized security tools.



















How to read property file in Java

HOME

database.baseUrl = https://localhost
database.port = 8080
database.username = admin
database.password = Admin123
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class ReadPropertiesExample {

    public static void main(String[] args) {

        Properties properties = new Properties();
        try {
            InputStream inputStream = new FileInputStream("src/test/resources/config.properties");

            // Load the properties file
            properties.load(inputStream);

            // Access properties
            String dbUrl = properties.getProperty("database.baseUrl");
            String dbPort = properties.getProperty("database.port");
            String dbUser = properties.getProperty("database.username");
            String dbPassword = properties.getProperty("database.password");

            // Print properties to verify
            System.out.println("Database URL: " + dbUrl);
            System.out.println("Database Port: " + dbPort);
            System.out.println("Database User: " + dbUser);
            System.out.println("Database Password: " + dbPassword);

        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}