Data driven testing in Cypress using Fixtures

In the previous post here we saw how to read external files in Cypress using cy.readFile. Cypress also provides another way to read files. In this post, I will show how to Fixtures to do data driven testing in Cypress. ####Syntax#### According to documentation, syntax is as below. cy.fixture(filePath) cy.fixture(filePath, encoding) cy.fixture(filePath, options) cy.fixture(filePath, encoding, options) ####Comparison with cy.readFile#### Main difference between cy.readFile and cy.Fixture is that former one starts looking for the files from project root folder and later looks for files under Fixture folder. cy.Fixture supports a wide range of file types/extensions like json, txt, html,jpeg,gif,png etc. If file name is not specified it looks for all supported filetypes in specific order starting with JSON. We can even assign alias to this , which will help to reuse this later on. ...

December 19, 2020

Reading external files in Cypress using readFile

In the previous post here and here, we saw how to write BDD test cases using cucumber along with Cypress and to use datatables. As we saw in those examples, we are hard coding few data in feature files. This is not a best practise since we need to modify the feature file for every new set of data. Assuming we need to have different data in different environments, this will make it hard to run the tests across various test environment. Let us look at how we can make read these data from a file outside of feature file. ...

December 19, 2020

Using Datatable in Cypress Cucumber

In the previous post here, we saw how to use cucumber along with Cypress. The demo scenario we saw was a basic example. Now let us look into how we can use datartable in cypress cucumber. ##Feature File## Feature: Demo for BDD in Cypress I want to demo using BDD in Cypress @focus Scenario: Logging into ECommerce Site Given I open Demo shopping page When I login as 'standard_user' user Then I should see products listed Given I add below products to cart |ProductName |Qty| |Sauce Labs Backpack |1 | |Sauce Labs Fleece Jacket |1 | |Sauce Labs Onesie |1 | The step definition file will be as below ...

December 11, 2020

Writing Cypress tests in BDD format

As discussed in previous posts here and here, we use Cypress for writing tests to validate GUI and API tests. In many companies we use behaviour driven development practises where the requirements or acceptance criteria are specified in Gherkin format. As a results, test automation scenarios are also written in Gherkin format. Cucumber , Specflow etc are some of such framework used extensively . In this post, let us examine how we can write BDD test cases in Cypress. ...

December 9, 2020

How to validate XHR in Cypress

In the previous post we saw about working with Cookies in Cypress. Now let us look at how we can work with XHR request in Cypress. XHR stands for XMLHttpRequest. It is an API in the form of an object whose methods transfer data between a web browser and a web server. Cypress provides inbuilt functionality to work with XHR request. It provides us with objects with information about request and response of these calls. It will help to do various assertions on header, url , body , status code etc as needed. It also help us to stub the response if needed. ...

November 25, 2020

How to read browser cookies in Cypress

Cypress has inbuild support to read browser cookies. There are two commands for this - GetCookie and GetCookies. Refer documentation for more details ###GetCookie This command get a cookie by its name. cy.getCookie(name) cy.getCookie(name, options) Note: name is the name of cookie . Options can be used to change default behaviour like logging , timeout Examples usage is as below //Below line is added to get intellisense while writing code in visual studio code /// <reference types="cypress" /> it('Read Cookies In Cypress',() => { cy.visit("www.commbank.com.au"); //GetCookie returns an object with properties like name, domain, httpOnly, path, secure, value, expiry ( if provided), sameSite(if provided) //Checking for individual cookie property value cy.getCookie('s_cc').should('have.property','value','true'); cy.getCookie('s_cc').should('have.property','domain','.commbank.com.au'); // Checking multiple properties of a cookie. *cy.getCookie* will get an object. *Then* helps to work with object yielded from previous cy.getCookie('s_cc').then((cookie) => { cy.log(cookie); cy.log(cookie.name); expect(cookie.domain).to.equal('.commbank.com.au'); expect(cookie.name).to.equal('s_cc'); expect(cookie.httpOnly).to.equal(false); expect(cookie.path).to.equal('/'); expect(cookie).to.not.have.property('expiry'); }) }) Results from test run will look like below ...

November 19, 2020

Running API Test using Cypress

Cypress is not just UI automation tool . It can be used for testing APIs as well . Even though we have other tools like Postman, Newman, Rest Assured, SOAP UI etc for testing APIs, I believe cypress is a good alternative for testing API. It will help to use same tool for both UI and API test automation. Demo Let us look at a sample API test case. In below example, we trigger a API call to http://services.groupkt.com/country/get/iso2code/AU and validate below in the response. ...

May 27, 2018

UI Automation with cypress

When we talk about UI automation for browsers, the default tool which comes to mind is Selenium. There are different wrappers around selenium like protractor, Nightwatch , selenium webdriver etc. All of them are build on top of selenium and have all advantages /disadvantages of selenium. All of the control browser by executing remote commands through Network. We will most probably need additional libraries, framework etc to make full use of selenium. ...

May 11, 2018