Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

102
Vistas
How to automate negative and positive tests using cypress

I need to automate positive part and negative part of UI, for example I select sign In section, Here my code

it('login is not correct',function(){
    

    cy.contains('Invalid Username or Password').should('not.exist')
    cy.get('input[type = "text"]').type('admins')
    cy.get('input[type = "password"]').type('admins')
    cy.get('.MuiButton-label').contains('LOGIN').should('be.visible').click()

    cy.contains('Invalid Username or Password').should('exist')
    



})

it('The correct login page',function(){
    cy.get('input[type = "text"]').type('admin')
    cy.get('input[type = "password"]').type('admin')
    cy.get('.MuiButton-label').contains('LOGIN').should('be.visible').click()
    
          
})

check the ui

my question is how to check both positive and negative tests

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Your test for for incorrect login looks correct as you are validating the error message.

it("login is not correct", function () {
  cy.contains("Invalid Username or Password").should("not.exist")
  cy.get('input[type = "text"]').type("admins")
  cy.get('input[type = "password"]').type("admins")
  cy.get(".MuiButton-label").contains("LOGIN").should("be.visible").click()
  cy.contains("Invalid Username or Password").should("exist")
})

For correct login, you have to assert some element from the page you get after successful login. Good way would be to assert the username admin

it('The correct login page',function(){
    cy.get('input[type = "text"]').type('admin')
    cy.get('input[type = "password"]').type('admin')
    cy.get('.MuiButton-label').contains('LOGIN').should('be.visible').click()
    cy.get('selector').should('have.text', 'admin')  //assert the username admin on the webpage
})

Using if-else. But I won't recommend this solution because we are using custom wait after login, because we don't know how much time it would take for the login action to complete and give us the feedback that is failed or passed and this can lead to flaky tests. So in my opinion covering two scenarios in two different tests is a better and full proof approach.

it("Login Functionality", function () {
  cy.get('input[type = "text"]').type("admin")
  cy.get('input[type = "password"]').type("admin")
  cy.get(".MuiButton-label").contains("LOGIN").should("be.visible").click()
  cy.wait(2000) //wait
  cy.get("body").then((body) => {
    if (body.find("selector of error message").length > 0) {
      //Login Failed
      expect("selector of error message").to.have.text("Invalid Username or Password")
    } else {
      //Login Passed
      expect("selector").to.have.text("admin")
    }
  })
})
about 4 years ago · Juan Pablo Isaza Denunciar

0

Your negative test is good, but I'd change the cy.contains('Invalid Username or Password').should('exist') to cy.contains('Invalid Username or Password').should('be.visible') to assert the error message is visible to the user instead of existing in the DOM.

For the positive test, you can easily add an assertion on the pathname (ie cy.location('pathname').should('include','/userHomeProfile/')

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda