Table Of Contents

What is Cypress?

Cypress is a next-generation testing tool constructed for the modern web. This tool addresses the critical problems which we face every day like synchronization issues, flaky tests, element loading issues, issues with dynamic elements etc.

How Cypress architecture is different from selenium?

Selenium relies on WebDriver to execute the commands that means selenium executes remote command across the network. But in the cypress, it directly operates inside the browser. So the browser executes the commands which we run as script.

Explanation: Real world scenario,

Consider Selenium,

If I have element.click() in my test script, Once I execute this script selenium sends this code to browser specific drivers like ChromeDriver, Ghecko Driver etc. The browser specific drivers then interact with specific browsers like Chrome, Firefox etc. and executes the click action.

Consider Cypress,

The  same command element.click() Considering Cypress.

Once you execute code in Cypress, The cypress directly sends the code to browser and click action gets executed.

So with the above example we can understand that the middle layer that is webdriver which is present in the selenium but Cypress doesn’t need any middle layer.

Can I use Cypress framework with C# or Java just like Selenium?

No, Cypress is NodeJS based framework. Cypress supports only JavaScript and Typescript as a programming language.

Could you tell me about some differences between Cypress and Selenium?

  • Selenium supports all major languages like C#, Java, Python, JavaScript, Ruby etc.
  • Cypress Supports only JavaScript/Typescript languages
  • Selenium Commands are executed through web drivers
  • Cypress Commands Executed directly on the browser
  •  Selenium Supports all major browsers chrome,Edge, Internet Explorer, Safari, Firefox
  • Cypress supports only Chrome, Edge and Firefox
  • With Selenium Configuration of drivers and language biding should be done by our own
  • With Cypress we get ready framework available we just need to install.
  • Selenium Appium can we used to test native mobile applications
  • Cypress doesn’t support any native mobile application testing

What are the advantages of Cypress?

  • Cypress is NodeJS modern framework so it works seamlessly with Single Page applications and internal Ajax calls.
  • Cypress provides Time Travel options, so It takes snapshots of each tests, after execution we can see what happened exactly in each step. We don’t have to do any configuration for this this comes by default with cypress.
  • Cypress Debuggability feature provides access to developer tools on the browser so we can debug directly in the browser.
  • Cypress Automatically waits for commands and assertions or any animation to complete so most of the time we don’t have to put additional sleep in our tests.
  • Since Cypress directly executes commands on browser it is quite faster compared to Selenium based tools.
  • Cypress runs tests and executes commands directly on browser so it is less flaky.
  • Cypress provides Video Capture option we can record whole set of tests execution.

Can I use Junit or TestNG in cypress?

    Or

    What is Testing Framework Cypress comes with?

We cannot use the Junit or TestNG in Cypress, Cypress comes with Mocha and Chai assertion libraries.

How can I install cypress?

If you want to install cypress, we need to install Node first, once we install node we can install cypress with command  npm install cypress

How can I open cypress window and execute tests?

Once you install the cypress, we can open cypress using npm cypress open command and execute the tests by clicking on test file name.

Note: First time when we execute npm cypress open command, cypress prepares the framework ready for you. This involves creating folder structure such as cypress, support, plugin, integration etc. This happens only when we execute npm cypress open command for the first time, subsequent execution of this command only launches the cypress window since set up is done already.

Could you talk something about Cypress file and folder structures?

  1. Cypress uses cypress.json file, if we want to specify any custom configuration which is located in the root of our project.
  2. The folder named cypress is located in the project root folder which is main folder for cypress automation framework.
  3. By default Cypress folder contains 4 subfolders namely fixtures, integration, plugins and support.
  4. Fixtures folder can be used to store our external Json data files and we can use these files in our tests using the command cy.fixture().
  5. Integration folder mainly consists of our actual spec/test files
  6. Plugins folder contains special files that executes in Node before project is loaded, before the browser launches, and during your test execution. This is very  helpful when we want to have pre processers and post processors. Files in this folder can be executed after all the test execution is complete as well.
  7. Example: Let us consider, If we want to generate HTML result after all the test is completed. The HTML report generation or collating those HTML reports can be done here in this file.
  8. Support folder contains the special file index.js which will be run before each and every test. Support folder can also be used to create utility methods which will be helpful in through out our automation framework. This file is the perfect place to put all your reusable behavior such as Custom commands or global overrides that you want to be applied to all of your spec files.

What is Cypress CLI?

Cypress CLI is unique feature in cypress it provides ability to run our cypress tests in command line. This feature is helpful when we run our cypress tests in pipelines. It provides many options and flags control the cypress test behavior.

