Testing Web Applications Using Cypress

Rate this content
Bookmark

This workshop will teach you the basics of writing useful end-to-end tests using Cypress Test Runner.


We will cover writing tests, covering every application feature, structuring tests, intercepting network requests, and setting up the backend data.


Anyone who knows JavaScript programming language and has NPM installed would be able to follow along.

This workshop has been presented at TestJS Summit - January, 2021, check out the latest edition of this Tech Conference.

FAQ

Yes, the workshop will be recorded and you will receive the video later.

You can post your questions in the Zoom chat or on Discord, and the instructor will try to answer them either live or as soon as a moment is right.

Make sure to clone the repository and perform 'npm install' to install all dependencies. Ensure you have Node installed.

If you can't see the Discord channel, ask for help from the workshop assistants or check the provided links again.

Yes, Cypress can run in different browsers like Chrome, Firefox, and Edge. You can specify the browser using the 'browser' option.

You can use 'cy.request' to reset the data by making an HTTP call to a special endpoint before each test. This ensures that the tests run in a clean state.

You can use 'cy.wait' to wait for specific network requests or DOM updates, and you can also enable test retries to handle intermittent failures.

You can use CSS selectors, data attributes, or the Cypress Selector Playground to find elements. It's recommended to use data attributes for more stable selectors.

The workshop is 3 hours long with a short, five-minute break at the start of each hour.

If you get disconnected, try to reconnect to the session right away so that the workshop can continue.

Gleb Bahmutov
Gleb Bahmutov
173 min
05 Jul, 2021

Comments

Sign in or register to post your comment.

Video Summary and Transcription

The Workshop covers various topics related to Cypress, including app control, network calls, continuous integration, and debugging. Cypress provides intelligent features such as command logging, test reruns, and powerful assertions. The documentation for Cypress is comprehensive and includes syntax, examples, and historical changes. Tests in Cypress should focus on expected behavior and utilize assertions for accurate results. Cypress offers features like time-travel debugging, conditional testing, and intercepting network requests for effective testing.

1. Introduction to the Workshop

Short description:

Thank you for attending this workshop. This workshop will cover a lot of topics related to Cypress. We'll cover the app, basic deaths, selecting elements, controlling the application, and network calls. We might also touch on continuous integration, retryability, and debugging. The workshop will be hands-on, so make sure to clone the repository and perform npm install. We have a Discord and Zoom chat for questions and assistance. Let's get started!

That's it. Have a good day. Thank you for attending this workshop. I love Cypress and I love giving this workshop. And if you are a really advanced user of Cypress, I think you'll find something to benefit from this workshop as well.

A little bit of housekeeping and rules. So this workshop will be for 3 hours, right? We'll take a short, five minute break at the start of each hour. Because it's hard to sit in one place for 3 hours. And so, because obviously you are in your office or at home or wherever you are, you can take a break whenever you want. I can't control you. So make sure to be comfortable. If for some reason we disconnect, right, I will try to reconnect to this session right away so that we can continue. So unless there are some really bad problems, I expect the session to continue and finish.

I will keep an eye on the Zoom chat and on the Discord chat. So one of the benefits of a workshop is that feel free to ask questions, right? I will try to answer as many questions as possible. This workshop will be recorded, so you will get a recording and you will receive this video later. So if you have any questions, post them in the Zoom chat or on Discord, and I will answer them either live or as soon as a moment is right.

Okay, so I expect everyone to have this repository cloned and ready to go. So if you haven't, please clone this repository so you have it locally. You will need the node, so I assume that you have it installed. And the first thing you want to do is make sure that you perform npm install. It might take maybe 5 minutes if you are starting from scratch, because it will install dependencies and cypress and everything. So this repository has everything, right? It has the application. We'll be testing. It has the spec files already. And it has the slides.

So I'm going to start sharing with slides. All the slides are just markdown files. And I'm using this get-page link to actually show the markdown. It's a good thing that it still is January, because I'll have to move the slides somewhere else next month.

