SlideShare a Scribd company logo
1 of 52
1
TOPIC
Building reliable
web applications
using Cypress
@mauricedb
Who am I?
@mauricedb
@mauricedb
@mauricedebeijer
Maurice de Beijer
@cloudgen_verona
#CodeGen2021
4
Agenda
• What is Cypress and why should you care?
• End 2 end versus unit testing
• Getting started with Cypress
• Accessible queries using Cypress Testing Library
• Cypress and AJAX requests
• Using visual regression testing
• Cypress testing and a CI server
• Dealing with flaky Tests
5
What is Cypress and why should you care?
6
Cypress.io
• Cypress is a front end testing tool
• Built for the modern web
• Tests are easy to write
• Using simple JavaScript
• Time Travel runner lets you inspect test at each step
• Click on a step to see the state of the browser
• Automatic waiting for elements
• Makes tests more resilient
• Video recording of running test
• Helps debugging failing tests on the CI server
7
8
Cypress.io
• Lets you fake the network
• Or use the real backend as appropriate
• Automatically retry
• Run flaky tests multiple times
• Many standard and 3rd party extensions
• Or write your own in JavaScript
• Can run in Docker containers
• Consistent cross platform behavior
• Runs inside the browser
• Not based on Selenium
9
Running Cypress tests
10
End to End versus Unit testing
11
The traditional testing pyramid
Manual
End to End
Integration testing
Unit testing
12
Both windows are fine
Source
13
A sturdy latch
Source
14
A better testing pyramid for the web
Manual
End to End
Integration testing
Unit testing
15
Getting started with Cypress
16
Getting started with
Cypress
Install Cypress
• Using `npm install cypress –save-dev`
Start Cypress
• Using `npx cypress open`
No drivers or servers required
17
Basic Cypress tests
19
Cypress Testing Library
20
Accessible queries using Cypress Testing Library
The more your tests resemble the way your software
is used, the more confidence they can give you.
(Kent C. Dobbs)
• Test the application like a user would use it
• Avoid implementation details in your tests
• Adds commands to select elements based on:
• Aria roles & labels of elements
• Labels associated with input elements
• Placeholder text
• ARIA = Accessible Rich Internet Applications
21
Find a table cell using it’s ARIA role
22
Finding an input element by using it’s label
23
25
Improve tests with ARIA attributes
• Some elements don’t have a useful label
• For example a movie row
• Add an `aria-label` to query it as needed
• Often better then a selector class, id or data-testid
26
Adding an ARIA label
27
Searching for the ARIA label
29
Cypress and AJAX requests
30
Cypress and AJAX requests
• By default the application will run as normal
• The real backend services will be called
• Using `cy.intercept()` you can change that
• Either call the API and use the same result in your test
• Or intercept and mock the whole request
31
Both have pros & cons
Real AJAX requests
Slower responses
Harder to control data
Often requires a DB reset
Often not an option with
external API’s
Tests the real application
Mock AJAX requests
Faster responses
Easy control over data
Doesn’t test the
backend services
33
Visual Regression Testing
34
Using Visual Regression Testing
Visual regression testing ensures the UI is unchanged
• Testing by selecting/manipulating elements proves the
application works
• But not that a user can actually use it
• You might have a white button on a white background
• The button is there and works
• But the user can’t see it
35
Visual Regression Testing
Pros
• Checks that the
appearance is
unchanged
• Complete check even for
elements you don’t think
about
• Validate elements like
the browsers canvas
Cons
• Hard to do if you don’t
control the data
• Difficult cross platform
because of small
rendering differences (CI)
37
Faking standard functions
• Stubs allow you to modify functions
• Including standard JavaScript functions
• Using the cy.stub()
• Control how time passes
• For setInterval(), setTimeout, Date.now() or new Date()
• Using the cy.clock()
38
Faking Math.random()
40
Continuous Integration
41
Cypress testing and a CI server
• Execute the `cypress run` command
• GitHub actions make testing the application easy
• Use cypress-io/github-action@2
• For CI servers that are not directly supported
• Use the start-server-and-test NPM package
42
GitHub Action to run Cypress tests
43
Visual Regression Testing and the CI Server
• The best way is to use a Docker container
• Standard container images like cypress/included:6.3.0
• Works cross platform and on the CI server
• Tip: Add --env failOnSnapshotDiff=false to cypress open
• Avoids a local run from failing on snapshot differences
44
GitHub Action for Visual Regression Testing
46
Flaky Tests
47
Flaky Tests
• Sometimes tests are not as reliable as you would like
• The same test fails occasionally
• Automatic retries will help pass the test
• Retry a failing test one or more times
• Often a sign that there is an issue
• Can be the test or the application
48
Enable retries
49
Cypress Dashboard - Runs
50
Cypress Dashboard - Flaky Tests
51
Conclusion
• Cypress is a great tool for End to End testing
• More reliable then relying on unit testing and manual testing
• Great infrastructure
• Useful dashboard to record runs
• Many plugins and extensions
• Write tests that use the application as users would
• Cypress Testing Library helps a lot
• Use visual regression tests where appropriate
• But don’t forget to test the application behavior
Thank you
Any questions?
@mauricedb @mauricedb @mauricedebeijer

