I have a React component with an onClick listener that looks like this:
<button
className='h-10 min-h-[2.5rem] m-2 rounded-lg border-2 text-white bg-violet-400 border-white'
id='logout-cta-button'
onClick={logout}
>
Log Out
</button>
The logout function referenced here, is defined in the same file as follows:
function logout() {
fetch('api/logout')
.then(() => {
document.cookie = 'token=; expires=Fri, 1 Jan 2000 0:00:00 UTC; path=/';
window.location.assign('/login');
})
.catch(err => {
console.error("Unable to log out for some reason.", err.toString());
})
}
The API shouldn't be relevant here, all you need to know is that it returns 200.
I have a Playwright test that calls for a click on the button as follows:
expect(page.locator('#logout-cta-button')).toBeTruthy();
await page.locator('#logout-cta-button').click();
await page.waitForURL('http://localhost:3000/login');
This Playwright test passes for Chromium and Webkit. But in Firefox, the redirect call in the logout function seems to be ignored resulting in the test failing. What might I be missing?
Try adding a
/to the end of the URL in yourwaitForURLcall. From the docs forwaitForURL:Here's a minimal, reproducible example without React, which is probably incidental to the problem (although if you're using React Router, the typical redirect is not using
window). I'm using Chromium, so it doesn't exactly match your situation exactly, but may still resolve it.index.html:
login/index.html:
pw.test.js:
Start a web server:
python -m http.server 3000, then navigate in your browser to http://localhost:3000 and click the button to make sure the redirect works as expected.Run the test and observe the failure:
Next, change the line
to
Run the test again and observe success:
Having shown the probable issue and fix, perhaps a better cross-browser approach is to use a regex: