0
0
Cypresstesting~15 mins

Negative assertions (not) in Cypress - Build an Automation Script

Choose your learning style9 modes available
Verify that the login button is not visible before entering credentials
Preconditions (1)
Step 1: Open the login page
Step 2: Check that the login button is not visible before entering any username or password
✅ Expected Result: The login button should not be visible on the page before entering username and password
Automation Requirements - Cypress
Assertions Needed:
Assert that the login button is not visible using negative assertion
Best Practices:
Use cy.get() with proper selectors
Use .should('not.be.visible') for negative assertion
Avoid hardcoded waits; rely on Cypress automatic waits
Use clear and descriptive test names
Automated Solution
Cypress
describe('Negative assertion test for login button visibility', () => {
  beforeEach(() => {
    cy.visit('/login');
  });

  it('should verify login button is not visible before entering credentials', () => {
    cy.get('#login-button').should('not.be.visible');
  });
});

The test suite starts with describe to group related tests.

beforeEach runs before each test to open the login page.

The test case uses cy.get('#login-button') to find the login button by its ID.

Then it uses .should('not.be.visible') to assert the button is not visible, which is the negative assertion.

This approach waits automatically for the element and checks visibility without hardcoded delays.

Common Mistakes - 3 Pitfalls
{'mistake': "Using .should('not.exist') instead of .should('not.be.visible')", 'why_bad': 'The button might exist in the DOM but be hidden, so checking existence fails to verify visibility.', 'correct_approach': "Use .should('not.be.visible') to check that the element is present but hidden."}
Using hardcoded waits like cy.wait(5000) before assertion
Selecting elements with brittle selectors like XPath or overly complex CSS
Bonus Challenge

Now add data-driven testing to verify that the login button remains not visible for three different empty input scenarios: empty username, empty password, and both empty.

Show Hint