https://cypress.io logo
Hi, Just a simple question :)
# i-need-help
b
How can I stay on the same WEB page in different tests. it('test_one', () => { cy.visit('www.google.com'); window.localStorage.setItem('test_one', 'test'); }) it('test_two', () => { //How can this second test of mine start already on the google page and with the things //I saved in the previous test in local storage? })
a
According to our docs, having tests rely on the state of previous tests is considered an anti-pattern. Tests should always be able to be run independently from one another and still pass. That being said you could create a Custom command using LocalStorage. I found an article here that provide a few options: https://reflect.run/articles/how-to-save-and-restore-state-in-cypress-tests/
Also, if you're unfamiliar with Custom commands, here are the Cypress docs: https://docs.cypress.io/api/cypress-api/custom-commands#docusaurus_skipToContent_fallback
b
Thanks, sorry but i'm still exploring cypress now. I must do a test for each feature? @adventurous-dream-20049
a
@busy-soccer-31234 I'm not sure I understand your question. You should test each feature (or user flow), however, I am not sure what you testing. Can you provide a video or a reproducible example of what you are trying to achieve?
e
Set your
cy.visit
in a
beforeEach
You would have to more specific on what you need saved between tests.
b
For example, in first test i go to page "/typologies/create" but before i send the token for authorization. In next test i go to page "/typologies" but he return login page when user as logged. it's easier that way 😁
Copy code
ts
describe('private-accommodation', () => {


  before(function() {
    cy.request({
      method: 'POST',
      url: '/authorization/authorize',
      body: {
        email: 'admin',
        password: 'admin'
      },
    }).then((response) => {
      const token = response.body.data[0].token;
      window.localStorage.setItem('token', token);
      expect(response.status).to.eq(200);
    });
  });



  beforeEach(function() {
    const token = window.localStorage.getItem('token');
    cy.intercept({
      url: '/authorization/authorize',
    }, (req) => {
      req.headers["Authorization"] = `Bearer ${token}`;
    });
  });


  it('typologies_create', () => {

    cy.visit('/typologies/create')
    cy.get('#name').type('cypress');

    cy.contains('button', 'Criar').click().then(() => {
      cy.get('#name').invoke('val').then((value) => {
        cy.request({
          url: '/api/private_accommodation/typologies',
          method: 'POST',
          failOnStatusCode: false,
          headers: {
            Authorization: `Bearer ${window.localStorage.getItem('token')}`
          },
          body: {
            name: value
          },
        }).then((resp) => {
          expect(resp.status).to.eq(200);
          });
        });
      });
  })


  it('not_found_page', () => {
    cy.visit('typologies/');
  })

})
@echoing-tent-95037 @adventurous-dream-20049
a
b
Oh right, thanks 🙂