More Related Content

What's hot

JIRA Performance Testing in Pictures - Edward Bukoski Michael March
JIRA Performance Testing in Pictures - Edward Bukoski Michael MarchJIRA Performance Testing in Pictures - Edward Bukoski Michael March
JIRA Performance Testing in Pictures - Edward Bukoski Michael March
Atlassian
 

What's hot (20)

ASP.NET MVC 5 and SignalR 2
ASP.NET MVC 5 and SignalR 2ASP.NET MVC 5 and SignalR 2
ASP.NET MVC 5 and SignalR 2
 
Validating latest changes with XCI
Validating latest changes with XCIValidating latest changes with XCI
Validating latest changes with XCI
 
Continous integration and delivery for single page applications
Continous integration and delivery for single page applicationsContinous integration and delivery for single page applications
Continous integration and delivery for single page applications
 
Make It Cooler: Using Decentralized Version Control
Make It Cooler: Using Decentralized Version ControlMake It Cooler: Using Decentralized Version Control
Make It Cooler: Using Decentralized Version Control
 
JIRA Performance Testing in Pictures - Edward Bukoski Michael March
JIRA Performance Testing in Pictures - Edward Bukoski Michael MarchJIRA Performance Testing in Pictures - Edward Bukoski Michael March
JIRA Performance Testing in Pictures - Edward Bukoski Michael March
 
How to practice TDD without shooting yourself in the foot
How to practice TDD without shooting yourself in the footHow to practice TDD without shooting yourself in the foot
How to practice TDD without shooting yourself in the foot
 
How to contribute to an open source project and don’t die during the Code Rev...
How to contribute to an open source project and don’t die during the Code Rev...How to contribute to an open source project and don’t die during the Code Rev...
How to contribute to an open source project and don’t die during the Code Rev...
 
Software design with Domain-driven design
Software design with Domain-driven design Software design with Domain-driven design
Software design with Domain-driven design
 
From the Trenches: Effectively Scaling Your Cloud Infrastructure and Optimizi...
From the Trenches: Effectively Scaling Your Cloud Infrastructure and Optimizi...From the Trenches: Effectively Scaling Your Cloud Infrastructure and Optimizi...
From the Trenches: Effectively Scaling Your Cloud Infrastructure and Optimizi...
 
Continuous Integration on AWS
Continuous Integration on AWSContinuous Integration on AWS
Continuous Integration on AWS
 
Serverless meetup - OpenWhisk overview and architecture
Serverless meetup - OpenWhisk overview and architectureServerless meetup - OpenWhisk overview and architecture
Serverless meetup - OpenWhisk overview and architecture
 
