Mouse Actions in Robot Framework

The Robot Framework Selenium library provides various keywords to perform mouse actions. Mouse actions are important in automation as most of the time we need to simulate the mouse actions in our automation scripts. 

In this blog, we will discuss- 

  • Project setup for Mouse Actions 
  • How we can perform the several types of mouse actions on a website 

Project setup for Mouse 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 

Project setup

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

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

File name

Commands available in Robot Framework to perform mouse actions are as follows : 

  • Mouse Down: Using this keyword we can simulate pressing the left mouse button on an element. 
  • Mouse Up: Using this keyword we can simulate releasing the left mouse button on the element. 
  • Mouse Down On Link: Using this keyword we can simulate the mouse down event on the link. 
  • Mouse Over: Using this keyword we can simulate hovering the mouse over the element. 
  • Mouse Out: Using this keyword we can simulate moving away from the element. 
  • Mouse Down On Image: Using this keyword we can simulate a mouse down event on the image. 
  • Drag And Drop: Using this keyword we can drag an element from its position and drop it into the target element. 
  • Open Context menu: Using this keyword we can perform the right-click operations. This keyword will open the context menu on the element. 

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

Testcase:

Step1: Visit https://the-internet.herokuapp.com/

Step2: Select the drag and drop option from the list by performing the mouse down and mouse up action 

Step3: Perform drag and drop action  

Step4: Go back to the main menu 

Step5: Select the hovers option from the list 

Step6: Perform mouse over and mouse out the action 

Step7: Go back to the main menu 

Step8: Select the context menu option from the list 

Step9: Perform open context menu action 

Step10: Close the browser 

Code:  

*** Settings ***
Library     SeleniumLibrary
 
*** Variables ***
${browser}  chrome 
${url}  https://the-internet.herokuapp.com
*** Test Cases ***
Verify Mouse Actions
    open browser    ${url}  ${browser}
    mouse down    xpath://a[normalize-space()='Drag and Drop']
    mouse up   xpath://a[normalize-space()='Drag and Drop']
    drag and drop    xpath://div[@id='column-a']    xpath://div[@id='column-b']
    go back
    click element    xpath://a[normalize-space()='Hovers']
    mouse over    xpath://div[@class='example']//div[1]//img[1]
    mouse out    xpath://div[@class='example']//div[1]//img[1]
    go back
    click element    xpath://a[normalize-space()='Context Menu']
    open context menu    xpath://div[@id='hot-spot']
    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 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. 

Mouse action code

Code explanation:  

In this Testcase, we visited the https://the-internet.herokuapp.com/ URL in the Chrome browser, after which we redirected to a web page having a list of options from which we first selected the drag and drop option using mouse down command to simulate pressing the left mouse button on the element and then mouse up command to simulate releasing the left mouse button on the element. 

Later we were redirected to the drag and drop page where we dragged an element and dropped it into the target element using drag and drop command, we then returned to the main menu and selected the Hovers option to be redirected to the Hovers page where we simulated hovering the mouse over the element by using mouse over command and simulated moving away from the element using mouse out command. 

Then we returned to the main menu and selected the context menu option to be redirected to the context menu page where we performed the right click operation to open the context menu on the element by using open context menu command. 

Finally, we ended the test and closed the browser using the close browser keyword. 

Execution: 

To execute the testcase we need to use the command below: 

Execution command

After execution we should get result as pass.

Pass result

Reports: 

After every execution, robot framework creates detailed HTML reports. 

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

Log.html: 

Mouse actions log

Report.html: 

Moues actions reports

Read our next blog on “How to create User Defined Keywords in Robot Framework”.