Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

420
Views
La condición if/else en Cypress no funciona como se esperaba

tengo este codigo

 Cypress.Commands.add('VerifyLoginState', () => { if(cy.contains('Login')) { cy.get('.form-control').eq(0).type('firstfield') cy.get('.form-control').eq(1).type('secondfield') cy.get('.btn-login').click() cy.wait(2500) cy.contains('Upcoming Appointments').should('be.visible') } else { cy.contains('Appointment summary').should('be.visible') } })

¿Cómo debo escribir el código para que pase a la condición de else, cuando estoy autenticado en el navegador y la condición de if no es válida?

En otras palabras, quiero verificar si un elemento está presente en la página, e incluso si no está presente, el código no debería dar un error y continuar.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Cypress produce el resultado de las funciones cy y no las devuelve. Entonces, su if/else no funcionará como lo haría en JavaScript tradicional. Consulte este artículo de Cypress sobre las pruebas condicionales. Algo como lo siguiente debería ayudarte:

 // Get the body of the DOM cy.get('body').then(($body) => { // Check if the body contains the `Login` element if ($body.contains('Login').length) { cy.get('.form-control').eq(0).type('firstfield') cy.get('.form-control').eq(1).type('secondfield') cy.get('.btn-login').click() cy.wait(2500) cy.contains('Upcoming Appointments').should('be.visible') } else { cy.contains('Appointment summary').should('be.visible') }
about 4 years ago · Juan Pablo Isaza Report

0

Otra opción es usar dentro

ref: https://docs.cypress.io/api/commands/within#Syntax

 cy.contains('Login') .within(() => { cy.get('.form-control').eq(0).type('firstfield') cy.get('.form-control').eq(1).type('secondfield') cy.get('.btn-login').click() cy.wait(2500) cy.contains('Upcoming Appointments').should('be.visible') })
about 4 years ago · Juan Pablo Isaza Report

0

Puede que le resulte más fácil probar uno u otro texto usando jQuery :contains y varios selectores separados por una coma

 cy.get('body') .children(':contains(Login), :contains(Appointment summary)') // only one is on the page .invoke('text') .then(labelText => { if (labelText.trim() === 'Login') { cy.get('.form-control').eq(0).type('firstfield') ... etc } else { cy.contains('Appointment summary').should('be.visible') } })
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!