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:
- Visit the Page: Navigate to the HTML page.
- Perform Actions: Simulate user interactions, such as clicking a button.
- Pause Execution: Call
cy.pause()
to stop the test and allow for manual inspection. - Verify State: After resuming, continue with assertions to check the application state.
Output:
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:
- Visit the Initial Page: Navigate to the starting page of the application.
- Click Navigation Link: Simulate clicking a link to navigate to a different page.
- Pause Execution: Call
cy.pause()
to stop and check the new page's state. - Verify Content: After resuming, continue with assertions to validate the presence of new page elements.
Output:
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
Software Testing Tutorial Software testing is an important part of the software development lifecycle that involves verifying and validating whether a software application works as expected. It ensures reliable, correct, secure, and high-performing software across web, mobile applications, cloud, and CI/CD pipelines in DevOp
10 min read
What is Software Testing? Software testing is an important process in the Software Development Lifecycle(SDLC). It involves verifying and validating that a Software Application is free of bugs, meets the technical requirements set by its Design and Development, and satisfies user requirements efficiently and effectively.Here
11 min read
Principles of Software testing - Software Testing Software testing is an important aspect of software development, ensuring that applications function correctly and meet user expectations. From test planning to execution, analysis and understanding these principles help testers in creating a more structured and focused approach to software testing,
3 min read
Software Development Life Cycle (SDLC) Software Development Life Cycle (SDLC) is a structured process that is used to design, develop, and test high-quality software. SDLC, or software development life cycle, is a methodology that defines the entire procedure of software development step-by-step. The goal of the SDLC life cycle model is
8 min read
Software Testing Life Cycle (STLC) The Software Testing Life Cycle (STLC) is a process that verifies whether the Software Quality meets the expectations or not. STLC is an important process that provides a simple approach to testing through the step-by-step process, which we are discussing here. Software Testing Life Cycle (STLC) is
7 min read
Types of Software Testing Software testing is a important aspect of software development life-cycle that ensures a product works correctly, meets user expectations, and is free of bugs. There are different types of software testing, each designed to validate specific aspects of an application, such as functionality, performa
15+ min read
Levels of Software Testing Software Testing is an important part of the Software Development Life Cycle which is help to verify the product is working as expected or not. In SDLC, we used different levels of testing to find bugs and errors. Here we are learning those Levels of Testing in detail.Table of ContentWhat Are the Le
4 min read
Test Maturity Model - Software Testing The Test Maturity Model (TMM) in software testing is a framework for assessing the software testing process to improve it. It is based on the Capability Maturity Model(CMM). It was first produced by the Illinois Institute of Technology to assess the maturity of the test processes and to provide targ
8 min read
SDLC MODELS
TYPES OF TESTING