Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

105
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda