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()
})
my question is how to check both positive and negative tests
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")
}
})
})
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/')