#ESPC18 How to do #devops with the #SharePoint Framework and why it matters?
#ESPC18 How to do #devops with the #SharePoint Framework and why it matters?#ESPC18 How to do #devops with the #SharePoint Framework and why it matters?
#ESPC18 How to do #devops with the #SharePoint Framework and why it matters?
 
Celery
CeleryCelery
Celery
 
Spring insight what just happened
Spring insight   what just happenedSpring insight   what just happened
Spring insight what just happened
 
ASP.NET Core Unit Testing
ASP.NET Core Unit TestingASP.NET Core Unit Testing
ASP.NET Core Unit Testing
 
Microsoft ASP.NET 5 - The new kid on the block
Microsoft ASP.NET 5 - The new kid on the block Microsoft ASP.NET 5 - The new kid on the block
Microsoft ASP.NET 5 - The new kid on the block
 
Efficient HTTP applications on the JVM with Ratpack - Voxxed Days Berlin 2016
Efficient HTTP applications on the JVM with Ratpack - Voxxed Days Berlin 2016Efficient HTTP applications on the JVM with Ratpack - Voxxed Days Berlin 2016
Efficient HTTP applications on the JVM with Ratpack - Voxxed Days Berlin 2016
 
CI/CD and Asset Serving for Single Page Apps
CI/CD and Asset Serving for Single Page AppsCI/CD and Asset Serving for Single Page Apps
CI/CD and Asset Serving for Single Page Apps
 
IaC? VSTS to the rescue! Abbreviations explained
IaC? VSTS to the rescue! Abbreviations explainedIaC? VSTS to the rescue! Abbreviations explained
IaC? VSTS to the rescue! Abbreviations explained
 
How to Build Single Page HTML5 Apps that Scale
How to Build Single Page HTML5 Apps that ScaleHow to Build Single Page HTML5 Apps that Scale
How to Build Single Page HTML5 Apps that Scale
 

Similar to Building reliable web applications using Cypress

BYOP: Custom Processor Development with Apache NiFi
BYOP: Custom Processor Development with Apache NiFiBYOP: Custom Processor Development with Apache NiFi
BYOP: Custom Processor Development with Apache NiFi
DataWorks Summit
 
Threat Modeling the CI/CD Pipeline to Improve Software Supply Chain Security ...
Threat Modeling the CI/CD Pipeline to Improve Software Supply Chain Security ...Threat Modeling the CI/CD Pipeline to Improve Software Supply Chain Security ...
Threat Modeling the CI/CD Pipeline to Improve Software Supply Chain Security ...
Denim Group
 

Similar to Building reliable web applications using Cypress (20)

Introduction to cypress in Angular (Chinese)
Introduction to cypress in Angular (Chinese)Introduction to cypress in Angular (Chinese)
Introduction to cypress in Angular (Chinese)
 
MyHeritage - End 2 End testing Infra
MyHeritage - End 2 End testing InfraMyHeritage - End 2 End testing Infra
MyHeritage - End 2 End testing Infra
 
Behat bdd training (php) course slides pdf
Behat bdd training (php) course slides pdfBehat bdd training (php) course slides pdf
Behat bdd training (php) course slides pdf
 
MyHeritage - QA Automations in a Continuous Deployment environment
MyHeritage -  QA Automations in a Continuous Deployment environmentMyHeritage -  QA Automations in a Continuous Deployment environment
MyHeritage - QA Automations in a Continuous Deployment environment
 
Infinum Android Talks #13 - Developing Android Apps Like Navy Seals by Ivan Kušt
Infinum Android Talks #13 - Developing Android Apps Like Navy Seals by Ivan KuštInfinum Android Talks #13 - Developing Android Apps Like Navy Seals by Ivan Kušt
Infinum Android Talks #13 - Developing Android Apps Like Navy Seals by Ivan Kušt
 
BYOP: Custom Processor Development with Apache NiFi
BYOP: Custom Processor Development with Apache NiFiBYOP: Custom Processor Development with Apache NiFi
BYOP: Custom Processor Development with Apache NiFi
 
Operating a High Velocity Large Organization with Spring Cloud Microservices
Operating a High Velocity Large Organization with Spring Cloud MicroservicesOperating a High Velocity Large Organization with Spring Cloud Microservices
Operating a High Velocity Large Organization with Spring Cloud Microservices
 
8 cloud design patterns you ought to know - Update Conference 2018
8 cloud design patterns you ought to know - Update Conference 20188 cloud design patterns you ought to know - Update Conference 2018
8 cloud design patterns you ought to know - Update Conference 2018
 
From Zero to Serverless
From Zero to ServerlessFrom Zero to Serverless
From Zero to Serverless
 
Test-Driven-Development for Networking: Making CI Work for You by Colin McNam...
Test-Driven-Development for Networking: Making CI Work for You by Colin McNam...Test-Driven-Development for Networking: Making CI Work for You by Colin McNam...
Test-Driven-Development for Networking: Making CI Work for You by Colin McNam...
 
Threat Modeling the CI/CD Pipeline to Improve Software Supply Chain Security ...
Threat Modeling the CI/CD Pipeline to Improve Software Supply Chain Security ...Threat Modeling the CI/CD Pipeline to Improve Software Supply Chain Security ...
Threat Modeling the CI/CD Pipeline to Improve Software Supply Chain Security ...
 
Introduction to Protractor
Introduction to ProtractorIntroduction to Protractor
Introduction to Protractor
 
Hacker Proof web app using Functional tests
Hacker Proof web  app using Functional testsHacker Proof web  app using Functional tests
Hacker Proof web app using Functional tests
 
Automated Acceptance Testing from Scratch
Automated Acceptance Testing from ScratchAutomated Acceptance Testing from Scratch
Automated Acceptance Testing from Scratch
 
Scribe online 03 scribe online cdk and api overview
Scribe online 03   scribe online cdk and api overviewScribe online 03   scribe online cdk and api overview
Scribe online 03 scribe online cdk and api overview
 
Continuous Deployment To The Cloud @DevoxxPL 2017
Continuous Deployment To The Cloud @DevoxxPL 2017 Continuous Deployment To The Cloud @DevoxxPL 2017
Continuous Deployment To The Cloud @DevoxxPL 2017
 
Cloud Design Patterns - Hong Kong Codeaholics
Cloud Design Patterns - Hong Kong CodeaholicsCloud Design Patterns - Hong Kong Codeaholics
Cloud Design Patterns - Hong Kong Codeaholics
 
Power of Azure Devops
Power of Azure DevopsPower of Azure Devops
Power of Azure Devops
 
Sencha Roadshow 2017: What's New in Sencha Test
Sencha Roadshow 2017: What's New in Sencha TestSencha Roadshow 2017: What's New in Sencha Test
Sencha Roadshow 2017: What's New in Sencha Test
 
Continuous Delivery: How RightScale Releases Weekly
Continuous Delivery: How RightScale Releases WeeklyContinuous Delivery: How RightScale Releases Weekly
Continuous Delivery: How RightScale Releases Weekly
 

More from Maurice De Beijer [MVP]

More from Maurice De Beijer [MVP] (20)

Practice TypeScript Techniques Building React Server Components App
Practice TypeScript Techniques Building React Server Components AppPractice TypeScript Techniques Building React Server Components App
Practice TypeScript Techniques Building React Server Components App
 
A foolproof Way to Estimate a Software Project
A foolproof Way to Estimate a Software ProjectA foolproof Way to Estimate a Software Project
A foolproof Way to Estimate a Software Project
 
Surati Tech Talks 2022 / Build reliable Svelte applications using Cypress
Surati Tech Talks 2022 / Build reliable Svelte applications using CypressSurati Tech Talks 2022 / Build reliable Svelte applications using Cypress
Surati Tech Talks 2022 / Build reliable Svelte applications using Cypress
 
Build reliable Svelte applications using Cypress
Build reliable Svelte applications using CypressBuild reliable Svelte applications using Cypress
Build reliable Svelte applications using Cypress
 
