By default, Cypress uses the global timeout specified in its configuration file (commonly 30 seconds) to locate elements. This means Cypress will automatically retry commands like cy.get() until the element appears or the timeout is reached.
However, in some test scenarios, elements may take longer than the default timeout to load or become visible. In such cases, Cypress allows you to override the default timeout for individual commands instead of changing it globally. This helps you handle exceptions where a specific action or element requires more waiting time.
Example: Setting a Custom Timeout in Cypress
// Wait up to 60 seconds for the element to appear
cy.get(‘.slow-loading-element’, { timeout: 60000 })
.should(‘be.visible’);
In this example, Cypress will wait up to 60 seconds for the element with the class .slow-loading-element to appear before failing the test. This timeout applies only to this specific cy.get() command.
Comparison with Selenium
In Selenium, waiting for elements is typically handled using the WebDriverWait class along with Expected Conditions. For example:
// Example in Java using Selenium WebDriver
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(60));
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(“.slow-loading-element”)));
In this Selenium example:
WebDriverWaitexplicitly waits for up to 60 seconds.- It checks repeatedly until the condition (
visibilityOfElementLocated) is met or the timeout expires.
Key Differences
| Feature | Cypress | Selenium |
|---|---|---|
| Default Timeout | 30 seconds (configurable via defaultCommandTimeout) | No built-in retry, requires explicit waits |
| Retry Mechanism | Automatically retries most commands until timeout | Must define retry logic using WebDriverWait |
| Syntax for Custom Wait | { timeout: <milliseconds> } in commands like cy.get() | WebDriverWait with conditions |
| Use Case | Best for adjusting wait time per element or action | Best for dynamic conditions and flexible waiting logic |
Summary
- Cypress automatically waits and retries, reducing the need for explicit waits.
- You can override the default timeout per command to handle slower elements.
- Selenium, in contrast, requires you to define explicit waits using
WebDriverWait.
Both approaches allow flexibility, but Cypress simplifies most waiting scenarios through its built-in retry mechanism.