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 !
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
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.
@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.
@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')
})
@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') })
I am not using cookies, but using localStorage to set token. How to preserve local Storage?
@praneetha-ck-robo There's an issue open for this https://github.com/cypress-io/cypress/issues/461
@JohnnyFun That was the solution to my issue. Thanks for posting!
@JohnnyFun
IT wooooorks! Thank you! I've been searching this forever
Most helpful comment
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
beforehook 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:
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
beforehook 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