Full-Circle Testing With Cypress

Rate this content
Bookmark

Cypress has taken the world by storm by brining an easy to use tool for end to end testing. It’s capabilities have proven to be be useful for creating stable tests for frontend applications. But end to end testing is just a small part of testing efforts. What about your API? What about your components? Well, in my talk I would like to show you how we can start with end-to-end tests, go deeper with component testing and then move up to testing our API, circ

27 min
03 Nov, 2022

Video Summary and Transcription

Cypress is a powerful tool for end-to-end testing and API testing. It provides instant feedback on test errors and allows tests to be run inside the browser. Cypress enables testing at both the application and network layers, making it easier to reach different edge cases. With features like AppActions and component testing, Cypress allows for comprehensive testing of individual components and the entire application. Join the workshops to learn more about full circle testing with Cypress.

1. Introduction to Full Circle Testing with Cypress

Short description:

Hey everyone, I'm a tester with maybe some developer skills or developer with testing skills. I'm also a Cypress Ambassador, and today I'll be talking about full circle testing with Cypress. Let's take a look at what that means. Cypress shines in end-to-end testing, and if you've never used it, I recommend checking out the workshop. There's also a workshop coming up at test.js summit. Let's dive into Cypress and how to write a test with it.

Hey everyone, I'm glad to be here. My name is Filip and I'm a tester with maybe some developer skills or developer with testing skills. I don't know. It depends how you look at it. I'm also a Cypress Ambassador, which means I like to talk about Cypress and I love to teach people how to use this amazing tool. So that's what I'll be doing today.

The topic of today's talk is full circle testing with Cypress. So what that means. Well, it's a completely made up term. I made it up to make this talk a little interesting and hope that you will take a look at it. But I do have an idea that's behind it that I would like to share with you. So let me draw a circle or a ring. But don't worry, you're not going to die when you look at it, hopefully. But hopefully you'll learn something new. And yeah, let's let's take a look.

So as you know, there's something that's called testing pyramid, which I'm not going to draw here because everyone knows what it is. It also contains something that might be at the top of the pyramid, and which is something that I'm going to start today's talk with. And that's end-to-end testing. So when talking about Cypress, this is where Cypress shines the most and this is what Cypress is mostly known for. So if you have never used Cypress, I recommend you to check out the workshop. I have a link at the bottom of this slide. But also there is going to be a workshop on test.js summit in just a couple of days. So make sure you check it out if you still can, hopefully you can. And we can meet there and you can learn to work with Cypress with me. So if you can make it to that one, make sure to check out my website, and you will find one at the end of November.

So let's take a look into Cypress and how you can actually write a test with Cypress. So on the right side I have my Cypress running in graphic user interface mode, so that would be the one you run with npx-cypress-open in your terminal. And on the left, I have a test which I'm going to show you later. I want to show you this one right now, the end-to-end test. So, inside this test, I'm doing a simple flow.

2. Full Circle Testing and API Testing in Cypress

Short description:

I'm visiting an application and performing various actions like selecting elements, typing, and interacting with the page. The flow automates going to the homepage, creating a new board, list, and card. Cypress provides instant feedback on test errors and allows us to run tests inside the browser, taking advantage of browser features like the console. We can also examine the application as it appeared during test execution, making it easier to find hard-to-locate elements. Cypress encourages API testing using the 'request' command.

I'm visiting an application, and then I'm selecting an element, typing into it, hitting enter, and then doing some actions again. Getting an element, clicking, typing, interacting with my page. Basically, what I want to do is to go to my homepage, create a new board, then create a new list, and inside it, create a new card. And this is what the flow automates.

So, whenever I make an error, for example, I change my selector to not be found, and when I save my test, it will automatically rerun. If I make an error during my test, I get this really nice assertion error explaining to me what actually happened. So in this case, what actually happened is that it couldn't find the selector because I made a typo on it and it was nowhere to be found. Cypress is actually going to retry searching for that selector for 4,000 milliseconds, so for four seconds.

Now, this means that Cypress works really great for the applications that are all the time re-rendering and doing some sort of fetching from API, etc. as modern applications do. So when you are testing, you get this instant feedback into what your test has done. But also, the important thing here is that we are running inside the browser. So while we are doing that, we get all of the advantages that the browser will provide us. So for example, we can open up a console and take a look into what's happening inside the console. So I'm getting a couple of warnings here, but these are not that important. Cypress actually takes a great advantage of the console and of the fact that we are running inside the browser. So when I click on any of my commands, for example this visit command, Cypress will print out information into the console with the details of that command. It will also print out information for every HTTP request that is being done with the application. So let's maybe fix the test, save it to make it pass. And also, what Cypress enables us, as you can see, is to travel back in time and examine the application as it was looking while the test was being executed. This is pretty neat, this gives you an insight, especially if you're looking for a selector that is kind of hard to find because the element appears and then it disappears. This is a really, really helpful tool. So you can see different API calls, and Cypress sort of invites you to get into the application and examine the application and get to know its nuts and bolts. So a great area to start examining your application a little bit more is to do API testing, and this is actually a second part of our circle of tests.

