Full-Circle Testing With Cypress


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



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, 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 drive 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 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. But 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 TestJS 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'll 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. Visiting an application. Visiting an application and then 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 home page, 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. And 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 4000 milliseconds, so for 4 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. And 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 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 sort of travel back in time and examine the application as it was looking while the test was being executed. So this is pretty neat. This gives you an insight and especially if you are 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. And oops, sorry, that was not the correct test. We want to run the API testing. All right. So now we have it. 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, 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 kind of not so good, right? So what I did with that is I got inspired by the open source community and namely Cleb Bafmuto who has built a plugin 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 CYRequest. 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 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 are testing the API with our shoot command and with a 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 sends 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. Here 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 signup page. And then I'm filling in the email and the password. And I'm checking this nice little checkbox. So let me show that to you. 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 checkbox. And what I can do with my request is to access the mailbox that I'm using with an API request. So there are different kind of tools that enable you to access your APIs with HTTP requests. So one of that is TestMail, but we got MailSAR, MailSlurp, and other services that provide this feature. So that's exactly what I'm doing in this test. Although it may look like a little bit complicated, it's actually not. I'm just generating a random number over here. I'm assigning it to a variable or an alias. And then I'm using this alias as a part of my email address so that it can be identified on the server. And then also using that tag in the request itself as a query string. So when I send that request, I get the contents of the inbox as a response. And I'm checking the body emails. And I'll check the subject of that email to be Welcome to Trello App, which is exactly the subject of that email. So yeah, that's pretty powerful. But you probably noticed that when I'm going over to timeline, that Cypress actually takes notice of all the API requests that are being done by the application itself, which brings me to another area of API testing, which is network layer testing. So not only we can send requests, we can actually observe the request that our application is doing. And let me show you on the example that I have prepared for you. So I'll open another test that's going to be network. And I'll filter out the first test using data only. Maybe you know this trick, maybe you don't. But if you do that only, you can filter out the only tests you want to run. So let me save that, open the test. And you can now see it in action. So what's happening here is my test is actually failing. So let me walk you through that. Inside this test, I'm using the intercept command, which when I explain this on my workshops, is something that tells Cypress to watch for a certain request to happen and to be called by our application. So this request is actually being called, the post API boards is actually being called when we create a new board on our page. So when I go to my homepage, fill in the details and hit enter key, that's where the API is being called. That's exactly the place where that happens. And we have that highlighted in Cypress. I have aliased it with a name of create board. And here is the alias. So what I do after I do the action, after I type in new board and hit enter, is that I wait for this API request to happen. And then I'm testing the request body name to have a certain text. And as you can see, this test is actually failing because my input is adding a white space after my input. So whenever I hit the enter key, it will register that as a new line and it will add that slash and sign into that, which is not good because I'm polluting my database. This demonstrates that I can not only test the response like to go one, two, or two or something like that, 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 an 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 a longer time and I have this reload button being shown in the application, which normally would be a 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. 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 a 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 Spina, it's written in view and for store management, it used this Spina 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 of that 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 getBordList 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. So 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. We would basically need to reload the application to make that getAPIBoards call to be triggered again. But with AppActions, we actually don't have to do that because we will invoke the store and then call the getBordList 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 getBordList, 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 this nice neat API tools available for me here and I access this using the invoke and using the internal store of the application. So 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. As 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 the 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 versions 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 message 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 sub page. We can even render out sub components. So this drop down items actually contains some slots with components in them and yeah, 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 the browser and we can interact with them, we can test them as you can see and we get all of the API of Cypress 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 test, we can send requests, we can add, we can change the state 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 a 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 TestJS Summit in here or in November on my other workshop. You can find the link at the bottom of this presentation. For now, I will be looking very much to hear your questions. See you in a bit.
27 min
03 Nov, 2022

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

Workshops on related topic