Part 3 - Hands-On Test Automation Project with Cypress + Cucumber + Docker

Part 3 - Hands-On Test Automation Project with Cypress + Cucumber + Docker

This article is a continuation of Part 2 - Cucumber. The implementation of Part 2 can be found in my demo-cypress project tagged as v2.0. You can download this release to use it as a starting point for this part of the article.

Part 3 - Configurations

At this point we should start thinking about how we would like to run our tests. Running the open command may be fine to execute it locally, but we might also need to run it through the command line if we want to run on a server. 

We can use the following to run the scenarios through the command line:

./node_modules/.bin/cypress run

This command will execute all the scenarios that match the configuration we added on cypress.json file: "testFiles": "**/*.feature". By default, Cypress will run tests in Electron headlessly (i.e., without a graphical user interface).

No alt text provided for this image

When we are creating an automation project we should also think about all the different ways we want or may need to execute the tests. For example, Cucumber allows us to create tags. Tags are a great way to organize our features and scenarios. They can be used for running a subset of scenarios. 

To initialize tests using tags we will have to run Cypress passing the TAGS environment variable following the syntax 'not @tag1 and (@tag2 or @tag3)'. Do you remember we added the tag "@login" to our scenario on login.feature? The following command should be used to run that specific tag:

./node_modules/.bin/cypress-tags run -e TAGS='@login'

We may find it easier to add the Cypress command to the scripts object in our package.json file and call it from an npm run script. When calling a command using npm run, we need to pass the command’s arguments using "--". 

Add the following to our package.json:

"scripts": {
  "cy:run": "./node_modules/.bin/cypress run",
  "cy:open": "./node_modules/.bin/cypress open",
  "cy:tags": "./node_modules/.bin/cypress-tags run"
} 

Now to open Cypress or run the tests through command line we use one of the following:

npm run cy:open


npm run cy:run 


npm run cy:tags --e TAGS='@login'

Another thing we should start thinking about at this point is: How can we execute the tests against different base URLs? This is a common situation where we need to be able to test our application throughout the development cycle, running in different environments as, for example, development, test, and release. 

Cypress allows us to change the baseUrl very easily. The default baseUrl goes in the configuration file cypress.json (as we already defined) - and then we can set CYPRESS_BASE_URL as an environment variable to override it and run the test like shown below.

CYPRESS_BASE_URL=https://the-internet.herokuapp.com npm run cy:run

There are many other ways to set environment variables using Cypress for different use cases. Feel free to check Cypress environment variable documentation to set your project according to your needs.

Last but not least, we should create a readme file and document everything that will help users of our project. On the root of our project create the file readme.md. Here is a sample of how I would create it.

# Demo Cypress Automation Framework


## Table of Contents


- [Authors](#authors)
- [Pre-requisites](#pre-requisites)
- [Libraries](#libraries)
- [Running Tests](#running-tests)


## Authors
* [Soraia Reis](https://github.com/soraiareis)


## Pre-requisites


You should download and install these properly on your system. Visit the websites (linked) to find instructions on how to set them up.


* [Node.js](https://nodejs.org/en/) - An open-source, cross-platform, JavaScript runtime environment that executes JavaScript code outside of a web browser. Install Node.js from <https://nodejs.org/en/>.
* [npm](https://www.npmjs.com/) - A package manager for the JavaScript programming language. It is the default package manager for the JavaScript runtime environment Node.js. It is distributed with Node.js, which means that when you download Node.js, you automatically get npm installed on your computer.


## Libraries


- [cypress](https://www.cypress.io/) - A JavaScript-based end-to-end testing framework.
- [cypress-cucumber-preprocessor](https://github.com/TheBrainFamily/cypress-cucumber-example) - Library used to support Behavior-Driven Development (BDD) using Cucumber.


## Running Tests


You can run the tests by opening Cypress using the below command line. 
```
npm run cy:open
```


You can also execute all tests through the terminal with the command line below.
```
npm run cy:run
```


You can also specify which tags you want to execute by passing as an argument following the syntax `not @tag1 and (@tag2 or @tag3)` like the command line below:
```
npm run cy:tags --e TAGS='@login'
```


You can also change the `baseURL` by setting the `CYPRESS_BASE_URL` environment variable like the command line below:
```
CYPRESS_BASE_URL=https://the-internet.herokuapp.com npm run cy:run
```

We are done with Part 3 of this article. :)

Remember to commit your changes to Git:

git add .
git commit -m "Adding Configuration"
git push

The implementation of this part can be found in my demo-cypress project tagged as v3.0. You can download this release to check how it looks and/or use it as a starting point for the Part 4 of this article.

Thanks for reading! I hope you found it helpful.

If you enjoyed it, I appreciate your help in spreading it by sharing it with a friend. 

Rodrigo R.

SDET | Cypress | Webdriver.Io | Playwright | Java / Java-script / Type-Script | JMeter | Maestro/Appium | Testing Automation | ISTQB

4 å¹´

Thanks for sharing Soraia. Awesome post.

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

Soraia Reis Fernandes的更多文章

社区洞察

其他会员也浏览了