Testing Web Apps with Cypress

Range Sliders & Select Lists

Steve Kinney

Steve Kinney

Testing Web Apps with Cypress

Check out a free preview of the full Testing Web Apps with Cypress course

The "Range Sliders & Select Lists" Lesson is part of the full, Testing Web Apps with Cypress course featured in this preview video. Here's what you'd learn in this lesson:

Steve demonstrates how to test complex inputs like range sliders and select boxes. The Cypress API offers methods for choosing values in a select box. For range sliders, invoking the jQuery val method allows the test to set a specific value.


Transcript from the "Range Sliders & Select Lists" Lesson

>> One of the things that we kind of just alluded to that we haven't really talked about is input fields. Yeah, the normal one is super easy, you type into it, right? What do you do with a range or like a select, right? Clearly, you're going to need to be able to use these.

And so, there's different kinds of ways for each of them, and then there's the kind of there's typing into input field, super easy, right? Maybe we need something for checking a checkbox or radio button, right? Or something along those lines, probably a select, like a drop down, right?

And then we have the kind of nuclear option, right, for changing one. So let's I'm gonna do a few of them and then I have that input obstacle course where you can try a number of other ones. And then we'll kinda look at them and see how we might work with a few of these as we go through.

So, if I go into other inputs, specs, let me just swap that over in Cypress. And these will pass, right? Here I found some data set that the reference sources at the bottom of the page, if you wanna find the data set itself. But I found this data set, and it was apparently, all of the secret menu items at a number of fast food chains, right?

So not only will you learn Cypress today, you will learn that Chick-fil-A has an ice dream, which is a different kid's meal prize, apparently, just getting a two cheeseburger meal is the McDonald's secret menu item. I grew up in a world where that was just on the menu, apparently, it's not.

But anyway, at your leisure, during lunch, after you've eaten or something along those lines, you can peruse this list and possibly, learn something new. The more important part for our purposes is it's got a few of those squirrely input fields, right? It's got me filtering just by a restaurant, right?

Or I've got some checkboxes where I can hide columns or I can filter by a minimum rating. I believe that the only thing reading it's popularity that only a Butterbeer Frappuccino reaches the tier of a level seven is popularity rating, nothing else reaches that. Again, I will leave it to you to probably spend more time exploring the list itself than the Cypress implications around it.

But what I am going to seek to do right now is at least control these three sets of different inputs, and you can guess where this goes next, right? But this should cover most of the things that you need to know in order to kind of work with these.

So let's try it out. So if I go over, they pass cuz they're not doing anything. So, yeah, no ops are great. I do have some aliases, right? I did grab on to the minimum rating visibility that was that range filter, as well as the drop down, which is that select, which is the restaurant filter.

There's a bunch of textboxes, so like what else, just grab one at some point. So we have that in place. So actually, we're gonna do these a little bit of a reverse order. Let's do the select, right? Cuz that probably gonna be the first, yeah, with checkboxes you can text them to click on them.

But as you saw, we did before with the labels and that's how it kind of works, but let's go with the select of this point. So I can do side I get, and we'll say restaurant-filter, right? Which is that alias that we made up at the top, and we will select I'm not ready for the tweets, the best fast food chain in existence don't ask me.

Tanner is making faces like getting vegan Taco Bell is a matter of personal pride even when I'm not eating vegan, it's doable. So we wanted to go ahead and we want to select it, right? And then you might want also verify that in this case, if you select it, that's the value that it has.

Sometimes you want the act of changing it and sometimes you want to assert that it hasn't given value. In a later one, we will literally create a battalion of tests that goes through each one and then verifies that the stuff on the page is correct. But for now, I want to set it and then I wanna verify that Cypress didn't lie to me.

And that it actually if I go get that value, that it's what I think it is. So then we'll go and we'll get the same restaurant filter. And then I want to say, listen, if I select Taco Bell, it should have the value Taco Bell Right? So that's both how to change a select field and then how to read it.

It was verify that I didn't lie to you, you can argue you said Taco Bell was good, thereby you lied to me, that's I'm not doing that right now, I'm only talking about Cypress. So we can also do something with the checkbox, right? So this is kind of interesting, because I might want to, again, assert things.

We know that we can click on the checkbox, but you might also wanna assert. Like in HTML, a checkbox has an action accurate attribute called checked or it's either checked or not checked. And when we get to the other thing later, we'll see that radio buttons, you might wanna know different things about them as well.

So in this case, I'm just gonna get a checkbox, I don't really care which one. And careful viewers will notice I'm gonna do something tricky with the alias as well that I didn't show you before. So go ahead and get me an input of the type checkbox, in this given case, I don't care which one.

Save it cuz I'm gonna need to look at it later, right, as a checkbox, and then check it, right? So the active saving as an alias is effectively almost like a tap or a pass through, all right? And so, I can continue chaining it along like this. And then I could say, The actual checkbox, and I could say should be checked.

If you check the checkbox, if I didn't make up this check method, right? Then the act of calling it should in fact, check the checkbox in this case, right? Cool, so we've got that in there as well. One of the things I'm kinda curious about if I could do is, again, slowly, It gonna chain it like that, you can argue, who knows why I would do that?

But, yes, that'll work as well. So this chaining all the time is kinda interesting, you can a lot of times keep the chain going, you can verify, you can do more stuff. You can make another assertion, you can do some more stuff, you can make another assertion. And that's what we're talking about before, the thing you were told the unit test is break them down into small pieces, one little setup, one little action, one little verification.

Here we can go through an entire user flow in a chain and kind of what work through everything as well. So sometimes alias is the right choice, sometimes getting comfortable with the chain, but sometimes you start with alias and you refactor it, right? Like these are all options.

My job is to tell you that they exist, and kinda point you in the different set of trade offs. All right, now the problematic one. So some of them, yeah, it was great that a select field had a method called select. Awesome, neat. It was wonderful that the checkbox had the method called check.

I don't even know what you would name one on range, right? And so, this one's a little bit tricky. And even if you were writing react or something along those lines or spelled or what have you, you would probably listen for a change event, but you'd have to set the value.

Well, that's kind of the answer, right, and how you were doing your regular code. There isn't a useful helper method out of the box later, we'll talk about you can make your own. But there isn't one in this case, so effectively when all else fails and you do not have a check method or a select method or what have you.

It is basically not dissimilar from what you would do in your react spelled Angular code, or whatever, but we'd like jQuery, right? Or what would you do with jQuery, right? Which is effectively set the value and then trigger the event, right? And that should update the terms, code, to kinda make all of those things happen.

So now we just have to talk about how we will do that, and so, in this case, we'll just cy a get writing filter. And we will say, we know that invoke calls a method on the other underlying object but puts it back in the chain. So we'll say invoke val, which is the jQuery method for with no argument getting you the value with a second argument setting the value, cuz we don't talk about enough, jQuery has the best API ever.

No, I do not want to install Docker desktop every day.
>> [LAUGH]
>> And then I can trigger, it's kind of up to you, depending on the implication is you have trigger change or you could trigger input. I'm going to trigger input, but try them both out, they should work.

And so, I wanna trigger it and then I could go ahead and say that we did the checkbox. Go ahead, get that rating filter, and then it should, Have a value. Of, in this case seven. Because input fields even if there are a number field, you always get it as a string.

So, yeah, cool, and so all of those work, you can actually see it for a second. If I do the only there for this one test, You can see that it did move it to seven, only the Butterbeer remains. The Butterbeer Frappuccino, right? So it's verifying that not only is it working because it's not failing, but it is actually doing the thing that I needed to do as well.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now