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
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
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
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
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
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.
Comments