Instructor: Right now, we have a test that just visits our application. We'd really like to test some actual functionality. The first thing we're going to do is, with this existing test that just has a visit, we're going to run our application locally with npm run dev.
Then, in a second terminal pane, we're going to use npm run cypress to launch the Cypress UI. Then we can click on this form and put spec to run our test. When our test runs, it'll visit our application right in Chrome.
We'd like our application to focus this and put when the page loads. Currently, that's not the case, so let's write a test that'll test this behavior for us.
First thing we need to do is make sure that we're testing that the right element is focused. I need to know how to identify this input. Right now, I don't remember what the class name is.
I'm going to right click. I'm going to choose Inspect. Because we're running our application in Chrome, we can use Chrome's DevTools to get information about our application.
In this case, we want the class. We'll see that the class here is newTodo. I'm going to copy this, and then I'm going to go back into my test.
Let's start by updating our description. I want to say that it focuses the input on load. I'm going to visit the application.
Then after that, what I'm going to do is I'm going to issue another command using that global cy object. I'm going to say cy.focused.
Focus is going to find the focused element on the page. Then I'm going to chain an insertion onto that using should. In our should, we're going to say that it should have the class. We're going to use that value that we just copied, newTodo.
This test is going to verify that we have a focused element and that it's the element that we expect it to be. We haven't focused a different element instead. I want to save this.
I'm going to switch back to the test. We'll see that it's already started running. It picked up the change in the my test, and then it ran. We have a failing test.
You might notice that our test took a long time. I'm at 4.9 seconds.
What happened is Cypress tried to find a focused element on the page. When it didn't find it, it continued to retry that for four seconds, giving you the benefit of the doubt that eventually your application would reach that desired state.
In a lot of cases where something's async, this going to help us out where we don't necessarily have to worry about the timing specifically as long as our app is fast enough for things to happen within that four-second window. With our test failing, let's go and update our application code to get this test to pass.
We're going to come down here into Source Components and open up this TodoForm.js file. We're going to add autofocus as an attribute. We'll save this.
Now we can switch back to our test. Cypress isn't going to pick up changes in our application code. We'll have to go in here. We'll run the test again.
We'll see that it visits our application. It asserts that our input is focused. This time it's passing.
We can see the cursor's blinking in here. Everything's working as expected. Our test is verifying that behavior.