Rayrun

How do I verify a URL using @playwright/test?

Answer

Verifying a URL with @playwright/test

To validate a URL using @playwright/test, you can create a test that navigates to the desired URL and then checks if the current URL matches the expected one. Here's a step-by-step guide:

  1. First, install the necessary dependencies:

    npm install -D @playwright/test
  2. Create a test file, e.g., url-validation.spec.ts.

  3. Write a test that navigates to the target URL and compares the current URL with the expected one:

    import { test, expect } from '@playwright/test';
    
    test('Validate URL', async ({ page }) => {
      // Navigate to the target URL
      await page.goto('https://ray.run/');
    
      // Get the current URL
      const currentUrl = page.url();
    
      // Check if the current URL matches the expected one
      expect(currentUrl).toBe('https://ray.run/');
    });
  4. Run the test using the playwright test command:

    npx playwright test

This test will navigate to the https://ray.run/ website and verify if the current URL matches the expected one. If the URLs match, the test will pass; otherwise, it will fail.

For more tips and best practices on writing efficient @playwright/test scripts, check out Tips for Writing Efficient Playwright Test Scripts.

Thank you!
Was this helpful?
Still have questions?

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

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.