I am Gleb Bakhmutov. I used to be VP of Engineering. Now I'm just an engineer, because I don't want to do all the meetings and like roadmaps, and so on. So right now I'm so lucky, I can just code and do workshops and write blog posts and do things that I find interesting. You can always write me an email after the workshop or during. I'm Gleb at Cyprus. You can also always contact me on Twitter. I try to be, you know, reactive and always reply to whoever's writing to me. I have a lot of blog posts on Cyprus blog and on my personal blog. You'll find a lot of in-depth kind of explanations. And I also do little Cyprus videos on YouTube. I've been using Cyprus for close to five years because I worked on Cyprus for four years and I used it for a year before so I'm really happy about Cyprus.

We'll cover a lot of stuff. I mean, as time allows. I will post the link to the repo in all channels. We'll cover a lot of stuff. We'll cover the app. It's a simple to do and VC app so we don't have to spend much time on it. We'll cover basic deaths, that kind of interactive page. We'll see how we can select elements using selector playground and the new studio and how I would control the application before the test so that we all start application in a known state. And then we'll look at network calls and how we can spy stop them, stop them with data over picture data. I probably will not get to this section. Maybe I'll cover continuous integration. Maybe I'll mention something about retryability and debugging while we cover previous section, but this workshop, this repository has all this content. So if you wanna kind of look at what the features do, right, or how to do plugins, so how to set up code coverage, the material is there, but we'll just won't have time to get for it because this workshop can be long. We're not gonna take lunch, but we'll take short breaks. Usually this workshop we do physically, right? And people talk and can help because it's a virtual workshop. It's harder, right? But just use chats with your opinions and questions.

I found that a best workshop and getting the most out of it depends on three steps. So first I will go through the slides, I'll explain the topic, and then I'll show how to do a specific test, right? So I'll type it and I'll show like what happens inside Cypress and so on. And then inside each integration spec file, there'll be exercises, right? Like writing more tests one by one. And both things I encourage you to do by yourself and I'll do them later, right? And I'll kind of show the solution, but the best thing for the workshop is to be hands on. So I really encourage you to work on your machine, during this workshop, okay? And if you get stuck, again, pause the question in the discord or on zoom, right? And I'll try to help. But the best thing is kind of listen to what I explain and then code together as I code and then try to go for the next exercises. Okay, I already said that you will need to clone the repository and do NPM install. Larick can you help people because some people do not see the discourse channel. Okay. I guess we will duplicate. Excellent. So here's what we have in our repository. So if you look at all files, all right, we have a lot of stuff. The application is inside to do MVC, okay? So it's in the sub folder and this has its own packages. So if you don't PM install, it actually installs with dependencies there as well. It's the details are not that important about the application. Then we have Cypress integration folder. That's what all the test files will be, okay. And so for each section, right they're all on like named, zero, one, zero, two, they're all numeric one after another, whereas sub folders. So if I go and I look at, let's say Cypress integration you will see that the first kind of spec file will open is here. And then for next section is your two and so on. So every section we'll just work in a different folder and everything is independent. So you don't have to kind of finish what previous chapter to go to the next one. They're all independent. And while we are testing replication, replication would be just running. So we'll start it and it will just stay. Okay, so is everyone ready? Okay, I assume everyone's ready. So from the terminal, write one inside the repository. You can execute NPM start. And you should see this, right? If you install everything, if you have no, that's what you will see, right? It literally just started application and it should be running at localhost 3000. Okay, so we're gonna just run that application in the background at all times.

2. Starting with Cypress

Short description:

When working with the application, we observe network calls, such as GET and POST to the todos endpoint. The app sends the todo with its title and ID. Reloading the page fetches the items again. The data is stored in a data.json file. Cypress is used to test rest API calls. We start with Cypress by creating a new project, installing Cypress as a dev-dependency, and opening Cypress with NPX Cypress open.

