GyaaniGuy

- self made 💻 😎

Automation with playwright - Part 4 Running tests

Playwright has 2 ‘modes’

  • regular mode: where you have fine grained control over the browser.
  • tests: Useful for running test case scenarios.

Creating and Running ‘tests’

Project Setup & Running

  • Install Playwright
  • Create a new folder tests
  • Create a file .spec.js file in the tests folder.

Full Example

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

test('Locators', async ({page}) => {
    await page.goto('https://gyaaniguy.top');
    let linkedInLink = await  page.locator('//a[text()="LinkedIn"]');
    
    await expect(linkedInLink).toBeVisible();
    await expect(page).toHaveTitle('STORE');
    await expect(page).toHaveURL('https://gyaaniguy.top');


    await page.close();
})

Code explanation:

We have a test block for each test. Playwright will open a browser and provide this block with a page. We then run navigate, click around and check for expected output and results.

  • page.goto(url): Navigates to the URL.
  • **await expect(linkedInLink).toBeVisible() : wait for link to be visible (location assertion)
  • expect(page).toHaveTitle(expectedTitle): Asserts the title.
  • expect(page).toHaveURL(expectedURL): Asserts the URL.
  • page.close(): Closes the page.

Running Tests from Terminal:

  • All tests: npx playwright test (headless, all browsers).
  • Specific file: npx playwright test homepage.spec.js
  • Specific browser: npx playwright test homepage.spec.js --project chromium
  • Headed mode: npx playwright test homepage.spec.js --project chromium --headed
  • Debug mode: npx playwright test homepage.spec.js --project chromium --headed --debug

Viewing Reports:

npx playwright show-report

dated April 2025