question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] How to recognize if the user is not logged in?

See original GitHub issue

I 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:

  1. log in -> JSESSIONID is present
  2. 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:closed
  • Created a year ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
rwollcommented, Jul 4, 2022

https://github.com/microsoft/playwright/tree/main/tests

See the config folder and then its siblings

1reaction
rwollcommented, Jul 4, 2022

One 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)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Track a user that is not logged in - Stack Overflow
I'm a newbie trying to create a website, I have to track user before logged in like this example site, I'm using php...
Read more >
How to detect a user that is not logged in
I was thinking that checking if $COOKIE[TEST_COOKIE] is set, but I am uncertain. Is this a good method? Is there a better cookie...
Read more >
Frequently asked questions - Zoom Support
Do you need an account to use Zoom? A Zoom account is not required if you are strictly joining Zoom Meetings as a...
Read more >
How to log in to your Marketplace account - HealthCare.gov
When you created your account, you selected security questions that only you know the answers to. If you forget your password or username,...
Read more >
Troubleshoot Snapchat Login Issues
Double-check that you're using the right username and password. If the Snapchat username or password is misspelled, then you may see the 'User...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found