Sunday, May 20, 2012

Selenium WebDriver : Handling mouseover events

I was searching though the internet to find out proper way of handling mouseover events with Selenium WebDriver. Found few resources but nothing proven to work. However finally managed to get though it. For others convenient, I though of writing down the way we handle mouse over events in our automation tests.

In order to get the job done, you need to use org.openqa.selenium.interactions.Actions class. The Action class is user-facing API for emulating complex user gestures. WebDirver users can use this class to simulate usage of keyboard or mouse events. It Implements builder pattern. So calling build will build   composite action containing all actions specified by the method calls.

I have used following code snippets to delete a tag in the tag cloud. The delete button of a particular tag gets visible only for mouse hover event.

Actions builder = new Actions(driver);
WebElement tagElement = driver.findElement(By.id("tag-cloud"));
builder.moveToElement(tagElement).build().perform();

You need to create new action builder instance by passing WebDriver instance to the class. Then move to the WebElement and build it to generates a composite action containing all actions so far, ready to be performed.

This will be the first post of Selenium WebDriver tips. Some useful aspects and solutions for common UI automation requirements using WebDriver will be published as i came across them.


23 comments:

Unknown said...
This comment has been removed by a blog administrator.
Unknown said...

Thanks for this Krishantha, I am new to selenium...please keep it coming, very informative and useful!
ranjana

Unknown said...
This comment has been removed by a blog administrator.
Anonymous said...

Sir can you please tell us how to interact with mouseover elements in a template...it would be of great help if u would paste the code for interacting with mouseover elements..Thanks in advance

venu said...
This comment has been removed by a blog administrator.
venu said...

I started looking at selenium and your post proved to be a boon. Thanks a lot.

Unknown said...

Hi Krish,

When i tried using the code you have mentioned i am getting the below error. Do we need to add any jar files?

org.openqa.selenium.InvalidElementStateException: Cannot perform native interaction: Could not load native events component.
Command duration or timeout: 71 milliseconds
Build info: version: '2.21.0', revision: '16552', time: '2012-04-11 19:09:00'
System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_05'
Driver info: driver.version: RemoteWebDriver
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:175)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:128)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:459)
at org.openqa.selenium.remote.ExecuteMethod.execute(ExecuteMethod.java:47)
at org.openqa.selenium.remote.RemoteMouse.mouseMove(RemoteMouse.java:89)
at org.openqa.selenium.interactions.MoveMouseAction.perform(MoveMouseAction.java:37)
at org.openqa.selenium.interactions.CompositeAction.perform(CompositeAction.java:32)
at Admin_TC_001_01.TC_001(Admin_TC_001_01.java:41)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:80)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:702)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:894)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1219)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:87)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1192)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1117)
at org.testng.TestNG.run(TestNG.java:1025)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:109)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:202)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:173)
Caused by: org.openqa.selenium.remote.ErrorHandler$UnknownServerException: Cannot perform native interaction: Could not load native events component.
Build info: version: '2.21.0', revision: '16552', time: '2012-04-11 19:09:00'
System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_05'
Driver info: driver.version: unknown

Krishantha Samaraweera said...

@Thomas - Are you using latest version of firefox? if yes, I recommend to use older firefox version 9 or 10 as WebDriver 2.20.0 or 2.21.0 is not yet fully compatible with latest version of firefox.

Anonymous said...

I understand that works completely clicking on the sub menu which is hidden but what if i have to enter values in the hidden object, can you please help me

Unknown said...

Thanks a lot Krishantha, your code for Mouse mover events using Webdriver worked perfectly fine.

Thanks,
Krishnan

Unknown said...

Hi, How do i hover over a tab and then click on one of the links displayed after hovering?

Like..
hover-> My Vodafone (tab)
Prepaid (1st link)
Postpaid (2nd link)(click)
Datacard (3rd link)

Unknown said...

hi Krishantha's

Action builder code is working on WINDOWS machine but not on LINUX (UBUNTU) for Firefox

Any solution for the same..??

Unknown said...

OMG, its fantastic!!! my search end up here for mouse over in webdriver.

Thank you very much Krshanth!!!!

Regards,
Narayanarao.Thota

VG said...

Hi Krishantha, Can you tell me how to click on the drop down menus in C#? The code you provided is in Java. Thanks!

VG said...

Hi Krishanta, Can you tell me how to click on a menu item in a drop down list in C#? I tried the example that you provided but its throwing error. Thanks!

Krishantha Samaraweera said...

@VG - i'm not much familiar with C#. You may find this post useful - http://stackoverflow.com/questions/5471515/webdriver-c-sharp-select-a-value-from-drop-down

suman chandupatla said...

Hi Krishan,

Can you please help me out on how to handle with Dynamic Xpath`s

for example: i need to enter FAcebook Status using script, but the Xpath is dynamic and i am not able to do so

Thanks
Suman

Abdul Manan said...

My problem is that fancy box not open until I move mouse pointer physically on selected image during test run or set the mouse pointer on screen where selected image will display before test run

Scenario:
I have gallery page in which different images paste on screen, when i click an image, fancy box open with next and previous buttons and user can move next and back to view all images in fancy box

WebElement el = driver.findElement(By.xpath("//div[2]/p/a/span"));
Actions builder = new Actions(driver);
builder.moveToElement(el).click();
builder.perform();
driver.manage().timeouts().implicitlyWait(2000, TimeUnit.SECONDS);

// following code also not work to move mouse pointer physically

Point coordinates = driver.findElement(By.xpath("//div[2]/p/a/span")).getLocation();

Robot robot = new Robot();
robot.mouseMove(coordinates.getX(),coordinates.getY()+120);

Please guide me if you have a solution, Thakns, Abdul Manan
abdulmanan.butt@gmail.com

vasanthbhaskaran's said...

hi krishan
i need to read the input values from excel for drop down box i have tried java executor too .it says
"'""""Element is not currently visible and so may not be interacted with
Command duration or timeout: 41 milliseconds"""""

Slacker1stClass said...

Thank you very much, this solved my problem which stuck me for entire day !!
Please keep it up for the selenium tips

Unknown said...

Hi Krishantha,

It is very nice blog for mouse hover event.
I have only class name for the button, so which code i can use for mouse hover on button with class name?
Can you please help me which code i can use for click on the button after mouse hover?

Thnaks,
Akash Gupta.

Shasak singh said...

restart your system

Madala Venkat said...

thank you so munch brother