Cypress Top Interview Questions and Answers Part - 4

Cypress Top Interview Questions and Answers Part - 4

1.?What are in-built framework in cypress (or) What are in-built assertions in cypress.?

Ans:

Cypress itself is a comprehensive end-to-end testing framework for web applications. It provides a complete testing solution with built-in functionalities and APIs. Some of the key built-in features and frameworks in Cypress include:

?Mocha: Cypress uses the Mocha testing framework as its underlying test runner. Mocha provides a flexible and powerful test execution environment, allowing you to write tests using its syntax and utilize its various features like test hooks, test organization, and reporting.

?Chai: Cypress integrates with the Chai assertion library, which provides a wide range of assertion styles and methods for making assertions in your tests. Chai helps you write expressive and readable assertions to validate the expected behavior of your application.

?jQuery: Cypress includes jQuery by default, allowing you to easily manipulate and traverse the DOM elements within your tests using familiar jQuery syntax. This makes it convenient to interact with elements, find elements by selectors, perform actions, and make assertions.

?Cypress Command API: Cypress provides its own command API, which extends and enhances the native browser APIs. This API offers a wide range of commands that allow you to interact with your application, simulate user actions, make assertions, and perform various tasks like navigating, clicking, typing, and more.

?Automatic Waiting: Cypress has built-in automatic waiting and retrying mechanisms. It intelligently waits for elements and assertions to resolve before proceeding with the next command, eliminating the need for explicit timeouts and manual waits. This helps ensure that your tests run reliably and consistently.

?Network Stubbing: Cypress allows you to stub and intercept network requests made by your application. You can simulate different network scenarios, modify responses, delay responses, or block requests altogether. This feature enables you to test different network conditions and interactions with APIs.

?Time Travel: Cypress offers time-travel debugging, allowing you to pause your tests at any point and inspect the state of your application. You can step through each command, view DOM snapshots, debug network requests, and make assertions in real-time, making it easier to troubleshoot and diagnose issues.

?Automatic Screenshots and Videos: Cypress automatically captures screenshots and records videos of your test runs. By default, screenshots are taken on test failure, and videos can be recorded for the entire test run. This helps in visual debugging and provides a record of the test execution.

?These are just a few of the built-in features and frameworks provided by Cypress. The combination of these features makes Cypress a powerful and convenient testing framework for web applications, providing a seamless experience for end-to-end testing and making it easier to write, run, and debug tests.

2.?What are the different kind of assertions in cypress ?

Ans:

In Cypress, you can use various types of assertions to validate the expected behavior of your application. Cypress integrates with the Chai assertion library, which provides multiple assertion styles and methods. Here are the different kinds of assertions commonly used in Cypress:

1.Chai BDD Assertions: These assertions follow the Behavior-Driven Development (BDD) style and provide human-readable syntax. Some commonly used BDD-style assertions in Cypress include:

  • expect(value).to.be: This assertion style allows you to chain various methods to make assertions. For example:?expect(value).to.be.equal(expected).
  • expect(value).to.equal(expected): Checks if the?value?is equal to the?expected?value.
  • expect(value).to.be.true: Checks if the?value?is true.

expect(value).to.exist: Checks if the?value?exists (not null or undefined).

2.Chai TDD Assertions: These assertions follow the Test-Driven Development (TDD) style and are slightly different from the BDD style. Some commonly used TDD-style assertions in Cypress include:

  • assert.equal(value, expected): Checks if the?value?is equal to the?expected?value.
  • assert.isTrue(value): Checks if the?value?is true.

assert.isDefined(value): Checks if the?value?is defined (not null or undefined).

3. Sinon-Chai

https://github.com/domenic/sinon-chai

These chainers are used on assertions with cy.stub() and cy.spy().

4.Cypress Assertions: Cypress also provides its own set of assertions as part of its command API. These assertions can be chained directly after a command. Some commonly used Cypress assertions include:

  • cy.get(selector).should('be.visible'): Checks if the element selected by?selector?is visible.
  • cy.get(selector).should('have.text', expectedText): Checks if the element selected by?selector?has the?expectedText?as its text content.
  • cy.url().should('include', expectedUrl): Checks if the current URL includes the?expectedUrl.These are just a few examples of the different kinds of assertions available in Cypress. The Chai library provides many more assertion methods and styles that you can use in your tests. You can refer to the Cypress documentation on assertions and the Chai documentation for a comprehensive list of available assertion methods and their usage.

3.What is positive and negative assertions in cypress?

Ans:

Should(“be.visible”)

Should(“not.be.visible”)

--

.should('not.exist')

.should('exist')

--

.should('be.checked')

.should('not.be.checked')

--

.should('not.have.css', 'display', 'none')

.should('have.css', 'display', 'none')

--

.should('be.enabled')..and('not.be.disabled')

.should('not.be.enabled')..and('be.disabled')

4. What difference between older and newer version of cypress ?

Ans:

Cypress 10 and above

==================

Under root folder

cypress

downloads

all the downloaded through scripts will be save d in this folder

e2e

To create the spec files and write script

We can create subfolder

fixtures

To manage the test data

To keep the test data

We can create subfolder to manage effectively

reports

Mochawesome reporter

index.html

HTML report will be generated

screenshots

All the failed tests screenshots will be generated

Videos

All the tests video recording will be generated

Support

commands.js

Custom commands

Reusable global methods

Ex: cy.login("Admin", "admin123")

e2e.js

Importing external package / library

node_modules

All the libraries / Packages will be available

cypress.config.js

In this file all the global configurations

baseUrl

viewport

retries

timeouts

env

reporters

videos

To configure all the plugins

We can have multiple config files also

package.json

Packagename

who author

Package version

scripts

dependencies

Dev dependencies

package-lock.json

==================================

Cypress below 10

=====================

Under root folder

cypress

downloads

all the downloaded through scripts will be save d in this folder

integration

filename.cy.js - 10 and above

filename.spec.js

To create the spec files and write script

We can create subfolder

fixtures

To manage the test data

To keep the test data

We can create subfolder to manage effectively

plugins

index.js

To configure all the plugins

Reports

Mochawesome reporter

HTML report will be generated

Screenshots

All the failed tests screenshots will be generated

Videos

All the tests video recording will be generated

Support

commands.js

Custom commands

Reusable global methods

Ex: cy.login("Admin", "admin123")

index.js

Importing external package / library

node_modules

All the libraries / Packages will be available

cypress.json

In this file all the global configurations

baseUrl

viewport

retries

timeouts

env

reporters

video

package.json

Contains below info:

Package name

who author

Package version

scripts

dependencies

Dev dependencies

package-lock.json

5.How run test cases in cypress?

Ans:

  1. Through command Line

npx cypress run?

- To run all the tests inside e2e folder in headless

npx cypress run --headed

- To run all the tests inside e2e folder in headed

npx cypress run --headed --browser=chrome

- To run all the tests inside e2e folder in headed and Specify the browser

Cypress supports the following browsers:

- electron

- chrome

- chromium

- chrome:canary

- edge

- firefox

npx cypress run --browser=chrome

- To run all the tests inside e2e folder in headless and in chrome

npx cypress run --spec "relative path of the file"

- To run single spec file

npx cypress run --spec "cypress\e2e\1-getting-started\**.cy.js"

- To run specific folder spec file

2. Through Cypress test runner

npx cypress open

要查看或添加评论,请登录

社区洞察

其他会员也浏览了