So when we are doing API testing in Cypress, we can use a command called request, so let me show you that one. And let's change it because I'm going to show you the API command in just a second. So let me save my test and let me open that in my Cypress runner. Oops, sorry, that was not the correct test. We want to run the API testing. So now we have it.

3. API Testing with Cypress

Short description:

I'm using the request command to send a POST request to the URL of API boards. I'm checking the status of the response to ensure it is 201. I created a plugin inspired by the open source community that adds a new command to the Cypress library called API. It prints out information about the request in the browser, including the method, URL, status, request body, and response body. Cypress is a great tool for both end-to-end testing and API testing. We can combine these two worlds together and call any API we want.

So inside here, what I'm doing is using the request command to send a POST request to the URL of API boards. So as you can see the syntax of my test is pretty simple. I'm using the CY global object which is available throughout the whole test suite and I'm using the request command that is there for sending API requests. So of course what I'm doing is sending the POST API and here is the body that I'm sending towards the server.

Now when I get a response, I take with the its command and I'll take the status and make sure that it is 201. Now how do I know to look for the status and for the contents of the response? Well the first way of how I can look into that is to click on my command and just as I've shown you a couple of seconds ago, open the console and examine what the command has yielded. So with my its command I have actually taken out the status and then checked whether the number of that status is 201, so it is and my test is passing.

So this is really cool, right? But I see a little problem here. Maybe you see that too. We have that big white space over here which is not used for anything. So that's that's kind of not not so good, right? So what I did with that is I got inspired by the open source community, and namely Clep Bakhmuto who has built a plug-in for showing the API in the browser. And I have made sort of my own version of that. So I have this installed in my repository and what this plugin will do is that will add a new command to the Cypress library. So it's called API and it works exactly the same as CY-request. So when I save my test, it will print out the information about the request into the browser. So I can see the method, I can see the URL, also the status and I also see the request body and response body and all that information. I can even copy the contents of the response body to the clipboard and maybe I can just try to, try to write a test so let's go with its body and let's do deep, equal and I'll paste my object over here. I think that should work. Let me save that and yeah. We're good. So we're testing the API with our shoot command and with visual feedback that we got here. So as I said, Cypress is known for end-to-end testing and as you can see, it's a great tool even for API testing. So what we can do is we can combine these two worlds together. We can do end-to-end testing and API testing. And since all of the requests that Cypress sent are actually sent from Node so they are not sent from out the browser, what we can do is to call any API we want. So actually I have a test over here, let me show you that one, where I'm testing an email. So let me open that one and let's explain what's happening in here. So I'll go through the timeline so I can travel back in time and guide you through what's happening. So I'm visiting a sign-up page and then I'm filling in the email and the password and I'm checking this nice little check box. So let me show that to you.

4. Testing Application Layer with Cypress

Short description:

This application allows users to receive a welcome email by checking a checkbox. With Cypress and the intercept command, I can test if the application is sending the proper data to the server. I can also manipulate the server response, such as changing the body, status, and response time. By testing the network and application layers, Cypress enables us to reach different edge cases. For example, using App Actions, I can access the application state and invoke functions that mutate the store. However, reloading the application may be necessary to trigger certain API calls again.

And this says send me a welcome email. So what this application will do is that it will actually send you an email if you check that check box. And what I can do, I can actually test whether the application is doing what it should be and whether it is sending the proper data to the server, which is great because now for API testing, I'm not using the direct requests that I know how to prepare, but I'm actually testing whether the application is sending the proper data to the server and checking if it's not sending some corrupted data or doesn't handle the inputs properly as it does here.

So what Cypress and the intercept command actually enables me as well is to check for different edge cases. Not only I can observe a API request happen, I can also sort of cut off the server response and provide my own. So what I can do, I can change the body, I can change the response status. I can even change the response time, which is exactly what I'm doing over here. So I have a callback and I'm handling the reply to sort of be delayed by 10 seconds. So when I now save my test, you can see that the API boards request is actually taking and I have this reload button being shown in the application, which normally would be really hard place to sort of reach and to test. But with Intercept, we can actually show that and see if it behaves properly, if it appears after three seconds, as it should, and if it works properly. So we can actually click on it and make sure that our application will reload.

