Monday, March 19, 2012

Webdriver - Mouse over events (Hover).

Webdriver - Mouse over events (Hover).

Read the following text from the webdriver team with respect to hovering problem.

When you attempt to hover over elements, and your physical mouse cursor is within the boundaries of the IE browser window, the hover will not work. More specifically, the hover will appear to work for a fraction of a second, and then the element will revert back to its previous state. The prevailing theory why this occurs is that IE is doing hit-testing of some sort during its event loop, which causes it to respond to the physical mouse position when the physical cursor is within the window bounds. The WebDriver development team has been unable to discover a workaround for this behavior of IE.

There are few objects and images on my web page where click() functionality is not working. I have taken lot of approaches, but the best solution found so far is below

     driver.findElement(By.xpath("//a[@id='Create']"));
    driver.findElement(By.xpath("//a[@id='Create']")).click();
The first statement will focus the element, next statement actually click the object.


I have another scenario where Image is displayed when I bring the mouse pointer on that row. To handle this situation I have used Actions class.

Below image shows display of icon when user bring the mouse pointer.


Webdriver code to click above pointed image.


Actions action = new Actions(driver);


action.moveToElement(driver.findElement(By.id("ctl00_CphePurchase_agvApprovalRequisitions$3$0$aReqNumber")));//id of requisition number
action.perform();


action.click(driver.findElement(By.id("ctl00_CphePurchase_agvApprovalRequisitions$3$0$imgBtnTrackStatus"))); //id of the image
action.perform();


Above code will not move the actual mouse pointer, but it will convey to IE that it has received a mouse input in that particular location.

For better understanding read the text from Jim (IE driver project lead).

Let me try to explain this. When simulating mouse movements with 
WebDriver, the actual mouse cursor does not move. Let me repeat that 
for emphasis. The actual physical mouse pointer on the screen *will* 
*not* *move*. If you are expecting that, you'll be disappointed. 

What WebDriver does on Windows is it sends the same messages to the IE 
window that it would receive from the input manager by the actual 
mouse. You won't see a change in the pointer on the screen, but if the 
element on the page responds to mouseover events, it should react as 
if you moved the mouse over it. 

The advantage here is that if something like an alert() method is 
called in the element's mouseOver event, WebDriver can still handle 
it. Additionally, it's a more accurate representation of the actual 
mouse movement, firing ancillary events like mouseEnter and so on. 
Contrast this approach with that of Selenium RC, which simply fires 
the mouseOver event directly via JavaScript. 

--Jim 

Still if you want to move the mouse pointer physically, you need to take different approach using Robot class


Point coordinates = driver.findElement(By.id("ctl00_portalmaster_txtUserName")).getLocation();
Robot robot = new Robot();
robot.mouseMove(coordinates.getX(),coordinates.getY()+120);

Webdriver provide document coordinates, where as Robot class is based on Screen coordinates, so I have added +120 to compensate the browser header.

Screen Coordinates: These are coordinates measured from the top left corner of the user's computer screen. You'd rarely get coordinates (0,0) because that is usually outside the browser window. About the only time you'd want these coordinates is if you want to position a newly created browser window at the point where the user clicked.
In all browsers these are in event.screenX and event.screenY.

Window Coordinates: These are coordinates measured from the top left corner of the browser's content area. If the window is scrolled, vertically or horizontally, this will be different from the top left corner of the document. This is rarely what you want.
In all browsers these are in event.clientX and event.clientY.

Document Coordinates: These are coordinates measured from the top left corner of the HTML Document. These are the coordinates that you most frequently want, since that is the coordinate system in which the document is defined.

For more details select this link.



---

5 comments:

  1. i have a problem using action builder after updating to 2.28 with Firefox 13.0

    this is the code i used
    WebElement change_language= driver.findElement(By.xpath("//*[@id='change_language']/p"));
    WebElement french= driver.findElement(By.xpath("//*[@id='change_language']/div/ul/li[2]/a"));
    Actions builder = new Actions(driver);
    builder.moveToElement(change_language).build().perform();
    french.click();

    any suggestions welcome

    ReplyDelete
  2. i have a problem while clicking sublink

    WebDriver driver=new FirefoxDriver();
    driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
    driver.get("http://qaplanet.in/web-app/hrm/login.php");
    driver.findElement(By.name("txtUserName")).sendKeys("admin");
    driver.findElement(By.name("txtPassword")).sendKeys("admin");
    driver.findElement(By.name("Submit")).click();

    after login to the page we need to clik AddEmployee under PIM:

    please reply with code

    ReplyDelete
  3. hi
    by using robot class i am able to hover and click on the subLink but after that webdriver is not able to find locators in the new page ... do i need to release mouseMove or anything likewise can you please help me in this issue.

    ReplyDelete
    Replies
    1. Find the different window handles available and switch to it.

      Delete
  4. Hi Bharath,

    I tried using the code approach of

    Point coordinates = driver.findElement(By.id("ctl00_portalmaster_txtUserName")).getLocation();
    Robot robot = new Robot();
    robot.mouseMove(coordinates.getX(),coordinates.getY()+120);

    and this did not work.

    The scenario is as follows.

    Go to www.hdfcbank.com Do Mouseover Find your nearest (Main Menu A, I am able to do this using actions driver concept) then Mouse over Submenu B which is ATM (this also I am able to do this) and then click on SubMenu C "All ATMs" (this is the one I am NOT able to to)

    Could you kindly give inputs to this scenario.

    Thanks,

    EJ

    ReplyDelete