Okay, so we're gonna just run that application in the background at all times. Okay, so it's a TodoMVC because that's the hardest application, almost difficult application I know how to code. If you have view, you can see the view interaction. It doesn't really matter how the application is implemented. We can run the CSS pretty much against any implementation because Cypress doesn't care about the permutation details, right? It does the browser, it does the UI, right? But user doesn't care how it's implemented. So why should Cypress?

Okay, let's open DevTools. So the one thing we wanna know right away is what happens when we'll work with the implementation, like this application, right? So the thing that I usually try to observe are like what are the network calls that happen when the application loads, right? So I can reload and I can see what we get in document, we get in a bunch of you know, scripts, right? And at the end, our application is making AJAX call, so we can inspect it. So our application is doing a GET to todos, right? And gets an empty list, cuz there are no todos. So inside of a page, we kinda have like standard mark markup, right? We have classes for the main section. We have a header, that's where we probably will have input box, and when we just play all to those in unordered list. And so for every todos, we'll just show it on the screen. So that's our markup. You can take a look at todo.mvc.app.js, it has a view store, but it's a implementation details. I don't really care. But I have a question for you, right? My audience. So what happens when you add a new todo item? So you can do it yourself. Let's type, you know, first to do. Okay, so when we press Enter, you can see that there is another call that application is making, and it's a POST, right? So it sends something to the todos endpoint, right? The server responds with 201, which means a new item was created. And what does the app send? Well, it sends the todo. And notice that the app sends the title, right? Which is first to do completed false. So by default, every item is incomplete and the frontend also sends the ID. So the app is creating the ID for the app, for to-do item, and sends it, right? So it's not even server-side, it's just client-side. And if we send second todo, right, I can see another call, okay, another ID, another item. And what happens when we reload? Okay, we can see that the items came back. We can see that this original call to get todos, right, fetches the items. So application, when it starts, fetches the items. Okay, so this is how it works. So it gets to the server by using Ajax calls. Now here's another question but you should look locally. You wanna figure out where the items were stored. So for this, I will switch to my code editor. Okay, so you can use any code editor, right? But I love VS Code nowadays. So it's so simple. Kareem, no, the presentation is not private. So if you go to the code, all right, you can follow along if you get lost or if you wanna spend more time, you can take this link and just click on any of the links. So right now I'm at the intro. Okay, so let's find where the application saves the data. All right, so it sends it, stores it, and then when we reload, we can see the data again. So it has to store somewhere. Okay, if you have troubles running application, make sure you have the right version of Node, at least 12, all right, and that you can execute npm start. One thing, it might be because this is using, where's our start, if you haven't troubles, maybe go directly into todo.nvc subfolder and do npm start there. Make sure you do npm install first. So if in my text editor, if I look around, right, and I go to todo.nvc, I can see this data JSON file. Okay, and yes, Martin, you're right. I'm not ignoring it because usually it's started with a simple, empty file. Okay, so this is where my server is storing the data. Let's verify it. Let's see, is it data JSON? Okay, so yeah, that's where it's at. Yeah, yeah, Lindsey, try installing dependencies again and trying to start on the one. All right, Nikolai, if you have a Grafana app, that's not me, that probably is some other app that you're running locally. Okay, so at startup, right, when application is starting, it's getting those items again, right, so we always return, okay? Okay, this is kind of a structure application and I'm only using it, you know, to kind of explain, but this is a modern application, right? It has a UI layer, this is all the markup, the input, the list of items, they're all there. Under the hood, the app is using Datastore and the Datastore is making rest API calls, so in this presentation, I will only talk about, kind of testing rest calls. They're a little bit simpler to test than GraphQL, you know, GraphQL is more advanced and just look at our documentation if you need to intercept and spy on GraphQL calls or other types of calls. Okay, I have a more kind of detail explanation of app and how you can test it, but for now, let's start with Cypress. So I finished the introduction and let me start with the next one.

Okay, so the first thing I wanna show is, you know, what happens when you just start with Cypress, right? I had the question actually, but it's kinda, you know, in the Zoom chat or in Discord, you know, how long have you used Cypress? How long have you used Cypress for, right? So, I use Cypress for longer than four years. I'm wondering if majority of people six months, three years, not yet one day. Excellent, so we have a range from just starting to veterans. Perfect, perfect, so everyone is welcome.

Okay, so this section is for people who just started Cypress, right, or wanna try. So, we'll write first test, we'll set up, and I'll explain documentation. So, for this test, we'll create a new folder, all right? So, you did install in, you know, to MVC in testing workshop, right? But here's what happens when you start from scratch. So, let me just show how it's happened. So, I'll do, I'll go to my temp folder, and I'll make a directory like first. And I'll make a directory like first-use-site. All right, and I'll switch. So, here's what happens if you just create a new project. Right, so I'm using NPM in here to quickly create package JSON. When I install Cypress and I save it as dev-dependency, it's not production-dependency, right? It's not something necessary for your app to run, right? No, but it's something that you want to install if you're developing, so that's why it's a dev-dependency. So, I'm installing Cypress, right? Using NPM install Cypress. Come on. Okay. So, because I already installed that version of Cypress, right, it found it, so I never have to re-download that big file. Okay, so what do we have here? Right now, just pack a JSON, right, and node modules. Now we wanna open Cypress and there are different ways. The best way, I would say, the most modern and portable way after doing PM install Cypress is to do this, NPX Cypress open, I suggest you do that with me. All right, NPX Cypress open. So, this is a folder where we just installed Cypress, we run it for the very first time. Okay. Notice that it shows that to help get you started, it created a bunch of files. Okay. So, here's what we see. Oh, by the way, sometimes I don't do NPX Cypress open. So, instead I just create scripts in my package file and then I can open with a command. And then I can just create a command. But first time you start a Cypress in a folder, right? It creates a bunch of files. So, the most kind of useful is cypress.json. Okay. Then you can follow the contents by looking at, for example, a markdown, and then you'll have all the files. Look all the commands and you can go back and forth.

Watch more workshops on topic

