How to Get the Current Page URL Using page.url() in Playwright

Get the Current Page URL Using page.url() in Playwright

Learn how to get the current page URL in Playwright using page.url() in JavaScript. Step-by-step code examples and real-world use cases for test automation.

In real-world playwright test automation, you need the current page URL to verify navigation, perform conditional actions, or debug your script. You can use the page.url() method to get the current page’s URL in JavaScript.

Why You Need to Get the Current URL in Playwright

You need the current page URL in Playwright to validate the redirection after login or navigation, to check if you are on the correct page before taking action, to debug failed test cases, or to create conditional flows based on the URL.

Playwright: Get Current Page URL in JavaScript

In JavaScript or TypeScript, you can use the page.url() method to get the current page URL.

JavaScript Example: Get Current URL in Playwright

const { test, expect } = require('@playwright/test');

test('Get current page URL', async ({ page }) => {
  await page.goto('https://www.google.com/');
  const newString = page.url();
  console.log("Current Page URL is: "+newString);   
});
JavaScript code example showing how to get the current page URL using Playwright.

Code Breakdown:

  • const newString = page.url(): This syntax will return the full URL of the page as a string and store it in the variable.
  • console.log(“Current Page URL is: “+newString): It will print the URL in the console.

How to Use URL in Playwright Assertions

You can also use the current URL to verify if the navigation was successful.

expect(page.url()).toBe('Expected url');

How to Compare Actual and Expected URL in Playwright

Once you’ve navigated to a page, you might want to assert that the current page URL matches the expected URL. This is useful for verifying:

  • Successful login or logout
  • Redirects after form submission
  • Navigation to the correct page after a button click

Let’s see how to compare the actual URL with the expected one in JavaScript.

JavaScript Example: Compare URLs in Playwright

const { test, expect } = require('@playwright/test');

test('Get current page URL', async ({ page }) => {
  await page.goto('https://only-testing-blog.blogspot.com/2025/04/playwright-practice-page.html');
  await page.getByRole('link', { name: 'Go to Home Page' }).click();
  const actualUrl = page.url();
  const expectedUrl = 'https://only-testing-blog.blogspot.com/';
  if (actualUrl === expectedUrl) {
    console.log('✅ URL matched:', actualUrl);
  } else {
    console.error('❌ URL mismatch!');
    console.log('Expected:', expectedUrl);
    console.log('Actual  :', actualUrl);
  }
});

In the above example, we have compared actual and expected URLs and printed the comparison result in a console.

Best Practices for Comparing URLs

  • Always use full URLs when possible for accuracy.
  • You can use page.waitForURL() to wait for navigation before checking the URL.
  • If the URL contains dynamic parts (e.g., query parameters), you can consider using regular expressions or a partial match.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *