Rayrun

What are the steps to create a basic test using Playwright?

Answer

Writing Your First Test with Playwright

First, let's import the necessary functions from Playwright's testing library.

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

Now, let's start writing your test. Call the test function and pass in a name for your test and an async function that takes in an object with a page property. This page property represents the browser page that will be used for testing.

test('My First Test', async ({ page }) => {
  // test code goes here
});

Inside this async function, you can perform actions on the webpage and make assertions about its state. For example, navigate to a specific URL and then check if the title of the webpage contains a specific text.

await page.goto('https://ray.run/');
await expect(page).toHaveTitle(/Ray/);

You can also interact with elements on the page, like clicking on a button.

await page.click('button');

Remember, Playwright automatically waits for various actionability checks before performing each action. So, you don't need to manually wait for elements or deal with race conditions when performing checks.

Lastly, always follow best practices when writing tests. Verify user-visible behavior rather than implementation details and keep your tests as isolated as possible from other tests.

Happy testing with Playwright! For more tips, check out Tips for Writing Efficient Playwright Test Scripts.

References

Thank you!
Was this helpful?
Still have questions?

If you still have questions, please ask a question and I will try to answer it.

Related Questions

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.