Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Staying logged, not log out after every test #603

Closed
Roobyx opened this issue Aug 24, 2017 · 8 comments
Closed

Staying logged, not log out after every test #603

Roobyx opened this issue Aug 24, 2017 · 8 comments

Comments

@Roobyx
Copy link

Roobyx commented Aug 24, 2017

Heya,
currently after each tests I have writen the cycle refreshes and it goes back to root.
I need to log in the site I am testing and navigate to the page that should be tested every single time, instead of simply staying logged and just start from there.

For an example: I am testing if all the links in the navbar are working properly, I find the link, click it and check if the url is the right one. Then script needs to log in again in order to test the next link (currently they are 30, so you get the point)

I assume there is a way, I just cannot find/think of it.
If anyone has an expirience or an idea, please share it. Thanks !

@brian-mann
Copy link
Member

You're likely just need to change the way you're thinking about this.

For instance, if your have 30 links in the nav, you likely have 30 different pages to test for. You should likely have 30 different spec files, each starting out in a before hook that logs in and visits that page.

It doesn't make sense to have a single test to cover all 30 links if they're all being tested independently.

You also likely don't need to click into each link. You should just test that the HTTP request is successful. A test for this could be as simple as this:

cy.get("nav a").each(($a) => {
  // pluck out the href for each anchor link
  const href = $a.prop("href")

  // make a programatic request to it
  // which will fail if not 2xx response code
  cy.request(href)
})

Doing this kind of thing starts to get close to being anti-patterns in Cypress because we don't really suggest using it as a tool for link spidering. There are much better alternative tools optimized specifically for this task.

That's why I originally suggested you likely don't need a single test that does this - instead write feature specs for each individual feature you're trying to test for and just visit them each in a before hook and preserve cookies so it doesn't log you out between tests.

Last note - don't use your UI to login, you should create a programatic custom command to login to your app which will drastically reduce the time it takes to reach the page you're trying to test.

https://docs.cypress.io/guides/getting-started/testing-your-app.html#Logging-In

@Roobyx
Copy link
Author

Roobyx commented Aug 24, 2017

Thanks for the really extensive and fast response !
I will look into doing it with requests, which makes way more sense.
I have started from the example instead from the guides, so I will check all of it now.

@samyakshah
Copy link

samyakshah commented May 29, 2018

@brian-mann I have created login command for my usecase which essentially sets cookie and logs the test user in. I run that command in before hook but it asks to login every time. How can I fix it?

Also, I have before and beforeEach hooks in the same test.

sorry, instead of opening up an issue i am reusing this thread.

@JohnnyFun
Copy link

JohnnyFun commented Jan 9, 2019

@samyakshah this doc talks about how to use Cypress.Cookies.preserveOnce(), which is likely what you're after.

beforeEach(function () {
    // before each test, we can automatically preserve the
    // 'session_id' and 'remember_token' cookies. this means they
    // will not be cleared before the NEXT test starts.
    //
    // the name of your cookies will likely be different
    // this is just a simple example
    Cypress.Cookies.preserveOnce('session_id', 'remember_token')
  })

https://on.cypress.io/cookies#Preserve-Once

@praneetha-ck-robo
Copy link

@samyakshah this doc talks about how to use Cypress.Cookies.preserveOnce(), which is likely what you're after.

beforeEach(function () {
    // before each test, we can automatically preserve the
    // 'session_id' and 'remember_token' cookies. this means they
    // will not be cleared before the NEXT test starts.
    //
    // the name of your cookies will likely be different
    // this is just a simple example
    Cypress.Cookies.preserveOnce('session_id', 'remember_token')
  })

https://on.cypress.io/cookies#Preserve-Once

I am not using cookies, but using localStorage to set token. How to preserve local Storage?

@jennifer-shehane
Copy link
Member

@praneetha-ck-robo There's an issue open for this #461

@josephshansen
Copy link

@JohnnyFun That was the solution to my issue. Thanks for posting!

@heitrix
Copy link

heitrix commented Jul 24, 2020

@JohnnyFun
IT wooooorks! Thank you! I've been searching this forever

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants