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

99
Views
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 answers
Answer question

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 Report

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 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!