facebook / memlab

A framework for finding JavaScript memory leaks and analyzing heap snapshots
https://facebook.github.io/memlab/
MIT License
4.35k stars 118 forks source link

How to run beforeInitialPageLoad conditionally? #61

Closed saxenarajat closed 1 year ago

saxenarajat commented 1 year ago

I can run the login logic in beforeInitialPageLoad but the session is maintained in the subsequent runs. Is there any way to either:

OR

Thanks for this wonderful project. Loving it so far.

JacksonGL commented 1 year ago

Hi @saxenarajat, please check out the answers below:

Run beforeInitiaPageLoad only when the session is not found

Could you explain a little bit about the use case for this? One thing to try is to invoke await page.goto(url) inside beforeInitiaPageLoad and see if it can establish a session (although the session would be different from the session created from url).

how to check if the user already has a session

One way is to check the user session in the action callback via the page argument.

Clear the session so that beforeInitialPageLoad can work without interruption.

beforeInitialPageLoad is the optional callback before establishing a session, not sure what interruption means in this context. Could you elaborate more on your use cases and what problem you are trying to solve?

saxenarajat commented 1 year ago

Thanks @JacksonGL for replying,

So I am trying to test a protected route in my app. If a user is not signed in, she would be redirected to the login screen before she could access that route. I added the logic to log in (searching of email/password fields and filling in the details) in the beforeInitialPageLoad method and it works fine.

However, when I re-run the scenario subsequently, it breaks as the user already have a session and is not redirected to the login screen.

JacksonGL commented 1 year ago

Thanks for sharing the context @saxenarajat

I would recommend using the cookies callback and void logging in if possible.

As an alternative for re-running the scenario, the beforeInitialPageLoad could use Puppeteer APIs to check the url of the redirected page, if the user is not redirected to the login page, just skip the login logic.