Cypress Interview Questions
Cypress Interview Questions
Cypress is a modern javascript-based front-end testing tool written in node js. Cypress runs tests
within the browser, which makes testing easier and more reliable.
Behind Cypress is a Node server process. Tools like Selenium operates outside the web browser
while The Cypress engine is coupled within the browser.
Cypress and the Node process constantly interact, synchronise, and conduct tasks on each other’s
behalf. Cypress operates at the network layer, reading and modifying web traffic in real-time.
This allows Cypress to modify not just everything coming in and out of the browser, but also
code that may interfere with its ability to automate the browser.
1)Cypress can run tests and also execute commands on the browser. so tests are less flaky and
much fast.
2)Automatic wait
3)Cypress can take snapshots and record videos of tests after executing each step. We don’t need
to configure extra plugins like we do in Selenium.
4)Built-in assertions available.
5)Network traffic control.
We cannot use the Junit or TestNG in Cypress, Cypress comes with Mocha and Chai assertion
libraries.
7)How to create a test suit in Cypress?
A test suit referred to a collection of test cases. We can create a test suit in Cypress using
describe() block or context() block. The describe() block acts as a suite, and inside that block,
each test can be written as a single it() block.
Cypress only supports CSS Selector. However, we can use the Cypress-Xpath plugin to work
with Xpath.
Cypress by default does not support XPath. However, there is one plugin Cypress-Xpath by
which we can use XPath to interact with DOM elements.
require('@cypress/xpath');
Cypress does not offer official BDD built-in support, however, the NPM Cypress-Cucumber-
Preprocessor plugin allows you to write your tests in BDD Cucumber Syntax. This plugin
automatically translates Gherkin syntax into Cypress.
There are built-in assertions available in Cypress. By using the “should” assertion we can verify
the title of a webpage in Cypress.
Cypress hooks are used to define or set preconditions that we want to execute before a set of tests
or before each test. For example, if we want to read test data from a fixture file or we want to
perform some configuration tasks, or cleanup tasks then we can utilize Cypress hooks concepts.
The available hooks in Cypress are before(),beforeEach(), after() and afterEach().
For example, if we have defined the env in the config.json file like this:
{"env": stage}
The fixtures folder keeps all the necessary data in the Cypress project. It helps us to get the input
data from external files. This directory can store multiple JSON files and these JSON files are
read by multiple tests. All fixture data has to be declared within the before hook block.
{
"Username": "AQH",
"City" : "DELHI"
}
cy.fixture(path of test data)
To verify whether the button is visible or not on the webpage use inbuilt Cypress assertion.
cy.get('button#form-submit').should('be.visible')
17)How many types of assertions are available in Cypress?
Assertions are the validations that verify that this behaviour or functionality is intended or not.
There are 2 types of assertions available in Cypress.
1)Implicit Assertions: These are inbuilt assertions. We can have positive as well as negative
implicit assertions. Perfect examples of implicit assertions are should() or and() type assertions.
1.1)Positive Assertion
cy.get('li.todo').should('have.length', 3)
1.2)Negative Assertion
cy.get('li.todo').should('not.have.length', 2)
2)Explicit Assertions: Explicit assertions are used to perform some custom logic before
applying assertion. An example of explicit assertion is the “expect()” assertion.
const employee =
{ name: 'QA', age: 30, }
it('Hit Get Request validate its response status code and body', () => {
cy.request({
method: 'GET',
url: 'https://randomuser.me/api/',
qs: 'results=1'
}).then((response) => {
expect(response.status).to.eq(200)
expect(response.body).to.have.property('info')
})
})
})
To execute Cypress tests, put all the file names in the desired order in the Cypress.JSON file
under the test files property section.
20)How to handle reusability in the Cypress framework?
By default Cypress includes the index.js and commands.js in the Support folder. Index.js
executes before every test file. We can put reusable behaviour such as custom commands and
global overrides in this folder.
Cypress has a go() command to navigate forward and backwards in the browser.
1. To go forward
cy.go('forward')
or
cy.go(1)
2. To go backwards
cy.go('back')
or
cy.go(-1)
We can use it.skip() or describe.skip(). That it/describe block will be skipped during test
execution.
cy.get('#upload')
.selectFile('cypress/images/logo.png')
Cypress has the scrollIntoView() command which scrolls an element into view.
cy.get('footer').scrollIntoView()
or
cy.get('#nav').scrollIntoView({ offset: { top: 150, left: 0 } }) //scroll
using offset
Use the cy. children() command to select the child element of a given element.
Several alternatives are available in the market of Cypress. A few of them are as followed.
1.WebdriverIO
2. CasperJS
3. Playwright
4. Jest
Cypress does not provide built-in support to generate test reports but we can integrate third-party
plugins like Mochawesome, and Allure to generate HTML reports. Please visit this article to
know how to generate reports in the Cypress framework.
We don’t need to add any plugin to perform logging in Cypress. Cypress provides the “cy.log()”
command. This command prints the message on the console.
The cy. visit() command is used to navigate to a specific URL or route in Cypress tests. It allows
you to start your test at a specific page or state.
{
"baseUrl" : "https://www.programsbuzz.com/"
}
describe('Automating The Signin',()=> {
it('visit the site ',()=>{
cy.visit('/')
})
})
To check the application’s responsiveness we can use “cy. viewport()” command. It manages the
orientation and dimension of the application.
In Cypress, we don’t need to add any dependency or plugin to capture screenshots like we used
to do in Selenium. Cypress has the inbuilt capability to capture screenshots of the fail tests. It
also allows us to take explicit screenshots during test execution. For example, if for debugging
purposes later I want to take a screenshot of a step where the user provided some input then we
can do this by using the following command.
cy.screenshot()
This post covers a wide range of topics related to Cypress interview questions. Gain Practical
experience in Cypress tool to understand the topics.