Unlocking Playwright: Test Reports with Tags and Annotations

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

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

  // 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 = 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');

});


test('invalid username', async ({ page, browserName }) => {

  const username = 'Admin123';
  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 = await page.locator('//button[@type="submit" and contains(@class, "orangehrm-login-button")]').click();

  // Get the text content from the element
  const actualErrorMessage = await page.locator('//p[contains(@class, "oxd-alert-content-text") and text()="Invalid credentials"]').textContent();

  // Print the text
  console.log(`Dashboard text: ${actualErrorMessage}`);

  expect(actualErrorMessage).toContain('Invalid credentials');

});
test.skip('invalid username',  async ({ page, browserName }) => {
//
}

test.fail('invalid username',  async ({ page, browserName }) {
//
}

test.fixme('invalid username',  async ({ page, browserName }) => { 
//
}

It marks the test as slow and triples the test timeout.

test.slow('invalid username',  async ({ page, browserName }) => { 
//
}
test.only('invalid username',  async ({ page, browserName }) => {
//
}
test('skip this test', async ({ page, browserName }) => {
  test.skip(browserName === 'firefox', 'Still working on it');
});
test.describe('login check',() => {
test('valid login', async ({ page, browserName }) => {

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

  await page.goto('https://opensource-demo.orangehrmlive.com/');
  console.log(`Running test on browser: ${browserName}`);

  await page.fill('input[name="username"]', username);
  await page.fill('input[name="password"]', password);

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

  const dashboardText = await page.locator('//h6[contains(@class, "oxd-topbar-header-breadcrumb-module")]').textContent();

  console.log(`Dashboard text: ${dashboardText}`);
  expect(dashboardText).toContain('Dashboard');

});


test('invalid username',  async ({ page, browserName }) => {

  const username = 'Admin123';
  const password = 'admin123'

  await page.goto('https://opensource-demo.orangehrmlive.com/');
  console.log(`Running test on browser: ${browserName}`);

  await page.fill('input[name="username"]', username);
  await page.fill('input[name="password"]', password);

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

  const actualErrorMessage = await page.locator('//p[contains(@class, "oxd-alert-content-text") and text()="Invalid credentials"]').textContent();

  console.log(`Dashboard text: ${actualErrorMessage}`);
  expect(actualErrorMessage).toContain('Invalid credentials');

});

});

test('invalid username',   {
  tag: '@fast',
}, async ({ page, browserName }) => {
//
}

npx playwright test --grep "@fast"

npx playwright test login_page.spec.ts --grep-invert "@fast"
npx playwright test --grep --% "@fast^|@slow"
npx playwright test --grep "(?=.*@fast)(?=.*@slow)"

Leave a comment