Open In App

Cypress - pause() Method

Last Updated : 24 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The pause() method in Cypress is used to pause the test execution at any point, allowing developers to inspect the application state during testing. This is particularly useful for debugging and verifying interactions within the application.

Syntax:

cy.pause()

Usage of pause() Method

The pause() method is typically called within a test to halt execution temporarily. This gives us the chance to examine the current state of the application, interact with elements manually, or troubleshoot issues.

Arguments

The pause() method does not take any arguments. It simply pauses the test execution at the point it is called.

Examples

Example 1: Pausing a Test for Inspection

In this example, we’ll use pause() to pause the test execution to inspect the state of the application.

HTML File (index1.html)
Save this HTML file in our local server directory (e.g., localhost:3000)

HTML
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Test Page - Pause Example 1</title>
</head>
<body>
    <button class="button">Click Me</button>
    <div class="result" style="display:none;">We have clicked the button!</div>

    <script>
        document.querySelector('.button').addEventListener('click', function() {
            document.querySelector('.result').style.display = 'block';
        });
    </script>
</body>
</html>

Cypress Test File (pause_example1_spec.js)
Save this file in the cypress/e2e (for Cypress v10 and above) or cypress/integration (for older versions) folder of our Cypress project

JavaScript
describe('Using `pause()` Method', () => {
    beforeEach(() => {
        // Visit the test application
        cy.visit('http://localhost:3000/index1.html');
    });

    it('should pause execution for inspection', () => {
        // Perform actions on the application
        cy.get('.button').click(); // Click a button
        
        // Pause the test execution
        cy.pause(); // Execution will pause here

        // The following commands will not run until we resume
        cy.get('.result').should('be.visible'); // Verify result after resuming
    });
});

Explanation:

  1. Visit the Page: Navigate to the HTML page.
  2. Perform Actions: Simulate user interactions, such as clicking a button.
  3. Pause Execution: Call cy.pause() to stop the test and allow for manual inspection.
  4. Verify State: After resuming, continue with assertions to check the application state.

Output:

pause11

When the test reaches cy.pause(), the execution will stop. We can interact with the application in the Cypress Test Runner before resuming the test execution.

Example 2: Pausing After Navigation

In this example, we’ll pause execution after navigating to a new page to verify that the navigation was successful.

HTML File (index.html)
Save this HTML file in our local server directory (e.g., localhost:3000)

HTML
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Test Page - Pause Example 2</title>
</head>
<body>
    <a class="navigate-link" href="index.html">Go to Next Page</a>
    <div class="new-page-content" style="display:none;">Welcome to the new page!</div>

    <script>
        document.querySelector('.navigate-link').addEventListener('click', function(event) {
            event.preventDefault(); // Prevent the default link behavior
            document.querySelector('.new-page-content').style.display = 'block';
        });
    </script>
</body>
</html>


Cypress Test File (pause_example2_spec.js)
Save this file in the cypress/e2e (for Cypress v10 and above) or cypress/integration (for older versions) folder of our Cypress project

JavaScript
describe('Using `pause()` Method After Navigation', () => {
    beforeEach(() => {
        // Visit the initial page
        cy.visit('http://localhost:3000/index.html');
    });

    it('should pause after navigating to another page', () => {
        // Click a link to navigate to another page
        cy.get('a.navigate-link').click();
        
        // Pause execution to inspect the new page
        cy.pause(); // Execution will pause here

        // Verify that the new page content is displayed after resuming
        cy.get('.new-page-content').should('be.visible');
    });
});

Explanation:

  1. Visit the Initial Page: Navigate to the starting page of the application.
  2. Click Navigation Link: Simulate clicking a link to navigate to a different page.
  3. Pause Execution: Call cy.pause() to stop and check the new page's state.
  4. Verify Content: After resuming, continue with assertions to validate the presence of new page elements.

Output:

pause22

When the test reaches cy.pause(), we can inspect the newly loaded page and its elements. Execution will resume when we choose to continue.

Conclusion

The pause() method in Cypress is a valuable tool for debugging and testing. It allows developers to halt test execution at critical points, facilitating inspection of the application's current state and helping identify issues more efficiently. This can enhance the overall testing process by making it easier to interact with the application directly.


Similar Reads