So with the testing of network layer, we can sort of get to different hard-to-reach edge cases. And one of the great examples of how we can reach different edge cases is testing the application layer. So that's what Cypress enables you to do as well, and that's what I want to show you. So in this example, I'm using something that's called App Actions. So let me change this test a little bit, save it now, and run it. All right, I got it. So, Cypress takes a great advantage of the fact that it is running in the browser. I've already shown you a couple of those, like printing out things into the console, taking notice of what the application is doing on the network, but also, you can take a look directly into the application state. So let me show you in the application I have this store.ts, which this application uses PNIA. It's written in Vue and for store management it used this pnia package and it uses this use store. So inside here I have all of the state or all of the default state defined and I also have defined some of the actions that mutate this state. Now what I have done with my application to make it more testable is that whenever we are testing, whenever there is Cypress on the window context, we are exposing the use store, so the whole state of the application, to the store variable on the window object. So what this enables me to do is to access the properties of the store and even the functions that mutate the store right from the window context. So in Cypress I have this cyWindow command which access the window object and then I can invoke different functions. So here I'm invoking the store and then I'm invoking the getBoredList function which actually triggers the API call for fetching all of the boards from the database. Inside here I also have a pause function so maybe even if you are using Cypress you haven't heard of that one but it's super useful when I'm demonstrating something. Inside here I can just go step by step and move on to the next command which is API call. So I'm creating a new board but as you can see it is not appearing in our application and this is because we don't have some sort of WebSocket connection to the database or with the server that would update the contents of our application. So we would basically need to reload the application to make that getAPIBoards call to be triggered again.

5. AppActions and Component Testing in Cypress

Short description:

With AppActions, we can access hidden parts of the application and manipulate them to reach hard-to-reach edge cases. Cypress also offers component testing, allowing us to test different states of individual components. We can render components in a real browser, interact with them, and use Cypress APIs to test different states. This approach allows us to achieve full circle testing by combining component testing with end-to-end testing. Join my workshops to learn more!

But with AppActions, we actually don't have to do that because we will invoke the store and then call the getBoardList action which is responsible for fetching all of that data from the server and which is the function that gets called whenever we open the application. So let me go to the next step. We get the window, we get the store and now we call the getBoardList which will trigger this API call and our board is now loaded. So with AppActions, we can access all kinds of stuff. We can even access the things that are hidden from the application. So let me comment this out and show you this. Because I have some hidden API tools that I made for this application and now I'm basically calling toggleTools to appear and I have these nice neat API tools available for me here and yeah, I accessed this using the invoke and using the internal store. This is a huge advantage compared to maybe different test automation tools because since we are running in the browser we have direct access to the application, that's opposed to tools that sort of drive the browser and access the browser sort of from outside to automate actions on the browser. Now of course this has some advantages and it has some disadvantages but I view this as a really big win for Cypress when it can basically access any part of your application that you give the authority to and manipulate it in a way that helps you reach maybe a hard to reach edge case or some part of your application which might be hard to set up because of the data or because of some other factors. Now Cypress is of course a great tool for getting into the app and testing different states and making the app do stuff so we can test it in a better way but we can dive even deeper and break down our application into smaller chunks. So Cypress has released this killer feature called component testing. So it recently got out of beta, it has been here for quite some time but with version of Cypress 10 it reached the whole crowd. So I want to show you this because our application is sort of built from different small components that make our application. I like to think about it as sort of Lego blocks that are used to build the application. So in here I'm basically using cyMount function that is going to render my component and I'm passing different properties into here. So if I type hello world, my button over here is going to render hello world. If I type hello test.js, it's going to render out this. So I can test different kinds of states of my component and it doesn't have to be button, we can go to something more complicated. So for example, we can test different states of our notification in here, test an info or an error message, or we can go to our navigation bar, click this home icon and see if it behaves differently when we are at home or when we are at some other subpage. We can even render out subcomponents, so this dropdown items actually contains some slots components in them and you can go basically anywhere with this. The biggest advantage of this one is that we get to render our components inside a real browser. So this is exactly how our components would be rendered inside a browser. We can interact with them, we can test them as you can see, and we get all of the api of cyprus enabled for us so we can test different states. If the component calls an API, we can use intercept to sort of change the state. We can send requests, we can change the state of the of the app using the app action that I have just shown you. Or we can just render multiple components and see how they interact with one another. But if you are doing that, you kind of reached the full circle and go back to end-to-end testing. So you can go all around as many times as you want, and hopefully you get good coverage of your application. This has been really fun. So thank you very much for listening this far. I will be really eager to meet you on one of my workshops, whether on test.js summit or in November on my other workshop. You can find the link at the bottom of this presentation. And now I will be looking very much to hear your questions.

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

