I'm new to Cypress, I need in the same test, visit two different URLs and store a value from the first one, to use in the second.
I need to visit this website: https://consulta.guru/gerador-cnpj-gratis click on "Gerar" and copy the generated value to use it on the second website.
I couldn't save this generated value in a variable, and when trying to use the cy.origin function to access another URL, I got this error: "cy.intercept() use is not supported in the cy.origin() callback. Consider using it outside of the callback instead".
But the use of cy.intercept is essential inside this callback. I will put the code I made so far (hides some confidential information).
describe('test', () => {
beforeEach(() => {
cy.visit('https://consulta.guru/gerador-cnpj-gratis');
cy.contains('button', 'Gerar').click({force: true});
cy.intercept('1').as('cnpj');
cy.wait('@cnpj')
.its('response.body')
.should('have.property', 'taxNumbers')
.then(cnpj => {
})
})
it('login', () => {
cy.origin('secondurl', () => {
cy.visit('/admin');
cy.get('input[name="email"]').type('email');
cy.intercept('login').as('login');
cy.contains('button', 'Avançar').click();
cy.wait('@login')
.its('response.body')
.should('have.property', 'generatedCode')
.then(sixDigitCode => {
cy.get('.css-k008qs > :nth-child(1)').type(sixDigitCode);
})
cy.contains('button', 'Avançar').click();
//teste
cy.contains('p', 'Grupos').click();
cy.get('input[name="search"]').type(cnpj);
})
})
})