2

Most behavior of Selenium WebDriver implements only Press Key or Scrolling Scroll Bar. But, how can we implement the ACTION of "press + scroll mouse wheel"?

More over, my goal is not just to zoom in/out, but also using the mouse wheel action. Although some questions have solved zooming in/out by using "CTRL+ADD", my question want to be solved by mouse wheel action.

Terence Xie
  • 185
  • 2
  • 16
  • possible duplicate of [Selenium webdriver zoom in/out page content](http://stackoverflow.com/questions/15024756/selenium-webdriver-zoom-in-out-page-content) – LittlePanda Apr 24 '15 at 06:39
  • Yes, this question solved the problem of zoom in/out. But I want to know how to implement zooming in/out by using mouse wheel action but not the "CTRL+ADD" action. – Terence Xie Apr 29 '15 at 07:11

2 Answers2

0

Use the mouseWheel method of the Robot class.

Example:

import java.awt.Robot;
import java.awt.event.InputEvent;

public class Main {
  public static void main(String[] args) throws Exception {
    Robot robot = new Robot();

    robot.mouseMove(200, 200);

    robot.mousePress(InputEvent.BUTTON1_MASK);
    robot.mouseRelease(InputEvent.BUTTON1_MASK);
    robot.mouseWheel(-100);
  }
}

From the documentation:

public void mouseWheel(int wheelAmt)

Rotates the scroll wheel on wheel-equipped mice.

Parameters: wheelAmt

  • number of "notches" to move the mouse wheel
  • Negative values indicate movement up/away from the user
  • Positive values indicate movement down/towards the user.
LittlePanda
  • 2,496
  • 1
  • 21
  • 33
  • We may need a solution via Selenium which is not depend on platform. Robot is undoubtedly a good test tool, but it can only test local host. – Terence Xie May 04 '15 at 01:33
0

A little bit late ...

I'm using some client side generated mousewheel event via the executeScript(...) function.

Example with Javascript/selenium-webdriver (The function is not really complete and it needs some loaded jQuery on the browser side):

driver.executeScript(function(domElement,count,shiftMod,ctrlMod) {

  //Generate Event
  var ev = $.Event('mousewheel',{ 
    buttons: 0,
    ctrlKey: ctrlMod || false,
    altKey: false,
    shiftKey: shiftMod || false, 
    deltaX: count,
    deltaY: count,
  });

  //Trigger the Event
  $(domElement).trigger(ev);         
  return true;
},webElement,count,shiftMod,ctrlMod);
powerpete
  • 2,663
  • 2
  • 23
  • 49