Example: use npm cypress run to run your tests in command line.

How can I run single specfile in command line?

We can run single spec file in command line using - - spec option and specifying test name.

Example:  npm cypress run - - spec=”myspec.ts”

I am new to cypress, I wanted to execute hello world test script in cypress how can I do that could you explain?

Once you have installed cypress it will create folder called integration, inside integration folder you can create your first spec file using typescript or JavaScript.

Your spec file should contain describe() block and or it() block. Use the cypress command inside this block and execute the test.

Why should I create my tests inside integration folder, why can’t I create test in other folders?

The default tests folder is integration in cypress, so when we execute the tests it will look for integration folder get all the test file.

But you can create the tests in other folder in that case you need to change the configuration and specify where your integration folder is located using option called “integrationFolder

How can see the default configuration in Cypress?

You can see the default configuration in Cypress Under on Cypress window  Test Runner > Settings and Configurations.

How can I create suites in cypress?

We can create a describe() block the describe block acts as suite, inside that each test can be created as single it() block.

Describe() is like a suite here.

Tell me at least 5 Cypress commands?

cy.visit(): cy.visit() is used to navigate to the specific website
Ex: cy.visit(‘http://www.google.com’);

cy.log(): cy.log is used for display cypress console logs during execution
Ex: cy.log(‘test 123’);

cy.get(): cy.get is used for getting dom element in cypress, once we get dom element we can perform action on that like click, type etc.
Example: let myElement = cy.get(‘#username’)

cy.url(): cy.url() is used to Get the current URL of the page that is currently active.
Example: Consider you have navigate to https://google.com using cy.visit();, now you can use the cy.url() to get the url back.

// Example Cypress Code:

cy.visit('https://www.google.com'); //Visit the webpage

let myurl= cy.url(); //Get webpage url

cy.url().should('include', 'google'); //Assert URL

How can I click on the button in Cypress?

.click() function is used to click the element on cypress. The click function should be chained with element. I.e we need to get the desired element first and we need to click.

Example: If we want to click on Google Search button, First we need to get the element of search using selectors like below.

cy.get('#search').click()

List out some commands which I can use to interact with DOM elements?

.click() : is used to click on the element

Example:

cy.get('#search').click()

.dblclick() : is used to double click on the element.

Example:

cy.get('input[name="btnK"]').dblclick();

.rightclick(): is used to right click on the element

Example:

cy.get('body').rightclick();

.type() : is used to type on element or text boxes

Example:

cy.get('input[name="texbox"]').type("This is to test");

.clear() : is used to clear the fields or text boxes.

Example:

cy.get('input[name="q"]').clear();

.check() : is used to Check checkbox(es) or radio(s).

Example:

cy.get('input[name="chkbox"]').check();

.uncheck() : is used to unCheck checkbox(es) or radio(s).

Example:

cy.get('input[name="chkbox"]').uncheck();

.select(): Select an <option> within a <select>

Example:

cy.get('select').select('user-1')

Consider a scenario, I have link on webpage and clicking this link opens new window I need to verify some text in that window. How can I verify?

Cypress by default doesn’t support, working with new windows so we need to follow different approach here.

Let’s consider example:

<a href="http://google.com" target="_blank"> Take me to Google </a>

In the above code clicking on Take me to Google link opens the new window the reason is we have attribute target="_blank". So now if we remove attribute. target="_blank" then it will open in the same window. That can be performed by below code

// We can remove the offending attribute - target='_blank'

// that would normally open content in a new tab.

cy.get('a[href*="google"]').invoke('removeAttr', 'target').click()

// after clicking the <a> we are now navigated to the new page and we can assert that the url is correct

cy.url().should('include', 'google')

What are the selectors supported by Cypress?

By Default cypress supports only CSS selectors, However we can use third party plugin to use Xpath selectors.

Can I use XPath in Cypress?

Cypress doesn’t support xpath by default, but if we install third party plugin like cypress-xpath we can use xpath in your script.

Example:

We need to install cypress-xpath in our project first using

 npm install cypress-xpath

Once we have installed the above plugin, we need to add entry into project's cypress/support/index.js

require('cypress-xpath')

After that we can use the xpath in out script like below

 cy.xpath('//ul[@class="todo-list"]//li')

What is environment variable in Cypress?

The Environment Variables is a variable whose value sets at the level of the operating system, and it sets outside the context of the program or framework

In order to use environment variable we need to define in cypress.json

{

  "env": {

    key: value

  }

}

Once we define we can access them in our script like below

cy.visit(Cypress.env('key'));

This environment variable value we can change dynamically using command line arguments

cypress run --env "Key"="Value"

Lets consider one example, let me create one evnrinment variable in cypress.json

For example:

The above value of environment variable can be accessed through

let tokenvalue = Cypress.env(‘token’);

If I want to change the value dynamically using command line I can do so.

cypress run --env "token"="ieoeeoeieoeie";

How can I change the baseUrl in cypress dynamically?

When we say executing multiple environment that means we are executing scripts on Staging, QA, production, Usually website remains same only website url changes. That is called baseUrl, we can change baseUrl dynamically by passing baseUrl in command line.

First we need to configure cypres.json with baseUrl option

Code: <cypress.json>

"baseUrl": "https://qa.web.com"

Then we can override this base url in command line while running our tests.

npx cypress run --config baseUrl="https://www.staging-website.com/"

the above code runs the tests on staging website. Earlier configured qa.web.com will be overridden with command line argument baseUrl value.

What is Preserve cookies in Cypress? Why do we need that?

Cypress by default clear the cookies after every test. In order to stop clearing cookies we need to use preserve cookies option in cypress

Example:

I have code like below

describe('my test', () => {

it('test1', () => {

//Some code to test

});

it('test2', () => {

//Some code to test

});

it('test3', () => {

//Some code to test

});

})

In the above code we have three tests namely test1, test2, test3,
Lets consider you have logged into your application in test1, after execution of test1 when it moves to test2, it again asks for login because login sessions and cookies are cleared after test1.

This is default bevaior in cypress. We can overcome this problem by adding code in our tests like below.

describe('my test', () => {

beforeEach(() => {

Cypress.Cookies.preserveOnce('session_id', 'remember_token')

});

it('test1', () => {

//Some code to test

});

it('test2', () => {

//Some code to test

});

it('test3', () => {

//Some code to test

});

})

What is custom commands in Cypress?

Cypress comes with default set of commands like cy.visit, cy.relaod etc.. like this we can create our custom commands in cypress.

For example: I can create some commands like cy.login(), which will enter the username and password and clicks on submit.

We can accomplish the custom command with below lines of code

All custom commands are first configured in cypress/support/command.js in this file we need to create a command like

Cypress.Commands.add("login", (username, password) => {

  //adding a new command named login

  cy.get("#username").type(username);

  cy.get("#password").type(password);

  cy.get("#login").click();

});

Once we add the above command now we can use in our script like below.

cy.visit("http://mysite.com/login.html");

cy.login('Myusername','My Passowrd');

How can I type Keypress in Cypress?

Keypress can be done in cypress using .type() command, If we need to use the keys like CTRL, SHIFT, ALT etc then we need to specify in braces like below

cy.get(‘login’).type('{shift}{alt}hello'));

How can I read the value from Cypress Configuration file?

We can read the cypress.config values from cypress using Cypress.config();

Example:

If I have defined pageLoadTimeout in config file like blow.

Below code is in cypress.json

{

  "pageLoadTimeout": 60000

}

I can access the same in my script like

let timeout = Cypress.config('pageLoadTimeout')

How to get the title of the page?

cy.title() : cy.title() can be used to get the title of the current active window

example:

cy.visit('http://www.facebook.com')

cy.title().should('eq','Facebook Login or Signup')

How can I use the sleep in Cypress?

cy.wait() : cy.wait is used to wait for specific amount of time.

Example:

cy.wait(1000)

How can I get the first and last child of the selected element?

.first() and .last() is used for getting first and last child of the element

Consider example,

cy.get('input[name='rows'])

let’s say this selectors returns array of elements say 6 elements.

Then

First child can be obtained using

cy.get('input[name='rows']).first()

Last child can be obtained using

cy.get('input[name='rows']).last()

What is shadow Dom? How can I access shadow DOM in cypress?

Shadow DOM allows hidden DOM trees to be attached to elements in the regular DOM tree — this shadow DOM tree starts with a shadow root.

Example: Consider we have shadow dom like below

<div class="shadow-host">

  #shadow-root

  <button class="my-button">Click me</button>

</div>

We can access the above shadow dom using below code in cypress

cy.get('.shadow-host').shadow().find('.my-button').click()


Tags


You may also like

Groups attribute in TestNG 

Groups attribute in TestNG 
Leave a Reply

Your email address will not be published. Required fields are marked

  1. I guess you've mentioned the wrong point "Cypress doesn’t supports only Javascript/Typescript" under the question "What is the disadvantage of using Cypress?".

    It should be like 'Cypress supports only JavaScript and Typescript as a programming language.'

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}