From the course: Selenium Essential Training

Keyboard and mouse input - Selenium Tutorial

From the course: Selenium Essential Training

Keyboard and mouse input

- [Meaghan] 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 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. Some learners have shared that when they first start IntelliJ, they see a banner at the top of the window with the message, "no JDK running". To fix that, look to the right-hand side of the banner. Select the Configure link and enter the path to where the JDK is downloaded on your computer. 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 key press page. After that line, I'll want to start adding in steps to automate this key press 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 web element called name, and I will say driver.findElement by the ID and go ahead and paste this ID, add 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 web element 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 tests, 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 Formy project key press 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.

Contents