The Issue
During automation, there will be instances when you may need to perform an action that a simple click command won’t satisfy. Pressing Enter, hitting Tab, deleting text, or using some combination of keyboard shortcuts would all fall into this category.
The Answer
Selenium Webdriver provides a send_keys method which enables the use of keyboard keys or shortcuts while automating test cases. These will perform the specified action after running the command, allowing us to successfully execute a test.
1 2 3 |
@driver.find_element(:identifier, "identifier_value").send_keys :enter @driver.find_element(:identifier, "identifier_value").send_keys :tab @driver.find_element(:identifier, "identifier_value").send_keys :delete |
The Code
Below is an example exhibiting the usage of keyboard keys:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
require 'selenium-webdriver' Selenium::WebDriver::Chrome.driver_path="CHROME-DRIVER-PATH" @driver = Selenium::WebDriver.for :chrome # Opening Google Sign-In Page @driver.get 'https://accounts.google.com/signin' sleep 2 #Enter the username username = @driver.find_element(:name, "identifier") username.send_keys"YOUR_USERNAME", :enter sleep 5 #Enter the password password = @driver.find_element(:name, "password") password.send_keys"YOUR_PASSWORD", :enter #Validating if the Welcome text is present or not welcome_text = @driver.find_element(:xpath, "//h1[contains(text(),'Welcome')]") puts { welcome_text.include? "Welcome" } ? "Text is displayed" : "Text is not displayed" |
In the above example, “:enter” is used instead of clicking on the Next button. By using the “:enter” key, it goes to the next page where the password has to be entered. After the password is entered, “:enter” is again used to sign in to the Gmail account instead of clicking on the “Sign In” button, which does the same operation.
The Result
The above code can be saved as a ruby (.rb) file and yields the following result
- Opens the browser
- Navigates to “Google Sign In” page
- Enters the username and hits enter
- Enters the password and hits enter
- Signs into your Gmail account
- Close the browser
The Takeaway
By using Selenium to send our keyboard keys, we have yet another tool employ as we write our test automation scripts. You can get creative and substitute click commands with key sends, or use the key send to resolve a specific issue that your click commands could not. There are numerous other Selenium key commands beyond the essential ones we’ve described here, experiment and find which key commands work best for your tests!