Top 40 Cypress Interview Questions & Answers
1. What is Cypress?
- Cypress is an open-source test automation framework for web applications.
- Supports cross-browser testing
- Platform compatibility
- Supports headless and headful mode
- Automatic waiting
- Real-time reloads
- Debugging
- Screenshots and videos
2. Platforms supported by Cypress
- Windows
- Mac OS
- Linux
3. Browsers supported by Cypress
- Chromium
- Edge
- Firefox
4. What is the primary programming language supported by Cypress?
- JavaScript
5. Command used to install Cypress
npm install cypress
6. What is the command to open Cypress?
npx cypress open
7. How do you write tests in Cypress?
Tests in Cypress are written using JavaScript. You can write a test by creating a new spec file in the ‘cypress/integration‘ directory and writing your test code using Cypress commands.
8. What are Cypress Commands?
Cypress commands are used to interact with elements on a web page, such as clicking buttons, typing text, and making assertions
9. What is the command to launch a URL?
cy.visit('https://letzdotesting.com');
10. What is the command to type values in a textbox?
cy.get('input[type="text"]').type('Hello, Cypress!')
11. What is the command to get the text values ?
cy.get('selector').invoke('text').should('eq', 'expectedText');
12. What is the command to clear values in a textbox?
cy.get('input[type="text"]').clear();
13. What is the command to click a control?
cy.get('button').click();
14. How do you handle test data in Cypress?
You can use fixtures in Cypress to handle test data. Fixtures are files that contain static data such as JSON or CSV files that can be used in your tests.
15. What is the command to find if an element is displayed on a screen?
cy.get('selector').should('be.visible');
16. What is the command to verify if a checkbox/radio is selected?
cy.get('input[type="checkbox"]').should('be.checked');
17. What is the command to verify if a button is enabled?
cy.get('button').should('not.be.disabled');
18. What is the command to verify if a button is disabled?
cy.get('button').should('be.disabled');
19. What is the command to verify if a textbox is editable?
cy.get('input[type="text"]').should('not.have.attr', 'readonly');
20. What is the command to take a screenshot of an element?
cy.get('selector').screenshot();
21. What are the different commands used to select a dropdown list?
cy.get('select').select('OptionText'); //SelectByVisisbleText
cy.get('select').select(2); //SelectByIndex
cy.get('select').select('OptionValue'); //SelectByValue
22. What are the different commands used to deselect a dropdown list?
Cypress doesn’t have a built-in method for deselecting options from a dropdown like the deselect method in Selenium. However, you can achieve a similar effect by reselecting the desired options
23. Name any 4 commonly used Assertions in Cypress?
should('exist')
: Asserts that the element exists in the DOM.
should('be.visible')
: Asserts that the element is visible.
should('have.text', 'expectedText')
: Asserts that the element has the expected text.
should('have.attr', 'attributeName', 'expectedValue')
: Asserts that the element has the expected attribute value.
24. What are the different navigation commands?
cy.visit('https://letzdotesting.com');
cy.reload(); // Refresh your current page
cy.go('back'); // Takes one page backward based on browser's history
cy.go('forward'); // Takes one page forward based on browser's history
cy.go(-1); // Navigate to previous url in browser's history
cy.go(1); // Navigate to next url in browser's history
25. What is the command to right click?
cy.get('button').rightclick();
26. What is the command to double-click?
cy.get('button').dblclick();
27. What is the command used to scroll down to a web element?
cy.get('#myElement').scrollIntoView();
28. How do you run tests in headless mode?
You can run tests in headless mode by using the –headless flag when running the Cypress command.
29. What is the command used to get the attribute values of an element?
cy.get('img').invoke('attr', 'src').should('eq', 'image.jpg');
30. How do you mock network requests in Cypress?
You can mock network requests in Cypress using the cy.route()
command. For example, cy.route('GET', '/api/users', 'fixture:users.json')
mocks a GET request to the ‘/api/users’ endpoint.
31. Write code to close the entire browser.
cy.window().then(win => win.close());
32. How do you handle asynchronous code in Cypress?
Cypress automatically handles asynchronous code using its built-in queuing system. You can use cy.then()
to chain commands together.
33. What are the different types of waits available in Cypress?
In Cypress, there are several types of waits available to handle different synchronization scenarios in your tests. Here are the main types of waits:
Implicit Wait: Cypress doesn’t have an implicit wait like Selenium. Instead, Cypress automatically waits for commands and assertions to pass before moving on to the next command.
Explicit Wait: Cypress provides explicit wait options using commands like cy.wait() and .should(). These commands can be used to wait for specific conditions before continuing with the test.
Timeouts: Cypress has default timeout settings for various commands and assertions. You can also specify custom timeouts using the .timeout() command to wait for a specific amount of time for a command or assertion to pass.
Retry-ability: Cypress automatically retries failed commands and assertions. You can configure the number of retries using the retries option in the configuration file.
Waiting for Network Requests: Cypress provides commands like cy.wait() and .should() that can wait for network requests to complete before continuing with the test.
Overall, Cypress’s approach to waiting is more dynamic and efficient compared to traditional explicit and implicit waits in other testing frameworks.
34. How to handle iframes in Cypress?
cy.iframe().find('button').click()
35. Name any three testing frameworks that can be integrated with Cypress?
- Jest
- Mocha
- Chai
36. How do you handle timeouts in Cypress?
cy.get('button').timeout(5000)
37. What software and tools do you need to run Cypress in Javascript?
- Node.js
- npm (Node Package Manager)
- Cypress (npm install cypress –save-dev)
- Test Runner (npx cypress open)
- Visual Studio Code
38. Name any advanced framework design that can be used with Cypress?
- Cucumber (BDD)
- Page Object Model
- Data-driven testing
- Parallel testing
- Cross-browser testing
- Configuration Properties
- Utilities
- Tests
- Logging
- Reporting
39. Name any 5 Exceptions that you got while working with Cypress
- CypressError: This is a generic error type used by Cypress for various kinds of errors. It can indicate issues such as element not found, timeout exceeded, or unexpected behavior.
- AssertionError: This error occurs when an assertion made using Cypress’s assertion methods (
should()
,expect()
, etc.) fails. It indicates that the expected condition was not met. - XHR2Error: This error occurs when a network request made by your application fails or times out. It can occur if there are issues with the network or the server.
- CommandTimeoutError: This error occurs when a command in your test exceeds the configured command timeout. You can adjust the command timeout in your Cypress configuration to avoid this error.
- Uncaught Exception: This error occurs when an unhandled exception is thrown in your application code during a Cypress test. It can indicate issues with your application’s code or dependencies.
40. Can you provide an example of how to set up mobile emulation in a Cypress test script?
cy.viewport('iphone-x'); cy.viewport('ipad-2'); cy.viewport('samsung-s10');
Thanks for Reading! All the best!!!
Suggested Readings : Two-factor authentication using Playwright
Suggested Readings : Top 40 Playwright Interview Questions & Answers
Suggested Readings: Official Cypress Documentation
Know anyone who is preparing for Software Testing Interview? Help your friends by sharing this article on Facebook, Twitter, or LinkedIn.