How to write Cypress test from Test runner?

Reading Time: 3 minutes

Cypress is one of those popular tools which has established its place very quickly and is very popular for Web integration and End to End UI test automation. It needs a javascript framework like jasmine or mocha. These are one of the famous assertion libraries in JavaScript. Cypress recommends using mocha. Whenever we are downloading cypress, it automatically bundles mocha. Let’s see How to write the Cypress test from the Test runner.

Test suite name “describe” and the “it” block as test cases have been there in the framework. There can be multiple test cases in the test suite.

describe(): describe uses to group the test cases. This has two arguments, the first name of the group and the second one is the callback function.

it(): We use it for an individual test case. It takes two arguments, a string explaining what the test should do, and a callback function that contains our actual test.

How to write a test case in Cypress?

For writing a test case we will have the following scenario:

  1. Create a .js file
  2. Write the test case inside the suite
  3. Write a test case to visit a website
  4. Validate the page by using asserts
  5. Run the test case

Step 1: Create a myfirst.js file inside the integration folder.

Cypress is one of those popular tools which has established its place very quickly and is very popular for Web integration and End to End UI test automation.

Here cy has been global command in cypress. This command is just like the driver. Also here we do not need to create an object as cy directly comes by default.

Step 2: Write test case inside test suite.

describe(‘My First Cypress Test’, function() {
it(‘Visits to google page’, function() {
cy.visit(“www.google.com”);
})
})

Step 3: Visit a website.

By default the command cy.visit() uses uses the pageLoadTimeout and the baseurl set globally.

Here the webpage link is defined inside cy.visit()

cy.visit(“www.google.com”);

Step 4: Assertion

Assertions are the validation steps that determine whether the specified step of the automated test case succeeded or not. In actual, Assertions validates the desired state of your elements, objects etc.

Cypress bundles the popular Chaiassertion library with extension of jQuery. There are some common assertions. We use these assertions with .should() .

cy.get('textarea').should('have.value', 'Google')

Step 5: Open Cypress and run myfirst.js

To run cypress, we need to open cypress test runner and then from that we can pick any of our .js file and run. The js file is already having the test case in it.

./node_modules/.bin/cypress open

After running the .js file, above one is the output. Hence we can run our test cases from test runner.

Hope you all enjoyed reading.

References

https://docs.cypress.io/examples/examples/tutorials#2-Text-inputs

knoldus

Written by 

I work with Knoldus and have experience of 8+ years in quality assurance I have good experience on SeleniumWebdriver(C#), Selenium IDE, Nightwatch,Appium, Jira, TFS, ZAP, Fiddler, Jmeter, Jenkins, Selenium Webdriver. I am also ITIL certified.

Leave a Reply