[Question] How to recognize if the user is not logged in?
See original GitHub issueI am using the Playwright Test with Typescript.
I have implemented the authentication as described in https://playwright.dev/docs/test-auth#reuse-signed-in-state
I am testing the eCommerce app.
I have a case where I want to do the checkout as a guest user (not logged in).
For my test I put test.use({ storageState: { cookies: [], origins: [] } });
in the describe bock and it works like a charm.
But then, because of the POM, I would like to have separate flows based on the fact if the user is logged in or not.
My question is: how to check that? how do I check if I am running the test with the logged-in user or guest customer?
I have done this so far on the cart-page.ts
:
public async clickCheckoutBtn(context: BrowserContext) {
let isLoggedInUser: boolean = await Utils.isLoggedInUser(context);
console.log('isLoggedInUser = ' + isLoggedInUser);
if (isLoggedInUser) {
// Necessary in order for the NEXT button on the Shipment Checkout Form to be clickable
await Promise.all([
this.checkoutBtn.click(),
this.page.waitForURL(/.*\/delivery-address\/add$/), // this is not the flow that happens with the guest customer, so I need to avoid it for the guest user
]);
} else {
this.checkoutBtn.click();
}
}
This is how I check it for now.
public static async isLoggedInUser(
context: BrowserContext
): Promise<boolean> {
let storageState = await context.storageState();
let JSESSIONID = storageState.cookies.filter(
(item) => item.name === 'JSESSIONID'
);
console.log(JSESSIONID);
if (JSESSIONID) return true;
return false;
}
}
But it doesn’t work as I would expect. JSESSIONID is still present in the browser cookies even if I am logged out.
Is there any better mechanism in order to check this?
I also noticed that when doing it manually in the app if I:
- log in -> JSESSIONID is present
- log out -> JSESSIONID is still there (I would expect not to be anymore)
I maybe don’t understand how my app works internally and need to investigate that. But maybe there is some more standard way to check if you are logged in or not?
Issue Analytics
- State:
- Created a year ago
- Comments:6 (4 by maintainers)
https://github.com/microsoft/playwright/tree/main/tests
See the
config
folder and then its siblingsOne note: fixtures are lazy by default, so they will only run and be setup if referred to in your test parameters (either explicitly or as a dependency of another fixture you use)