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

106
Views
Al hacer clic en enlaces que no contienen href en ciprés

Estoy automatizando mi lista de verificación de regresión y estoy atascado en un caso de prueba en el que tengo que probar un enlace que se abre en una nueva pestaña, ya que Cypress no admite varias pestañas, así que quiero abrir ese enlace en la misma pestaña. El problema es que el botón no contiene ningún enlace href en la etiqueta. Aquí está la clase.

Nota: Los fragmentos de código que se muestran a continuación funcionan bien si existe href.

 <a rel="noopener noreferrer" target="_blank"> <div class="tooltip" style="width: fit-content;"> <span class="tooltip-trigger"> <button class="button-wrapper default undefined undefined mr-3">Preview</button> </span> </div> </a>

He intentado varias soluciones pero nada me funciona. Algunos son los siguientes.

 cy.xpath('/html/body/div[1]/div[1]/header/div[2]/a') .should('have.attr', 'href').and('include', 'Preview') .then((href) => { cy.visit(href) })

Otra solución.

 cy.xpath("/html/body/div[1]/div[1]/header/div[2]/a") .first() .should(($a) => { $a.attr("target", "_self"); }).click();

Probé este también.

 cy.xpath("/html/body/div[1]/div[1]/header/div[2]/a") .invoke("removeAttr", "target") .click();

Esperamos recibir noticias de la comunidad.

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

0

La siguiente solución funcionó en mi caso.

 cy.window().then((win) => { cy.stub(win, "open") .callsFake((url) => { return win.open.wrappedMethod.call(win, url, "_self"); }) .as("open"); }); cy.get("a").contains("Preview").click(); cy.get("@open").should( "have.been.calledWithMatch", "{your url}" );

Puedes leer más sobre esta solución aquí. https://glebbahmutov.com/blog/cypress-tips-and-tricks/#deal-with-windowopen

about 4 years ago · Juan Pablo Isaza Report

0

Cuando me enfrento a este problema, cambio el atributo de destino a _self. No estoy seguro de si mi selector obtendrá su elemento. No sé si el lapso con la clase tooltip-trigger es único.

 cy.get(`span[class="tooltip-trigger"]`) .should('be.visible') .parent() .parent() .parent() .find('a') .and('have.attr', 'target' , '_blank') .invoke('attr', , 'target' , '_self') .click() //assert location
about 4 years ago · Juan Pablo Isaza Report

0

No estoy seguro de si esto lo hace, pero dado que no hay href en el elemento <a> , supongo que la nueva pestaña se abre mediante el controlador onclick del <button> .

Comenzaría por bloquear el onclick y afirmar que se llamó.

 const stub = cy.stub() cy.get('button') .then($button => { $button[0].onclick = stub }) .click() cy.wrap(stub).should('be.called')

Luego, para avanzar con la prueba en la URL de la nueva pestaña, abra otra prueba y cy.visit() directamente en esa URL.

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!