Designing Effective Tests With React Testing Library
React Summit 2023React Summit 2023
151 min
Designing Effective Tests With React Testing Library
Top Content
Featured Workshop
Josh Justice
Josh Justice
React Testing Library is a great framework for React component tests because there are a lot of questions it answers for you, so you don’t need to worry about those questions. But that doesn’t mean testing is easy. There are still a lot of questions you have to figure out for yourself: How many component tests should you write vs end-to-end tests or lower-level unit tests? How can you test a certain line of code that is tricky to test? And what in the world are you supposed to do about that persistent act() warning?
In this three-hour workshop we’ll introduce React Testing Library along with a mental model for how to think about designing your component tests. This mental model will help you see how to test each bit of logic, whether or not to mock dependencies, and will help improve the design of your components. You’ll walk away with the tools, techniques, and principles you need to implement low-cost, high-value component tests.
Table of contents- The different kinds of React application tests, and where component tests fit in- A mental model for thinking about the inputs and outputs of the components you test- Options for selecting DOM elements to verify and interact with them- The value of mocks and why they shouldn’t be avoided- The challenges with asynchrony in RTL tests and how to handle them
Prerequisites- Familiarity with building applications with React- Basic experience writing automated tests with Jest or another unit testing framework- You do not need any experience with React Testing Library- Machine setup: Node LTS, Yarn
Master JavaScript Patterns
JSNation 2024JSNation 2024
145 min
Master JavaScript Patterns
Featured Workshop
Adrian Hajdin
Adrian Hajdin
During this workshop, participants will review the essential JavaScript patterns that every developer should know. Through hands-on exercises, real-world examples, and interactive discussions, attendees will deepen their understanding of best practices for organizing code, solving common challenges, and designing scalable architectures. By the end of the workshop, participants will gain newfound confidence in their ability to write high-quality JavaScript code that stands the test of time.
Points Covered:
1. Introduction to JavaScript Patterns2. Foundational Patterns3. Object Creation Patterns4. Behavioral Patterns5. Architectural Patterns6. Hands-On Exercises and Case Studies
How It Will Help Developers:
- Gain a deep understanding of JavaScript patterns and their applications in real-world scenarios- Learn best practices for organizing code, solving common challenges, and designing scalable architectures- Enhance problem-solving skills and code readability- Improve collaboration and communication within development teams- Accelerate career growth and opportunities for advancement in the software industry
How to Start With Cypress
TestJS Summit 2022TestJS Summit 2022
146 min
How to Start With Cypress
Featured WorkshopFree
Filip Hric
Filip Hric
The web has evolved. Finally, testing has also. Cypress is a modern testing tool that answers the testing needs of modern web applications. It has been gaining a lot of traction in the last couple of years, gaining worldwide popularity. If you have been waiting to learn Cypress, wait no more! Filip Hric will guide you through the first steps on how to start using Cypress and set up a project on your own. The good news is, learning Cypress is incredibly easy. You'll write your first test in no time, and then you'll discover how to write a full end-to-end test for a modern web application. You'll learn the core concepts like retry-ability. Discover how to work and interact with your application and learn how to combine API and UI tests. Throughout this whole workshop, we will write code and do practical exercises. You will leave with a hands-on experience that you can translate to your own project.
Integrating LangChain with JavaScript for Web Developers
React Summit 2024React Summit 2024
92 min
Integrating LangChain with JavaScript for Web Developers
Featured Workshop
Vivek Nayyar
Vivek Nayyar
Dive into the world of AI with our interactive workshop designed specifically for web developers. "Hands-On AI: Integrating LangChain with JavaScript for Web Developers" offers a unique opportunity to bridge the gap between AI and web development. Despite the prominence of Python in AI development, the vast potential of JavaScript remains largely untapped. This workshop aims to change that.Throughout this hands-on session, participants will learn how to leverage LangChain—a tool designed to make large language models more accessible and useful—to build dynamic AI agents directly within JavaScript environments. This approach opens up new possibilities for enhancing web applications with intelligent features, from automated customer support to content generation and beyond.We'll start with the basics of LangChain and AI models, ensuring a solid foundation even for those new to AI. From there, we'll dive into practical exercises that demonstrate how to integrate these technologies into real-world JavaScript projects. Participants will work through examples, facing and overcoming the challenges of making AI work seamlessly on the web.This workshop is more than just a learning experience; it's a chance to be at the forefront of an emerging field. By the end, attendees will not only have gained valuable skills but also created AI-enhanced features they can take back to their projects or workplaces.Whether you're a seasoned web developer curious about AI or looking to expand your skillset into new and exciting areas, "Hands-On AI: Integrating LangChain with JavaScript for Web Developers" is your gateway to the future of web development. Join us to unlock the potential of AI in your web projects, making them smarter, more interactive, and more engaging for users.
Detox 101: How to write stable end-to-end tests for your React Native application
React Summit 2022React Summit 2022
117 min
Detox 101: How to write stable end-to-end tests for your React Native application
Top Content
WorkshopFree
Yevheniia Hlovatska
Yevheniia Hlovatska
Compared to unit testing, end-to-end testing aims to interact with your application just like a real user. And as we all know it can be pretty challenging. Especially when we talk about Mobile applications.
Tests rely on many conditions and are considered to be slow and flaky. On the other hand - end-to-end tests can give the greatest confidence that your app is working. And if done right - can become an amazing tool for boosting developer velocity.
Detox is a gray-box end-to-end testing framework for mobile apps. Developed by Wix to solve the problem of slowness and flakiness and used by React Native itself as its E2E testing tool.
Join me on this workshop to learn how to make your mobile end-to-end tests with Detox rock.
Prerequisites- iOS/Android: MacOS Catalina or newer- Android only: Linux- Install before the workshop
Using CodeMirror to Build a JavaScript Editor with Linting and AutoComplete
React Day Berlin 2022React Day Berlin 2022
86 min
Using CodeMirror to Build a JavaScript Editor with Linting and AutoComplete
Top Content
WorkshopFree
Hussien Khayoon
Kahvi Patel
2 authors
Using a library might seem easy at first glance, but how do you choose the right library? How do you upgrade an existing one? And how do you wade through the documentation to find what you want?
In this workshop, we’ll discuss all these finer points while going through a general example of building a code editor using CodeMirror in React. All while sharing some of the nuances our team learned about using this library and some problems we encountered.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

