Turn your manual testers into automation experts! Request a DemoStart testRigor Free

“Cypress cannot execute commands outside a running test” Explained

The “Cypress cannot execute commands outside a running test” error is a frequently encountered issue in Cypress- a renowned testing framework for web applications.

This problem arises when you attempt to run Cypress commands outside the confines of a specific test, often in the global domain or in areas where Cypress commands are not anticipated to function.

Let us dive into how this error occurs and how to avoid it in detail:

Why does this Error Occur?

Below are a few reasons why this error is encountered and the best possible solutions:

Usage in Global Scope

Cause: One common scenario is when you attempt to use Cypress commands in the global scope of your test file. The global scope is outside the context of any specific test case, so Cypress doesn’t have a test to run commands against.
// Incorrect usage in global scope
cy.visit('https://example.com'); // Error occurs here

Solution: Cypress commands should always be placed within a specific test case (it block) or within hooks like before, beforeEach, after, or afterEach.

Custom Commands

Cause: Another scenario is when you define custom Cypress commands or functions that include Cypress commands and call them outside a test.
// Incorrect usage of custom command

Cypress.Commands.add('login', () => {
  cy.visit('https://example.com'); // Error occurs here
});

// Calling custom command outside a test
cy.login(); // Error occurs here

Solution: Custom commands that contain Cypress commands should be called within the context of a test, not in the global scope.

Asynchronous Code Outside Cypress Commands

Cause: It may lead to this error if you use asynchronous code outside a Cypress command or a .then() block.
// Incorrect usage of async code outside Cypress command
const element = cy.get('.some-element'); // Error occurs here
element.click();
Solution: If you need to perform asynchronous operations, use Cypress’ built-in handling of promises or wrap your async code within a .then() block.
// Correct usage with .then()
cy.get('.some-element').then(($el) => {
  $el.click(); // Inside a Cypress command
});

Cypress Test Structure

Cause: Cypress tests are structured using Mocha, a popular JavaScript testing framework. Each test is represented by an it block. Here’s a simplified example:
describe("Example Test Suite", () => {
  it("should perform a test", () => {
    // Your test code here
  });
});

Solution: Adding all the tests inside the it blocks is always better.

How to Avoid this Error?

  • Place Cypress Commands within Test Cases: Ensure that all Cypress commands, including cy.get(), are placed within a test case (it block) or within appropriate hooks (before, beforeEach, after, afterEach) that are associated with a specific test.
  • Use Cypress Commands in Custom Commands: If you create custom Cypress commands or functions, use Cypress commands inside those functions and call them within a test case.
  • Wrap Asynchronous Code: When working with asynchronous code, always use Cypress’s .then() or .should() to ensure the code runs within the Cypress context.
  • Verify Test File Structure: Check that your test file follows the correct structure for Cypress tests. Test cases should be wrapped within it blocks, and hooks should be used appropriately.
  • Configuration Verification: Double-check that Cypress is correctly set up and integrated into your project, particularly confirming the cypress.json configuration file.
  • Syntax Review: Examine your test code for any syntax mishaps, overlooked typos, or absent parentheses, as such discrepancies can interrupt the execution and trigger the error.

Elevate Automation: Enhancing its Intelligence

Today, the emphasis is on intelligent and sophisticated test automation. While Cypress once held sway, it’s no longer viewed as a frontrunner in the realm of contemporary web frameworks. Cutting-edge automation tools like testRigor employ AI-driven algorithms to ward off timeout errors. Delve into some of the notable features of testRigor:

  • Cloud-hosted: testRigor is a cloud-hosted automated tool that eliminates all the complexities associated with setting up the tool, creating the environment, and configuring the dependencies. After acquiring a license for testRigor, a mere sign-in lets you dive in, conserving your time and energy.
  • Automatic Test Generation – Leveraging generative AI, testRigor enables automated test script generation. Just input the test case title in the description, and watch as test steps are effortlessly produced in moments.
  • Easy Test Script Creation/Maintenance -With testRigor, you can draft test cases using plain English, bypassing the complexities of programming languages. This intuitive method allows virtually anyone to craft or maintain test cases, streamlining script troubleshooting.
  • Cross-browser and Cross-platform Support – testRigor excels in providing comprehensive coverage across various browsers, browser versions, and platforms.
  • Custom Element Locators – testRigor sidesteps the unreliability of XPaths or CSS selectors. Leveraging its AI-driven algorithms, it uses a distinct technique to pinpoint elements. You can specify an element using the text you see or its position on UI, such as:
    click "Cart"
    click on button "Delete" below "Section Name" to the right of "label"

If you are done with Cypress test script errors and looking for a better alternative. Read here how testRigor can help.

Summing Up

Consider advanced test automation tools to expedite your testing process without the hindrance of test script errors and extensive debugging. Selecting a tool that’s cutting-edge and in sync with modern technological trends is pivotal. In this context, testRigor shines, enhancing test scripts with its self-healing feature and offering a straightforward, codeless method for script development, within budget.

Begin your experience with a free trial here.

You're 15 Minutes Away From Automated Test Maintenance and Fewer Bugs in Production
Simply fill out your information and create your first test suite in seconds, with AI to help you do it easily and quickly.
Achieve More Than 90% Test Automation
Step by Step Walkthroughs and Help
14 Day Free Trial, Cancel Anytime
“We spent so much time on maintenance when using Selenium, and we spend nearly zero time with maintenance using testRigor.”
Keith Powe VP Of Engineering - IDT
Related Articles

SAP Concur Testing

Concur Technologies, now SAP Concur, is an American software-as-a-service (SaaS) company providing travel and expense management ...

Authorize.net Testing

You need a payment gateway in several scenarios, especially when you are running a business that requires secure, efficient, and ...

KYC Portal CLM Testing

KYC Portal CLM is an award-winning Client Lifecycle Management (CLM) platform designed to streamline and automate the Know Your ...
On our website, we utilize cookies to ensure that your browsing experience is tailored to your preferences and needs. By clicking "Accept," you agree to the use of all cookies. Learn more.
Cookie settings
Privacy Overview
This site utilizes cookies to enhance your browsing experience. Among these, essential cookies are stored on your browser as they are necessary for ...
Read more
Strictly Necessary CookiesAlways Enabled
Essential cookies are crucial for the proper functioning and security of the website.
Non-NecessaryEnabled
Cookies that are not essential for the website's functionality but are employed to gather additional data. You can choose to opt out by using this toggle switch. These cookies gather data for analytics and performance tracking purposes.