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

Reading Configuration Values in Dotnet core using Options Pattern

Normally every project will have some values to be read from config files like user name, connection strings, environment specific details etc. Dotnet Framework In dotnet framework projects, this can be easily done by using Configuration Manager. This is available when we add System.Configuration assembly reference. Let us look at an example of a app config file like below <?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <appSettings> <add key="Url" value="https://www.saucedemo.com/"/> <add key="UserName" value="standard_user"/> <add key="Password" value="secret_sauce"/> </appSettings> </configuration> Storing user name and password in app config is not a best practise. For sake of simplicity , let us keep it here. In dotnet framework , we can read above config values with below ...

December 17, 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

How to modify environment variables for a different not logged in user in windows

Modifying environment variables for a logged in user is straight forward. However there are some instances when we need to modify environment variables for a different user. One frequent usage which I have come across is when we need to modify PATH variables for a service account in a CI box. Service account may not have local logon rights to the machine Or we may not know its password. Hence I always end up using below method to update path variables ...

October 14, 2020

Generating C# classes from xsd

XML Schema Definition tool will help to generate classes that conform to a schema. Steps are as follows. Open VS Command prompt . ( Start Menu » Visual Studio 2019 » Developer command prompt for VS2019) Pass xml schema as an argument to xsd.exe . \c at the end denotes to generate classes xsd.exe C:\Temp\sampleschema.xsd /c /o:C:\Temp There are other options as well. Main ones are below. xsd.exe <schema.xsd> /classes|Dataset [/e:] [/l:] [/n:] [/o:] [/s:] /classes | Dataset : denotes whether to generate class or dataset /e: Element from schema to process /l: Language to use for generated ode . Choose from 'CS','VB','JS','VJS','CPP'. Default is CS /n: Name os namespace /o: Output directory for generated classes There are other options as well. Details can be found on help “xsd /?” ...

May 13, 2020

Exercism

Most crucial factor for effectively learning a programming language are below. Having hands own experience Having a structured learning path Having a mentor to guide and review the code. I was recently looking to learn more about javascript and came across Exercism.io. Exercism.io offer a solution for all above factors and is free of cost All language track will have a series of exercises starting with very basic hello world program and then moving to complex concepts. User should download the exercise , which will have a failing test suite. Once we implement the code and ensure test cases are now passing , we can submit the code for mentor review. Mentors review the code and suggest better ways of doing it, if any. Exercism.io will prevent us from jumping ahead and force to complete one exercise before moving to next one. This actually helps to follow a structured learning path . ...

January 9, 2020