Scaling Up with Remix and Micro Frontends
Remix Conf Europe 2022Remix Conf Europe 2022
23 min
Scaling Up with Remix and Micro Frontends
Top Content
This talk discusses the usage of Microfrontends in Remix and introduces the Tiny Frontend library. Kazoo, a used car buying platform, follows a domain-driven design approach and encountered issues with granular slicing. Tiny Frontend aims to solve the slicing problem and promotes type safety and compatibility of shared dependencies. The speaker demonstrates how Tiny Frontend works with server-side rendering and how Remix can consume and update components without redeploying the app. The talk also explores the usage of micro frontends and the future support for Webpack Module Federation in Remix.
Network Requests with Cypress
TestJS Summit 2021TestJS Summit 2021
33 min
Network Requests with Cypress
Top Content
Cecilia Martinez, a technical account manager at Cypress, discusses network requests in Cypress and demonstrates commands like cydot request and SCI.INTERCEPT. She also explains dynamic matching and aliasing, network stubbing, and the pros and cons of using real server responses versus stubbing. The talk covers logging request responses, testing front-end and backend API, handling list length and DOM traversal, lazy loading, and provides resources for beginners to learn Cypress.
Testing Pyramid Makes Little Sense, What We Can Use Instead
TestJS Summit 2021TestJS Summit 2021
38 min
Testing Pyramid Makes Little Sense, What We Can Use Instead
Top Content
Featured Video
Gleb Bahmutov
Roman Sandler
2 authors
The testing pyramid - the canonical shape of tests that defined what types of tests we need to write to make sure the app works - is ... obsolete. In this presentation, Roman Sandler and Gleb Bahmutov argue what the testing shape works better for today's web applications.
Full Stack Components
Remix Conf Europe 2022Remix Conf Europe 2022
37 min
Full Stack Components
Top Content
RemixConf EU discussed full stack components and their benefits, such as marrying the backend and UI in the same file. The talk demonstrated the implementation of a combo box with search functionality using Remix and the Downshift library. It also highlighted the ease of creating resource routes in Remix and the importance of code organization and maintainability in full stack components. The speaker expressed gratitude towards the audience and discussed the future of Remix, including its acquisition by Shopify and the potential for collaboration with Hydrogen.
Debugging JS
React Summit 2023React Summit 2023
24 min
Debugging JS
Top Content
Watch video: Debugging JS
Debugging JavaScript is a crucial skill that is often overlooked in the industry. It is important to understand the problem, reproduce the issue, and identify the root cause. Having a variety of debugging tools and techniques, such as console methods and graphical debuggers, is beneficial. Replay is a time-traveling debugger for JavaScript that allows users to record and inspect bugs. It works with Redux, plain React, and even minified code with the help of source maps.
Making JavaScript on WebAssembly Fast
JSNation Live 2021JSNation Live 2021
29 min
Making JavaScript on WebAssembly Fast
Top Content
WebAssembly enables optimizing JavaScript performance for different environments by deploying the JavaScript engine as a portable WebAssembly module. By making JavaScript on WebAssembly fast, instances can be created for each request, reducing latency and security risks. Initialization and runtime phases can be improved with tools like Wiser and snapshotting, resulting in faster startup times. Optimizing JavaScript performance in WebAssembly can be achieved through techniques like ahead-of-time compilation and inline caching. WebAssembly usage is growing outside the web, offering benefits like isolation and portability. Build sizes and snapshotting in WebAssembly depend on the application, and more information can be found on the Mozilla Hacks website and Bike Reliance site.