Robot Framework

How to handle Keyboard Actions in Robot Framework 

Keyboard Actions in Robot Framework

Robot Framework provides the ‘Press Keys’ command to handle the keyboard actions. Using the Press Keys command, we can simulate pressing keys on the element. 

In this blog, we will discuss- 

  • Project setup for Keyboard Actions 
  • How to perform some of the keyboard actions using the ‘Press Keys’ command 

Project setup for Keyboard Actions-

Firstly, we must install all prerequisite libraries in our project (for more info about prerequisites you can visit- https://www.neovasolutions.com/2022/07/21/robot-framework-automation-using-selenium-and-python/

To ensure that the required libraries are installed, use pip list command, or manually navigate to: 

File > settings > project: robot automation> python interpreter 

Keyboard Action project setup

After verifying the prerequisites, create a file named ‘KeyboardActions’  

— Create the file under the ‘Testcases’ directory with .robot extension >Enter 

File Name

Given below is an example for a better understanding of how we can use keyboard actions in our test 

Testcase:

Step1: Visit https://www.saucedemo.com/

Step2: Enter Username 

Step3: Enter Password 

Step4: Click the Login button 

Step5: Close the browser 

Code:

*** Settings *** 
Library    SeleniumLibrary 
 
*** Variables *** 
${browser}  chrome 
${url}  https://www.saucedemo.com/
*** Test Cases *** 
VerifyLoginPage 
    open browser    ${url}  ${browser}
    maximize browser window
    ${"UserName"}  set variable    xpath://input[@id='user-name']
    input text  ${"UserName"}  problem_user
    press keys    xpath://input[@id='user-name']   TAB
    ${"Password"}  set variable    xpath://input[@id='password']
    input text  ${"Password"}  secret_saucee
    press keys    xpath://input[@id='password']  BACKSPACE
    press keys    xpath://input[@placeholder='Last Name']   ENTER
    close browser

***Settings*** – To work with robot framework keywords we need to import   Library SeleniumLibrary in the settings section because all the keywords are built in the selenium library. Similarly, we can import other libraries, resource files, and variable files as per our project requirement in this section. 

***Variables*** – This section contains the defined variables that are used commonly, here we declared ${url} and ${browser} variables for browser and URL so that we can reuse these variables. In the same way, we can create more variables according to the project requirement. 

Keyboard Actions code

Code explanation:

In this test case, we visited the https://www.saucedemo.com/ URL in the Chrome browser, after which we were redirected to a login page, where we entered the username by setting ${“UserName”} variable and provided ‘problem_user’  as the input value to it, then we simulated pressing the TAB key by using press keys command, we then entered the password by setting ${“Password”} variable and provided ‘secret_sauce’ as the input value to it.  

After entering the password, we simulated pressing the BACKSPACE key by using the press keys command, lastly, we simulated pressing the ENTER key using the press keys command to be redirected to the next page, and then Finally, we ended the test and closed the browser using the close browser keyword. 

Execution:

To execute the test case we need to use the command below: 

Execution command

After execution, we should get the result as a pass. 

Pass result

Reports:

After every execution, the robot framework creates detailed HTML reports. 

We get three files – log.html, output.xml, and report.html. 

Log.html: 

Keyboard actions log

Report.html: 

Keyboard actions report

Read our next blog on “How to handle Mouse Actions in Robot Framework”.