- Perform Mouse Hover with the help of Actions class – Example 1
- Example 2 of mouse hover
- Keypress events (Control+a)
- Keypress events (Control+c)
- TAB key
- Keypress events (Control+v)
- Source code (mouse hover)
- Source code (keypress)
Perform Mouse Hover with the help of Actions class – Example 1
Launch https://www.spicejet.com/
When we mouse hover ‘Add-ons’ menu, a child sub-menu automatically comes up
Let us automate a scenario where the user mouse-hovers ‘Add-ons’ and clicks ‘SpiceMax’ sub-menu. In Selenium, this mouse hover operation can be achieved by using ‘Actions’ class.
Let us inspect ‘Add-ons’
Also let us inspect ‘SpiceMax’ child sub-menu
We can now write our code.
In line#16, the driver object is passed in constructor of Actions class. The driver reference ‘act’ now gets all the capabilities of Actions’s class methods, example, moveToElement() method
In line#17, the method ‘moveToElement’ uses the ‘act’ reference’. This helps in moving to ‘Add-ons’ parent menu.
Also, in line#17, we have to append the methods .build().perform() at the end, otherwise mouse hover will not happen.
Execute the script. The script was able to successfully mouse hover and click the sub-menu ‘SpiceMax’ item as shown below
Example 2 of mouse hover
Let us see another example. Launch https://www.yahoo.com/
When we mouse-hover ‘More’ parent item, a lot of child sub-menu items get displayed
Let us inspect ‘More’. It is represented by ‘span’ tag having text ‘more’
Let us inspect ‘Parenting’ under More > Life
We can use ‘linkText’ locator
So our script would be:
Save and run the script.
Notice below that the parenting page opens
Keypress events (Control+a)
Launch https://text-compare.com/
There are 2 textbox areas on this page, one on the left hand side and another on right hand side
Use case that we are going to automate:
The script will write some text in the left area, will select the entire text using “ctrl+a”, will copy the entire text using ctrl+c, will shift to the right area using ‘tab’ key and finally paste the text in the right area using “ctrl+v”. This way, we will have same text both the text areas. So let us see how to automate this scenario.
Inspect left had side text area
Inspect right had side text area
Let us write some text on the left had side text area using the sendkeys() method, see line#23 shown below
After writing the text, the script will perform ‘select-all’ operation using ctrl+a (see lines 28-31 above).
Execute the script, notice below that the desired text gets typed in the left area
Keypress events (Control+c)
The below steps would perform the ‘copy’ operation
TAB key
The below steps would perform the ‘tab’ operation
Keypress events (Control+v)
The below steps would perform the ‘paste’ operation
Execute the script.
Notice that the same text gets copied in the right area
Similarly you can try to experiment with other keypress operations
Source code (mouse hover)
Source code (keypress)
Thank you for reading!