Skip to main content

End-to-end Testing Login and Signup Pages with Cypress

When building web applications, it’s essential to ensure the login and signup pages work smoothly. One of the most efficient ways to achieve this is through automated testing using Cypress. Cypress makes it simple to write tests that check user interactions, ensuring everything behaves as expected.

In this post, we'll walk through how to test login and signup pages using Cypress. We'll focus on three core functionalities:

  • Login Process: Verifying that a user can log in with correct credentials.

  • Signup Process: Ensuring that a new user can sign up successfully.

  • Post-login Verification: Confirming that the user is redirected to the correct success page.

Setting Up Cypress

Before writing the tests, ensure you have Cypress set up in your project. If you haven’t already installed Cypress, run:

npm install cypress --save-dev

Once installed, open Cypress for the first time:

npx cypress open

This will launch the Cypress Test Runner and open the Cypress UI, where you can run your tests.

Writing Login Tests

First, let’s create tests to ensure users can log in properly.

Test Case 1: Successful Login

  • Visit the Login Page

  • Fill in the login form with valid credentials

  • Click the Submit button

  • Verify that the user is redirected to the success page

  • Confirm the page displays "Congratulations student. You successfully logged in!"

Cypress Test Code:

describe('Login Page Tests', () => {
  it('should log in with valid credentials', () => {
    cy.visit('https://practicetestautomation.com/practice-test-login/');
    
    cy.get('#username').type('student');
    cy.get('#password').type('Password123');
    cy.get('#submit').click();
    
    cy.url().should('include', '/logged-in-successfully');
    cy.contains('Congratulations student. You successfully logged in!').should('be.visible');
  });

  it('should display an error for invalid credentials', () => {
    cy.visit('https://practicetestautomation.com/practice-test-login/');
    
    cy.get('#username').type('wronguser');
    cy.get('#password').type('wrongpassword');
    cy.get('#submit').click();
    
    cy.contains('Your username is invalid!').should('be.visible');
  });
});

Writing Signup Tests

Next, let’s test the signup page to ensure that new users can sign up.

Test Case 2: Successful Signup

  • Visit the Signup Page

  • Fill in the form with valid information

  • Click the Submit button

  • Verify the user is redirected to the login page

Cypress Test Code:

describe('Signup Page Tests', () => {
  it('should sign up a new user', () => {
    cy.visit('/signup');
    
    cy.get('input[name="username"]').type('newuser');
    cy.get('input[name="email"]').type('newuser@example.com');
    cy.get('input[name="password"]').type('newpassword');
    cy.get('input[name="confirmPassword"]').type('newpassword');
    cy.get('button[type="submit"]').click();
    
    cy.url().should('include', '/login');
    cy.contains('Please log in to continue').should('be.visible');
  });
});

Post-Login Verification

Once a user logs in successfully, they should be redirected to the Logged In Successfully page.

Test Case 3: Verify Logged In Successfully Page

  • Confirm the URL is /logged-in-successfully

  • Verify the page title is "Logged In Successfully"

  • Ensure the message "Congratulations student. You successfully logged in!" is displayed

  • Verify the Log Out button is present and functional

Cypress Test Code:

describe('Logged In Successfully Page Tests', () => {
  it('should display success message after login', () => {
    cy.visit('https://practicetestautomation.com/practice-test-login/');
    
    cy.get('#username').type('student');
    cy.get('#password').type('Password123');
    cy.get('#submit').click();
    
    cy.url().should('include', '/logged-in-successfully');
    cy.contains('Congratulations student. You successfully logged in!').should('be.visible');
  });
  
  it('should log out successfully', () => {
    cy.get('a').contains('Log out').click();
    cy.url().should('include', '/practice-test-login/');
  });
});

Best Practices for Cypress Testing

  • Keep Tests Independent: Each test should be able to run on its own, without relying on other tests to pass first.

  • Clean the State: Ensure that the application is in a clean state before running each test. Use Cypress commands to reset the app state if necessary.

  • Test Edge Cases: Consider testing invalid inputs, network errors, and other edge cases to ensure your app behaves as expected in all scenarios.

Conclusion

Testing login and signup pages with Cypress can help automate the process of ensuring your app's authentication functionality works as expected. By writing simple tests for login, signup, and post-login verification, you can catch potential issues early and improve the overall reliability of your application.

Happy testing! 

Comments

Popular posts from this blog

8 Mistakes Every Beginner Programmer Makes (and How to Avoid Them)

  Starting with programming can be exciting but also challenging. Every beginner makes mistakes—it's part of the learning process! However, knowing common pitfalls can help you improve faster. Here are eight mistakes every beginner programmer makes and how to avoid them. 1. Not Understanding the Problem Before Coding ❌ Mistake: Jumping straight into coding without fully understanding the problem can lead to messy, inefficient, or incorrect solutions. ✅ Solution: Take a step back and analyze the problem . Break it into smaller parts and think about the logic before writing any code. Use flowcharts, pseudocode, or even pen and paper to sketch out your solution. 📌 Example: Instead of diving into loops, first clarify what needs to be repeated and under what conditions. 2. Ignoring Error Messages ❌ Mistake: Many beginners panic when they see an error message and either ignore it or randomly change things to make the error disappear. ✅ Solution: Read the error message carefully —it of...

Understanding SQL Query Execution Order

When writing SQL queries, understanding the execution order is crucial for writing efficient and optimized code. Many beginners assume that queries execute in the order they are written, but in reality, SQL follows a specific sequence of execution. SQL Execution Order SQL queries run in the following order: 1️⃣ FROM + JOIN 2️⃣ WHERE 3️⃣ GROUP BY 4️⃣ HAVING 5️⃣ SELECT (including window functions) 6️⃣ ORDER BY 7️⃣ LIMIT Let’s break down each step with examples. 1. FROM + JOIN (Data Retrieval) The SQL engine first retrieves data from the specified table(s) and applies any JOIN operations. 🔹 Example: SELECT employees.name, departments.department_name FROM employees JOIN departments ON employees.department_id = departments.id; Here, the JOIN happens before any filtering ( WHERE ) or grouping ( GROUP BY ). 2. WHERE (Filtering Data) Once data is retrieved, the WHERE clause filters rows before aggregation occurs. 🔹 Example: SELECT * FROM employees WHERE salary > 50000 ; Thi...

Exploring the Latest AI Tools: Revolutionizing Productivity and Creativity

Artificial Intelligence (AI) continues to revolutionize various industries by providing powerful tools that enhance creativity, productivity, and efficiency. Here’s a curated list of the latest AI tools across different domains in 2024: Design Tools AI-powered design tools help streamline the creative process, making it easier to create stunning visuals, graphics, and branding materials. Autodraw – AI-assisted sketching tool. Flair AI – Generates high-quality product mockups. Booth AI – AI-driven photoshoot generator. Content Creation These tools assist in generating and enhancing content, from text to multimedia, making content production more efficient. Writesonic – AI-powered writing assistant for blogs and ads. Beautiful AI – Smart presentation design tool. Stocking AI – Generates realistic stock images. Clipdrop – Enhances and removes background from images. Steve AI – Converts text into animated videos. Tome – AI storytelling and presentation software. Murf AI – AI voic...