Meaghan Lewis walks through an example of using keyboard and mouse input in WebDriver tests. Keyboard input is usually associated with sending keys to a field and mouse input is usually associated with clicking on a field or button.
- [Instructor] In this video, I will focus on input from the mouse and keyboard. From the test application homepage, I'll scroll down and select the key and mouse press option. On this page, I see that there is a text field for name input and a button under that. Let's get started with automating a test for this page. First what I'll want to do is to hop over and open up the exercise file for this video. From the welcome to IntelliJ IDEA menu, I'll select open and then navigate to the exercise file 02_02 and I'll click on the start directory and open that.
Once the project opens, I'll want to build it by going to the top menu, selecting build, and then build project. Once that is done, I will focus now on the test class, this keyboard and mouse input test class. What this test does is first on line nine, it sets the location for the chrome driver. You'll need to make sure to set the path to chrome driver on your computer for this test and all other tests in this exercise file going forward. After that, a new instance of the chrome driver is created on line 11 and on line 13, the driver navigates to the test application keypress page.
After that line, I'll want to start adding in steps to automate this keypress page. To get the details I need for the test, let's go back to the test application. Once there, what I want to do is inspect the text field to find a web element to use in the test. So I will right click on this full name field, select inspect which opens up the inspector, and focus on this highlighted line. I'll go ahead and grab the id of name and copy that to my clipboard.
Then going back to my test, I'm going to automate this name field. First I will define a new WebElement called name and I will say driver.findElement by the id and go ahead and paste this id of name into there and add a semicolon at the end. I'll next want to write the step to send keys or type a name into the name field. Feel free to use any name. I'll go ahead and just use mine.
So I can type name.sendKeys and in quotes I will type Meaghan Lewis. Some elements we need to click into first or make the field active or selected. In this case, we'll want to use mouse input before sending keys to the field. So after finding the web element name on line 15, I'll add a line before sending the keys and I can simply type name.click to click into the field first to make it active and then send keys to that name field.
In addition to fields, the mouse input is most commonly used to click buttons. Now I'll want to write a step to click the button on the test application. So going back to the test, in the top left corner I'll select this to inspect the button element and I see that it has an id of button so I can copy that to my clipboard and take that back to my test. I'll skip a line and then on line 19, I will define a new WebElement called button and I will tell the driver to find this element by the id of button.
And on line 20, I can say button.click in order to click the button. So now my test is complete. And to run it, I can simply right click on the class title and select the option to run keyboard and mouse input main method and there's also an associated keyboard shortcut that can be used to run test as well. So let's go ahead and run the test now and what's happening in the background is that chrome opens up, navigates to the form of your project keypress page, once there it fills out the name field, clicks on the button, and then closes.
Back in the output window of IntelliJ, I can see that the process finished with an exit code of zero indicating success.
- Using browser drivers and WebDriver
- Inspecting elements
- Selecting a locator
- Combining multiple CSS attributes
- Using common components
- Implicit and explicit waits
- Automating workflows
- Testing behaviors
- Using page objects
- Connecting to GitHub
- Running on the grid
- Using cloud-based testing tools