Building Reliable Applications Using React, .NET & Azure
Building Reliable Applications Using React, .NET & AzureBuilding Reliable Applications Using React, .NET & Azure
Building Reliable Applications Using React, .NET & Azure
 
Concurrent Rendering Adventures in React 18
Concurrent Rendering Adventures in React 18Concurrent Rendering Adventures in React 18
Concurrent Rendering Adventures in React 18
 
Building reliable applications with React, C#, and Azure
Building reliable applications with React, C#, and AzureBuilding reliable applications with React, C#, and Azure
Building reliable applications with React, C#, and Azure
 
Building large and scalable mission critical applications with React
Building large and scalable mission critical applications with ReactBuilding large and scalable mission critical applications with React
Building large and scalable mission critical applications with React
 
Building Reliable Applications Using React, .NET & Azure
Building Reliable Applications Using React, .NET & AzureBuilding Reliable Applications Using React, .NET & Azure
Building Reliable Applications Using React, .NET & Azure
 
Why I am hooked on the future of React
Why I am hooked on the future of ReactWhy I am hooked on the future of React
Why I am hooked on the future of React
 
Getting started with React Suspense and concurrent rendering
Getting started with React Suspense and concurrent renderingGetting started with React Suspense and concurrent rendering
Getting started with React Suspense and concurrent rendering
 
React suspense, not just for Alfred Hitchcock
React suspense, not just for Alfred HitchcockReact suspense, not just for Alfred Hitchcock
React suspense, not just for Alfred Hitchcock
 
From zero to hero with the Reactive extensions for JavaScript
From zero to hero with the Reactive extensions for JavaScriptFrom zero to hero with the Reactive extensions for JavaScript
From zero to hero with the Reactive extensions for JavaScript
 
Why I am hooked on the future of React
Why I am hooked on the future of ReactWhy I am hooked on the future of React
Why I am hooked on the future of React
 
The new React
The new React The new React
The new React
 
From zero to hero with the reactive extensions for JavaScript
From zero to hero with the reactive extensions for JavaScriptFrom zero to hero with the reactive extensions for JavaScript
From zero to hero with the reactive extensions for JavaScript
 
Why I am hooked on the future of React
Why I am hooked on the future of ReactWhy I am hooked on the future of React
Why I am hooked on the future of React
 
I am hooked on React
I am hooked on ReactI am hooked on React
I am hooked on React
 
Create flexible React applications using GraphQL apis
Create flexible React applications using GraphQL apisCreate flexible React applications using GraphQL apis
Create flexible React applications using GraphQL apis
 
Create flexible react applications using GraphQL API's
Create flexible react applications using GraphQL API'sCreate flexible react applications using GraphQL API's
Create flexible react applications using GraphQL API's
 

Recently uploaded

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 

Recently uploaded (20)

ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 

Building reliable web applications using Cypress

  • 4. 4 Agenda • What is Cypress and why should you care? • End 2 end versus unit testing • Getting started with Cypress • Accessible queries using Cypress Testing Library • Cypress and AJAX requests • Using visual regression testing • Cypress testing and a CI server • Dealing with flaky Tests
  • 5. 5 What is Cypress and why should you care?
  • 6. 6 Cypress.io • Cypress is a front end testing tool • Built for the modern web • Tests are easy to write • Using simple JavaScript • Time Travel runner lets you inspect test at each step • Click on a step to see the state of the browser • Automatic waiting for elements • Makes tests more resilient • Video recording of running test • Helps debugging failing tests on the CI server
  • 7. 7
  • 8. 8 Cypress.io • Lets you fake the network • Or use the real backend as appropriate • Automatically retry • Run flaky tests multiple times • Many standard and 3rd party extensions • Or write your own in JavaScript • Can run in Docker containers • Consistent cross platform behavior • Runs inside the browser • Not based on Selenium
  • 10. 10 End to End versus Unit testing
  • 11. 11 The traditional testing pyramid Manual End to End Integration testing Unit testing
  • 12. 12 Both windows are fine Source
  • 14. 14 A better testing pyramid for the web Manual End to End Integration testing Unit testing
  • 16. 16 Getting started with Cypress Install Cypress • Using `npm install cypress –save-dev` Start Cypress • Using `npx cypress open` No drivers or servers required
  • 18.
  • 20. 20 Accessible queries using Cypress Testing Library The more your tests resemble the way your software is used, the more confidence they can give you. (Kent C. Dobbs) • Test the application like a user would use it • Avoid implementation details in your tests • Adds commands to select elements based on: • Aria roles & labels of elements • Labels associated with input elements • Placeholder text • ARIA = Accessible Rich Internet Applications
  • 21. 21 Find a table cell using it’s ARIA role
  • 22. 22 Finding an input element by using it’s label
  • 23. 23
  • 24.
  • 25. 25 Improve tests with ARIA attributes • Some elements don’t have a useful label • For example a movie row • Add an `aria-label` to query it as needed • Often better then a selector class, id or data-testid
  • 27. 27 Searching for the ARIA label
  • 28.
  • 30. 30 Cypress and AJAX requests • By default the application will run as normal • The real backend services will be called • Using `cy.intercept()` you can change that • Either call the API and use the same result in your test • Or intercept and mock the whole request
  • 31. 31 Both have pros & cons Real AJAX requests Slower responses Harder to control data Often requires a DB reset Often not an option with external API’s Tests the real application Mock AJAX requests Faster responses Easy control over data Doesn’t test the backend services
  • 32.
  • 34. 34 Using Visual Regression Testing Visual regression testing ensures the UI is unchanged • Testing by selecting/manipulating elements proves the application works • But not that a user can actually use it • You might have a white button on a white background • The button is there and works • But the user can’t see it
  • 35. 35 Visual Regression Testing Pros • Checks that the appearance is unchanged • Complete check even for elements you don’t think about • Validate elements like the browsers canvas Cons • Hard to do if you don’t control the data • Difficult cross platform because of small rendering differences (CI)
  • 36.
  • 37. 37 Faking standard functions • Stubs allow you to modify functions • Including standard JavaScript functions • Using the cy.stub() • Control how time passes • For setInterval(), setTimeout, Date.now() or new Date() • Using the cy.clock()
  • 39.
  • 41. 41 Cypress testing and a CI server • Execute the `cypress run` command • GitHub actions make testing the application easy • Use cypress-io/github-action@2 • For CI servers that are not directly supported • Use the start-server-and-test NPM package
  • 42. 42 GitHub Action to run Cypress tests
  • 43. 43 Visual Regression Testing and the CI Server • The best way is to use a Docker container • Standard container images like cypress/included:6.3.0 • Works cross platform and on the CI server • Tip: Add --env failOnSnapshotDiff=false to cypress open • Avoids a local run from failing on snapshot differences
  • 44. 44 GitHub Action for Visual Regression Testing
  • 45.
  • 47. 47 Flaky Tests • Sometimes tests are not as reliable as you would like • The same test fails occasionally • Automatic retries will help pass the test • Retry a failing test one or more times • Often a sign that there is an issue • Can be the test or the application
  • 50. 50 Cypress Dashboard - Flaky Tests
  • 51. 51 Conclusion • Cypress is a great tool for End to End testing • More reliable then relying on unit testing and manual testing • Great infrastructure • Useful dashboard to record runs • Many plugins and extensions • Write tests that use the application as users would • Cypress Testing Library helps a lot • Use visual regression tests where appropriate • But don’t forget to test the application behavior
  • 52. Thank you Any questions? @mauricedb @mauricedb @mauricedebeijer

Editor's Notes

  1. https://forms.gle/k3C3rnWpJ1LNVzrg8 https://docs.google.com/forms/d/10DfiJbLmoAaPkWhL8DU3zwciH1U8zdqAnQ5GB-Zm8Ik/edit#responses