Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Find and Test Focused Input with Chrome’s DevTools in Cypress

    Andy Van SlaarsAndy Van Slaars

    In this lesson, we’ll add tests that finds a focused input. We’ll use Chrome’s dev tools from inside the Cypress runner to inspect the element and update our test to verify that the expected element is focused. We’ll see how Cypress can be used to test drive our application by creating a failing test and updating our application code to make it pass.



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson




    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.