TestJS Summit 2021TestJS Summit 2021
33 min
Network Requests with Cypress
Top Content
Whether you're testing your UI or API, Cypress gives you all the tools needed to work with and manage network requests. This intermediate-level task demonstrates how to use the cy.request and cy.intercept commands to execute, spy on, and stub network requests while testing your application in the browser. Learn how the commands work as well as use cases for each, including best practices for testing and mocking your network requests.
TestJS Summit 2021TestJS Summit 2021
38 min
Testing Pyramid Makes Little Sense, What We Can Use Instead
Top Content
Featured Video
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.
TestJS Summit 2021TestJS Summit 2021
31 min
Test Effective Development
Top Content
Developers want to sleep tight knowing they didn't break production. Companies want to be efficient in order to meet their customer needs faster and to gain competitive advantage sooner. We ALL want to be cost effective... or shall I say... TEST EFFECTIVE!But how do we do that?Are the "unit" and "integration" terminology serves us right?Or is it time for a change? When should we use either strategy to maximize our "test effectiveness"?In this talk I'll show you a brand new way to think about cost effective testing with new strategies and new testing terms!It’s time to go DEEPER!
TestJS Summit 2022TestJS Summit 2022
20 min
Testing Web Applications with Playwright
Testing is hard, testing takes time to learn and to write, and time is money. As developers we want to test. We know we should but we don't have time. So how can we get more developers to do testing? We can create better tools.Let me introduce you to Playwright - Reliable end-to-end cross browser testing for modern web apps, by Microsoft and fully open source. Playwright's codegen generates tests for you in JavaScript, TypeScript, Dot Net, Java or Python. Now you really have no excuses. It's time to play your tests wright.
TestJS Summit 2023TestJS Summit 2023
21 min
Everyone Can Easily Write Tests
Let’s take a look at how Playwright can help you get your end to end tests written with tools like Codegen that generate tests on user interaction. Let’s explore UI mode for a better developer experience and then go over some tips to make sure you don’t have flakey tests. Then let’s talk about how to get your tests up and running on CI, debugging on CI and scaling using shards.

Workshops on related topic

React Summit 2023React Summit 2023
151 min
Designing Effective Tests With React Testing Library
Featured Workshop
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
TestJS Summit 2022TestJS Summit 2022
146 min
How to Start With Cypress
Featured WorkshopFree
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.
React Summit 2022React Summit 2022
117 min
Detox 101: How to write stable end-to-end tests for your React Native application
WorkshopFree
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
TestJS Summit 2023TestJS Summit 2023
48 min
API Testing with Postman Workshop
WorkshopFree
In the ever-evolving landscape of software development, ensuring the reliability and functionality of APIs has become paramount. "API Testing with Postman" is a comprehensive workshop designed to equip participants with the knowledge and skills needed to excel in API testing using Postman, a powerful tool widely adopted by professionals in the field. This workshop delves into the fundamentals of API testing, progresses to advanced testing techniques, and explores automation, performance testing, and multi-protocol support, providing attendees with a holistic understanding of API testing with Postman.
1. Welcome to Postman- Explaining the Postman User Interface (UI)2. Workspace and Collections Collaboration- Understanding Workspaces and their role in collaboration- Exploring the concept of Collections for organizing and executing API requests3. Introduction to API Testing- Covering the basics of API testing and its significance4. Variable Management- Managing environment, global, and collection variables- Utilizing scripting snippets for dynamic data5. Building Testing Workflows- Creating effective testing workflows for comprehensive testing- Utilizing the Collection Runner for test execution- Introduction to Postbot for automated testing6. Advanced Testing- Contract Testing for ensuring API contracts- Using Mock Servers for effective testing- Maximizing productivity with Collection/Workspace templates- Integration Testing and Regression Testing strategies7. Automation with Postman- Leveraging the Postman CLI for automation- Scheduled Runs for regular testing- Integrating Postman into CI/CD pipelines8. Performance Testing- Demonstrating performance testing capabilities (showing the desktop client)- Synchronizing tests with VS Code for streamlined development9. Exploring Advanced Features - Working with Multiple Protocols: GraphQL, gRPC, and more
Join us for this workshop to unlock the full potential of Postman for API testing, streamline your testing processes, and enhance the quality and reliability of your software. Whether you're a beginner or an experienced tester, this workshop will equip you with the skills needed to excel in API testing with Postman.
TestJS Summit - January, 2021TestJS Summit - January, 2021
173 min
Testing Web Applications Using Cypress
WorkshopFree
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.
TestJS Summit 2023TestJS Summit 2023
148 min
Best Practices for Writing and Debugging Cypress Tests
Workshop
You probably know the story. You’ve created a couple of tests, and since you are using Cypress, you’ve done this pretty quickly. Seems like nothing is stopping you, but then – failed test. It wasn’t the app, wasn’t an error, the test was… flaky? Well yes. Test design is important no matter what tool you will use, Cypress included. The good news is that Cypress has a couple of tools behind its belt that can help you out. Join me on my workshop, where I’ll guide you away from the valley of anti-patterns into the fields of evergreen, stable tests. We’ll talk about common mistakes when writing your test as well as debug and unveil underlying problems. All with the goal of avoiding flakiness, and designing stable test.