How to perform Mouse Wheel scrolling over HTML5 Canvas in Selenium?












6















I am working on a GWT Application (similar to Paint). In this, I have an HTML5 Canvas in which there is a functionality that scrolling a mousewheel up and down will zoom in and out of the canvas.



I have searched a lot but didn't find a workaround to fix this issue. Here's what did:



int PosX = 0;
int PosY = 10;
JavascriptExecutor executor = (JavascriptExecutor) getDriver();
String script = "document.getElementById('frontCanvas').scrollBy("
+ PosX + "," + PosY + ")";
executor.executeScript(script);

WebDriverWait wait = new WebDriverWait(getDriver(), 20);
wait.until(ExpectedConditions.javaScriptThrowsNoExceptions(script));


Now, this above code is working for another Angular application in which I am scrolling up and down a div element (which has a scrollbar) but it is not working on my canvas (which doesn't have a scrollbar) in the GWT application.



I am using Selenium 3.14.0 and run this code on the Chrome browser.
Can anyone suggest what can be done to fix this issue?










share|improve this question

























  • Why are you calling scrollBy event on html element instead of calling it on window directly? It will only work if parent of canvas element has fixed height and width.

    – Dipen Shah
    Dec 3 '18 at 19:44













  • @DipenShah calling window directly isn't working in my case

    – DG4
    Dec 5 '18 at 4:33











  • Then I would suggest to go from parent to parent and find the parent whose width and height is smaller than canvas element it self. I guess that is the only way, to make it work.

    – Dipen Shah
    Dec 5 '18 at 4:38











  • @DipenShah, I'm not sure if this code can be used to achieve the desired objective.

    – DG4
    Dec 5 '18 at 12:20











  • Was just for demonstration of what I said earlier, but NVM.

    – Dipen Shah
    Dec 5 '18 at 12:59
















6















I am working on a GWT Application (similar to Paint). In this, I have an HTML5 Canvas in which there is a functionality that scrolling a mousewheel up and down will zoom in and out of the canvas.



I have searched a lot but didn't find a workaround to fix this issue. Here's what did:



int PosX = 0;
int PosY = 10;
JavascriptExecutor executor = (JavascriptExecutor) getDriver();
String script = "document.getElementById('frontCanvas').scrollBy("
+ PosX + "," + PosY + ")";
executor.executeScript(script);

WebDriverWait wait = new WebDriverWait(getDriver(), 20);
wait.until(ExpectedConditions.javaScriptThrowsNoExceptions(script));


Now, this above code is working for another Angular application in which I am scrolling up and down a div element (which has a scrollbar) but it is not working on my canvas (which doesn't have a scrollbar) in the GWT application.



I am using Selenium 3.14.0 and run this code on the Chrome browser.
Can anyone suggest what can be done to fix this issue?










share|improve this question

























  • Why are you calling scrollBy event on html element instead of calling it on window directly? It will only work if parent of canvas element has fixed height and width.

    – Dipen Shah
    Dec 3 '18 at 19:44













  • @DipenShah calling window directly isn't working in my case

    – DG4
    Dec 5 '18 at 4:33











  • Then I would suggest to go from parent to parent and find the parent whose width and height is smaller than canvas element it self. I guess that is the only way, to make it work.

    – Dipen Shah
    Dec 5 '18 at 4:38











  • @DipenShah, I'm not sure if this code can be used to achieve the desired objective.

    – DG4
    Dec 5 '18 at 12:20











  • Was just for demonstration of what I said earlier, but NVM.

    – Dipen Shah
    Dec 5 '18 at 12:59














6












6








6


2






I am working on a GWT Application (similar to Paint). In this, I have an HTML5 Canvas in which there is a functionality that scrolling a mousewheel up and down will zoom in and out of the canvas.



I have searched a lot but didn't find a workaround to fix this issue. Here's what did:



int PosX = 0;
int PosY = 10;
JavascriptExecutor executor = (JavascriptExecutor) getDriver();
String script = "document.getElementById('frontCanvas').scrollBy("
+ PosX + "," + PosY + ")";
executor.executeScript(script);

WebDriverWait wait = new WebDriverWait(getDriver(), 20);
wait.until(ExpectedConditions.javaScriptThrowsNoExceptions(script));


Now, this above code is working for another Angular application in which I am scrolling up and down a div element (which has a scrollbar) but it is not working on my canvas (which doesn't have a scrollbar) in the GWT application.



I am using Selenium 3.14.0 and run this code on the Chrome browser.
Can anyone suggest what can be done to fix this issue?










share|improve this question
















I am working on a GWT Application (similar to Paint). In this, I have an HTML5 Canvas in which there is a functionality that scrolling a mousewheel up and down will zoom in and out of the canvas.



I have searched a lot but didn't find a workaround to fix this issue. Here's what did:



int PosX = 0;
int PosY = 10;
JavascriptExecutor executor = (JavascriptExecutor) getDriver();
String script = "document.getElementById('frontCanvas').scrollBy("
+ PosX + "," + PosY + ")";
executor.executeScript(script);

WebDriverWait wait = new WebDriverWait(getDriver(), 20);
wait.until(ExpectedConditions.javaScriptThrowsNoExceptions(script));


Now, this above code is working for another Angular application in which I am scrolling up and down a div element (which has a scrollbar) but it is not working on my canvas (which doesn't have a scrollbar) in the GWT application.



I am using Selenium 3.14.0 and run this code on the Chrome browser.
Can anyone suggest what can be done to fix this issue?







java selenium selenium-webdriver html5-canvas mousewheel






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 11:26







DG4

















asked Nov 21 '18 at 10:25









DG4DG4

190318




190318













  • Why are you calling scrollBy event on html element instead of calling it on window directly? It will only work if parent of canvas element has fixed height and width.

    – Dipen Shah
    Dec 3 '18 at 19:44













  • @DipenShah calling window directly isn't working in my case

    – DG4
    Dec 5 '18 at 4:33











  • Then I would suggest to go from parent to parent and find the parent whose width and height is smaller than canvas element it self. I guess that is the only way, to make it work.

    – Dipen Shah
    Dec 5 '18 at 4:38











  • @DipenShah, I'm not sure if this code can be used to achieve the desired objective.

    – DG4
    Dec 5 '18 at 12:20











  • Was just for demonstration of what I said earlier, but NVM.

    – Dipen Shah
    Dec 5 '18 at 12:59



















  • Why are you calling scrollBy event on html element instead of calling it on window directly? It will only work if parent of canvas element has fixed height and width.

    – Dipen Shah
    Dec 3 '18 at 19:44













  • @DipenShah calling window directly isn't working in my case

    – DG4
    Dec 5 '18 at 4:33











  • Then I would suggest to go from parent to parent and find the parent whose width and height is smaller than canvas element it self. I guess that is the only way, to make it work.

    – Dipen Shah
    Dec 5 '18 at 4:38











  • @DipenShah, I'm not sure if this code can be used to achieve the desired objective.

    – DG4
    Dec 5 '18 at 12:20











  • Was just for demonstration of what I said earlier, but NVM.

    – Dipen Shah
    Dec 5 '18 at 12:59

















Why are you calling scrollBy event on html element instead of calling it on window directly? It will only work if parent of canvas element has fixed height and width.

– Dipen Shah
Dec 3 '18 at 19:44







Why are you calling scrollBy event on html element instead of calling it on window directly? It will only work if parent of canvas element has fixed height and width.

– Dipen Shah
Dec 3 '18 at 19:44















@DipenShah calling window directly isn't working in my case

– DG4
Dec 5 '18 at 4:33





@DipenShah calling window directly isn't working in my case

– DG4
Dec 5 '18 at 4:33













Then I would suggest to go from parent to parent and find the parent whose width and height is smaller than canvas element it self. I guess that is the only way, to make it work.

– Dipen Shah
Dec 5 '18 at 4:38





Then I would suggest to go from parent to parent and find the parent whose width and height is smaller than canvas element it self. I guess that is the only way, to make it work.

– Dipen Shah
Dec 5 '18 at 4:38













@DipenShah, I'm not sure if this code can be used to achieve the desired objective.

– DG4
Dec 5 '18 at 12:20





@DipenShah, I'm not sure if this code can be used to achieve the desired objective.

– DG4
Dec 5 '18 at 12:20













Was just for demonstration of what I said earlier, but NVM.

– Dipen Shah
Dec 5 '18 at 12:59





Was just for demonstration of what I said earlier, but NVM.

– Dipen Shah
Dec 5 '18 at 12:59












2 Answers
2






active

oldest

votes


















2





+25









HTML5 Canvas



The HTML element is used to draw graphics, on the fly, via JavaScript. The element is only a container for graphics. You must use JavaScript to actually draw the graphics. Canvas has several methods for drawing paths, boxes, circles, text, and adding images.



In general, to scroll the mousewheel up and down we could have opted for the Actions Class. But as per Automated Testing of HTML5 Canvas Applications with Selenium WebDriver it seems this API is not that reliable. In Firefox, every mouse down, mouse up, or mouse click happens at the center of the element. So the code above produces a mouse move event to the provided (x,y), then a mouse move event to the center of the Canvas, then a mouse down, mouse up, and click all at the center of the Canvas. That may be fine for a button, but is unworkable for a Canvas, where you want to be able to hover, click, etc. at a specific location. The situation is even worse in Safari, where it just produces an exception indicating that mouse move events aren't supported. Chrome, meanwhile, works fine.



Alternative



An work around would be to use the JavascriptExecutor Interface manually dispatching synthesized mouse events using JavaScript.



Taking out a leaf out from @FlorentB.'s epic answer, to scroll a mousewheel up and down, you can emit the mouseover, mousemove and wheel events to the top element with a script injection and you can use the following solution:





  • Code Block:



    package demo;

    import org.openqa.selenium.By;
    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebDriverException;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;

    public class Canvas {

    static WebDriver driver;
    public static void main(String args) {

    System.setProperty("webdriver.chrome.driver", "C:\Utility\BrowserDrivers\chromedriver.exe");
    ChromeOptions options = new ChromeOptions();
    options.addArguments("start-maximized");
    options.addArguments("disable-infobars");
    options.addArguments("--disable-extensions");
    driver = new ChromeDriver(options);
    driver.get("https://www.google.co.uk/maps");
    WebElement elm = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#scene > div.widget-scene > canvas")));
    // Mouse wheel UP or Zoom In
    wheel_element(elm, -500, 0, 0);
    System.out.println("Mouse wheel UP or Zoom In through Wheel achieved !!!");
    // Mouse wheel DOWN or Zoom Out
    wheel_element(elm, 120, 0, 0);
    System.out.println("Mouse wheel DOWN or Zoom Out through Wheel achieved !!!");
    System.out.println("Mouse Scroll through Wheel achieved !!!");
    }

    public static void wheel_element(WebElement element, int deltaY, int offsetX, int offsetY)
    {
    try{
    String script = "var element = arguments[0];"
    +"var deltaY = arguments[1];"
    +"var box = element.getBoundingClientRect();"
    +"var clientX = box.left + (arguments[2] || box.width / 2);"
    +"var clientY = box.top + (arguments[3] || box.height / 2);"
    +"var target = element.ownerDocument.elementFromPoint(clientX, clientY);"
    +"for (var e = target; e; e = e.parentElement) {"
    +"if (e === element) {"
    +"target.dispatchEvent(new MouseEvent('mouseover', {view: window, bubbles: true, cancelable: true, clientX: clientX, clientY: clientY}));"
    +"target.dispatchEvent(new MouseEvent('mousemove', {view: window, bubbles: true, cancelable: true, clientX: clientX, clientY: clientY}));"
    +"target.dispatchEvent(new WheelEvent('wheel', {view: window, bubbles: true, cancelable: true, clientX: clientX, clientY: clientY, deltaY: deltaY}));"
    +"return;"
    +"}"
    +"}";

    WebElement parent = (WebElement) ((JavascriptExecutor) driver).executeScript("return arguments[0].parentNode;", element);
    ((JavascriptExecutor) driver).executeScript(script, parent, deltaY, offsetX, offsetY);
    }catch(WebDriverException e)
    {
    System.out.println("Exception caught in Catch block");
    }
    }

    }



  • Console Output:



    Mouse wheel UP or Zoom In through Wheel achieved !!!
    Mouse wheel DOWN or Zoom Out through Wheel achieved !!!
    Mouse Scroll through Wheel achieved !!!







share|improve this answer


























  • I tried this method but it doesn't do anything in the canvas of the application in which I am working.

    – DG4
    Dec 5 '18 at 4:04





















1














This will scroll specific in the current page using JS



JavascriptExecutor executor = (JavascriptExecutor) getDriver();
executor.executeScript("window.scrollBy(" + start + "," + end + ")");


else you can Scroll until a WebElement is found example:



WebElement x;
JavascriptExecutor executor = (JavascriptExecutor) getDriver();
getJs().executeScript("arguments[0].scrollIntoView();", x);


Thanks,






share|improve this answer


























  • This method is not working in my case.

    – DG4
    Dec 5 '18 at 4:30











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53409984%2fhow-to-perform-mouse-wheel-scrolling-over-html5-canvas-in-selenium%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2





+25









HTML5 Canvas



The HTML element is used to draw graphics, on the fly, via JavaScript. The element is only a container for graphics. You must use JavaScript to actually draw the graphics. Canvas has several methods for drawing paths, boxes, circles, text, and adding images.



In general, to scroll the mousewheel up and down we could have opted for the Actions Class. But as per Automated Testing of HTML5 Canvas Applications with Selenium WebDriver it seems this API is not that reliable. In Firefox, every mouse down, mouse up, or mouse click happens at the center of the element. So the code above produces a mouse move event to the provided (x,y), then a mouse move event to the center of the Canvas, then a mouse down, mouse up, and click all at the center of the Canvas. That may be fine for a button, but is unworkable for a Canvas, where you want to be able to hover, click, etc. at a specific location. The situation is even worse in Safari, where it just produces an exception indicating that mouse move events aren't supported. Chrome, meanwhile, works fine.



Alternative



An work around would be to use the JavascriptExecutor Interface manually dispatching synthesized mouse events using JavaScript.



Taking out a leaf out from @FlorentB.'s epic answer, to scroll a mousewheel up and down, you can emit the mouseover, mousemove and wheel events to the top element with a script injection and you can use the following solution:





  • Code Block:



    package demo;

    import org.openqa.selenium.By;
    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebDriverException;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;

    public class Canvas {

    static WebDriver driver;
    public static void main(String args) {

    System.setProperty("webdriver.chrome.driver", "C:\Utility\BrowserDrivers\chromedriver.exe");
    ChromeOptions options = new ChromeOptions();
    options.addArguments("start-maximized");
    options.addArguments("disable-infobars");
    options.addArguments("--disable-extensions");
    driver = new ChromeDriver(options);
    driver.get("https://www.google.co.uk/maps");
    WebElement elm = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#scene > div.widget-scene > canvas")));
    // Mouse wheel UP or Zoom In
    wheel_element(elm, -500, 0, 0);
    System.out.println("Mouse wheel UP or Zoom In through Wheel achieved !!!");
    // Mouse wheel DOWN or Zoom Out
    wheel_element(elm, 120, 0, 0);
    System.out.println("Mouse wheel DOWN or Zoom Out through Wheel achieved !!!");
    System.out.println("Mouse Scroll through Wheel achieved !!!");
    }

    public static void wheel_element(WebElement element, int deltaY, int offsetX, int offsetY)
    {
    try{
    String script = "var element = arguments[0];"
    +"var deltaY = arguments[1];"
    +"var box = element.getBoundingClientRect();"
    +"var clientX = box.left + (arguments[2] || box.width / 2);"
    +"var clientY = box.top + (arguments[3] || box.height / 2);"
    +"var target = element.ownerDocument.elementFromPoint(clientX, clientY);"
    +"for (var e = target; e; e = e.parentElement) {"
    +"if (e === element) {"
    +"target.dispatchEvent(new MouseEvent('mouseover', {view: window, bubbles: true, cancelable: true, clientX: clientX, clientY: clientY}));"
    +"target.dispatchEvent(new MouseEvent('mousemove', {view: window, bubbles: true, cancelable: true, clientX: clientX, clientY: clientY}));"
    +"target.dispatchEvent(new WheelEvent('wheel', {view: window, bubbles: true, cancelable: true, clientX: clientX, clientY: clientY, deltaY: deltaY}));"
    +"return;"
    +"}"
    +"}";

    WebElement parent = (WebElement) ((JavascriptExecutor) driver).executeScript("return arguments[0].parentNode;", element);
    ((JavascriptExecutor) driver).executeScript(script, parent, deltaY, offsetX, offsetY);
    }catch(WebDriverException e)
    {
    System.out.println("Exception caught in Catch block");
    }
    }

    }



  • Console Output:



    Mouse wheel UP or Zoom In through Wheel achieved !!!
    Mouse wheel DOWN or Zoom Out through Wheel achieved !!!
    Mouse Scroll through Wheel achieved !!!







share|improve this answer


























  • I tried this method but it doesn't do anything in the canvas of the application in which I am working.

    – DG4
    Dec 5 '18 at 4:04


















2





+25









HTML5 Canvas



The HTML element is used to draw graphics, on the fly, via JavaScript. The element is only a container for graphics. You must use JavaScript to actually draw the graphics. Canvas has several methods for drawing paths, boxes, circles, text, and adding images.



In general, to scroll the mousewheel up and down we could have opted for the Actions Class. But as per Automated Testing of HTML5 Canvas Applications with Selenium WebDriver it seems this API is not that reliable. In Firefox, every mouse down, mouse up, or mouse click happens at the center of the element. So the code above produces a mouse move event to the provided (x,y), then a mouse move event to the center of the Canvas, then a mouse down, mouse up, and click all at the center of the Canvas. That may be fine for a button, but is unworkable for a Canvas, where you want to be able to hover, click, etc. at a specific location. The situation is even worse in Safari, where it just produces an exception indicating that mouse move events aren't supported. Chrome, meanwhile, works fine.



Alternative



An work around would be to use the JavascriptExecutor Interface manually dispatching synthesized mouse events using JavaScript.



Taking out a leaf out from @FlorentB.'s epic answer, to scroll a mousewheel up and down, you can emit the mouseover, mousemove and wheel events to the top element with a script injection and you can use the following solution:





  • Code Block:



    package demo;

    import org.openqa.selenium.By;
    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebDriverException;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;

    public class Canvas {

    static WebDriver driver;
    public static void main(String args) {

    System.setProperty("webdriver.chrome.driver", "C:\Utility\BrowserDrivers\chromedriver.exe");
    ChromeOptions options = new ChromeOptions();
    options.addArguments("start-maximized");
    options.addArguments("disable-infobars");
    options.addArguments("--disable-extensions");
    driver = new ChromeDriver(options);
    driver.get("https://www.google.co.uk/maps");
    WebElement elm = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#scene > div.widget-scene > canvas")));
    // Mouse wheel UP or Zoom In
    wheel_element(elm, -500, 0, 0);
    System.out.println("Mouse wheel UP or Zoom In through Wheel achieved !!!");
    // Mouse wheel DOWN or Zoom Out
    wheel_element(elm, 120, 0, 0);
    System.out.println("Mouse wheel DOWN or Zoom Out through Wheel achieved !!!");
    System.out.println("Mouse Scroll through Wheel achieved !!!");
    }

    public static void wheel_element(WebElement element, int deltaY, int offsetX, int offsetY)
    {
    try{
    String script = "var element = arguments[0];"
    +"var deltaY = arguments[1];"
    +"var box = element.getBoundingClientRect();"
    +"var clientX = box.left + (arguments[2] || box.width / 2);"
    +"var clientY = box.top + (arguments[3] || box.height / 2);"
    +"var target = element.ownerDocument.elementFromPoint(clientX, clientY);"
    +"for (var e = target; e; e = e.parentElement) {"
    +"if (e === element) {"
    +"target.dispatchEvent(new MouseEvent('mouseover', {view: window, bubbles: true, cancelable: true, clientX: clientX, clientY: clientY}));"
    +"target.dispatchEvent(new MouseEvent('mousemove', {view: window, bubbles: true, cancelable: true, clientX: clientX, clientY: clientY}));"
    +"target.dispatchEvent(new WheelEvent('wheel', {view: window, bubbles: true, cancelable: true, clientX: clientX, clientY: clientY, deltaY: deltaY}));"
    +"return;"
    +"}"
    +"}";

    WebElement parent = (WebElement) ((JavascriptExecutor) driver).executeScript("return arguments[0].parentNode;", element);
    ((JavascriptExecutor) driver).executeScript(script, parent, deltaY, offsetX, offsetY);
    }catch(WebDriverException e)
    {
    System.out.println("Exception caught in Catch block");
    }
    }

    }



  • Console Output:



    Mouse wheel UP or Zoom In through Wheel achieved !!!
    Mouse wheel DOWN or Zoom Out through Wheel achieved !!!
    Mouse Scroll through Wheel achieved !!!







share|improve this answer


























  • I tried this method but it doesn't do anything in the canvas of the application in which I am working.

    – DG4
    Dec 5 '18 at 4:04
















2





+25







2





+25



2




+25





HTML5 Canvas



The HTML element is used to draw graphics, on the fly, via JavaScript. The element is only a container for graphics. You must use JavaScript to actually draw the graphics. Canvas has several methods for drawing paths, boxes, circles, text, and adding images.



In general, to scroll the mousewheel up and down we could have opted for the Actions Class. But as per Automated Testing of HTML5 Canvas Applications with Selenium WebDriver it seems this API is not that reliable. In Firefox, every mouse down, mouse up, or mouse click happens at the center of the element. So the code above produces a mouse move event to the provided (x,y), then a mouse move event to the center of the Canvas, then a mouse down, mouse up, and click all at the center of the Canvas. That may be fine for a button, but is unworkable for a Canvas, where you want to be able to hover, click, etc. at a specific location. The situation is even worse in Safari, where it just produces an exception indicating that mouse move events aren't supported. Chrome, meanwhile, works fine.



Alternative



An work around would be to use the JavascriptExecutor Interface manually dispatching synthesized mouse events using JavaScript.



Taking out a leaf out from @FlorentB.'s epic answer, to scroll a mousewheel up and down, you can emit the mouseover, mousemove and wheel events to the top element with a script injection and you can use the following solution:





  • Code Block:



    package demo;

    import org.openqa.selenium.By;
    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebDriverException;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;

    public class Canvas {

    static WebDriver driver;
    public static void main(String args) {

    System.setProperty("webdriver.chrome.driver", "C:\Utility\BrowserDrivers\chromedriver.exe");
    ChromeOptions options = new ChromeOptions();
    options.addArguments("start-maximized");
    options.addArguments("disable-infobars");
    options.addArguments("--disable-extensions");
    driver = new ChromeDriver(options);
    driver.get("https://www.google.co.uk/maps");
    WebElement elm = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#scene > div.widget-scene > canvas")));
    // Mouse wheel UP or Zoom In
    wheel_element(elm, -500, 0, 0);
    System.out.println("Mouse wheel UP or Zoom In through Wheel achieved !!!");
    // Mouse wheel DOWN or Zoom Out
    wheel_element(elm, 120, 0, 0);
    System.out.println("Mouse wheel DOWN or Zoom Out through Wheel achieved !!!");
    System.out.println("Mouse Scroll through Wheel achieved !!!");
    }

    public static void wheel_element(WebElement element, int deltaY, int offsetX, int offsetY)
    {
    try{
    String script = "var element = arguments[0];"
    +"var deltaY = arguments[1];"
    +"var box = element.getBoundingClientRect();"
    +"var clientX = box.left + (arguments[2] || box.width / 2);"
    +"var clientY = box.top + (arguments[3] || box.height / 2);"
    +"var target = element.ownerDocument.elementFromPoint(clientX, clientY);"
    +"for (var e = target; e; e = e.parentElement) {"
    +"if (e === element) {"
    +"target.dispatchEvent(new MouseEvent('mouseover', {view: window, bubbles: true, cancelable: true, clientX: clientX, clientY: clientY}));"
    +"target.dispatchEvent(new MouseEvent('mousemove', {view: window, bubbles: true, cancelable: true, clientX: clientX, clientY: clientY}));"
    +"target.dispatchEvent(new WheelEvent('wheel', {view: window, bubbles: true, cancelable: true, clientX: clientX, clientY: clientY, deltaY: deltaY}));"
    +"return;"
    +"}"
    +"}";

    WebElement parent = (WebElement) ((JavascriptExecutor) driver).executeScript("return arguments[0].parentNode;", element);
    ((JavascriptExecutor) driver).executeScript(script, parent, deltaY, offsetX, offsetY);
    }catch(WebDriverException e)
    {
    System.out.println("Exception caught in Catch block");
    }
    }

    }



  • Console Output:



    Mouse wheel UP or Zoom In through Wheel achieved !!!
    Mouse wheel DOWN or Zoom Out through Wheel achieved !!!
    Mouse Scroll through Wheel achieved !!!







share|improve this answer















HTML5 Canvas



The HTML element is used to draw graphics, on the fly, via JavaScript. The element is only a container for graphics. You must use JavaScript to actually draw the graphics. Canvas has several methods for drawing paths, boxes, circles, text, and adding images.



In general, to scroll the mousewheel up and down we could have opted for the Actions Class. But as per Automated Testing of HTML5 Canvas Applications with Selenium WebDriver it seems this API is not that reliable. In Firefox, every mouse down, mouse up, or mouse click happens at the center of the element. So the code above produces a mouse move event to the provided (x,y), then a mouse move event to the center of the Canvas, then a mouse down, mouse up, and click all at the center of the Canvas. That may be fine for a button, but is unworkable for a Canvas, where you want to be able to hover, click, etc. at a specific location. The situation is even worse in Safari, where it just produces an exception indicating that mouse move events aren't supported. Chrome, meanwhile, works fine.



Alternative



An work around would be to use the JavascriptExecutor Interface manually dispatching synthesized mouse events using JavaScript.



Taking out a leaf out from @FlorentB.'s epic answer, to scroll a mousewheel up and down, you can emit the mouseover, mousemove and wheel events to the top element with a script injection and you can use the following solution:





  • Code Block:



    package demo;

    import org.openqa.selenium.By;
    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebDriverException;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;

    public class Canvas {

    static WebDriver driver;
    public static void main(String args) {

    System.setProperty("webdriver.chrome.driver", "C:\Utility\BrowserDrivers\chromedriver.exe");
    ChromeOptions options = new ChromeOptions();
    options.addArguments("start-maximized");
    options.addArguments("disable-infobars");
    options.addArguments("--disable-extensions");
    driver = new ChromeDriver(options);
    driver.get("https://www.google.co.uk/maps");
    WebElement elm = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#scene > div.widget-scene > canvas")));
    // Mouse wheel UP or Zoom In
    wheel_element(elm, -500, 0, 0);
    System.out.println("Mouse wheel UP or Zoom In through Wheel achieved !!!");
    // Mouse wheel DOWN or Zoom Out
    wheel_element(elm, 120, 0, 0);
    System.out.println("Mouse wheel DOWN or Zoom Out through Wheel achieved !!!");
    System.out.println("Mouse Scroll through Wheel achieved !!!");
    }

    public static void wheel_element(WebElement element, int deltaY, int offsetX, int offsetY)
    {
    try{
    String script = "var element = arguments[0];"
    +"var deltaY = arguments[1];"
    +"var box = element.getBoundingClientRect();"
    +"var clientX = box.left + (arguments[2] || box.width / 2);"
    +"var clientY = box.top + (arguments[3] || box.height / 2);"
    +"var target = element.ownerDocument.elementFromPoint(clientX, clientY);"
    +"for (var e = target; e; e = e.parentElement) {"
    +"if (e === element) {"
    +"target.dispatchEvent(new MouseEvent('mouseover', {view: window, bubbles: true, cancelable: true, clientX: clientX, clientY: clientY}));"
    +"target.dispatchEvent(new MouseEvent('mousemove', {view: window, bubbles: true, cancelable: true, clientX: clientX, clientY: clientY}));"
    +"target.dispatchEvent(new WheelEvent('wheel', {view: window, bubbles: true, cancelable: true, clientX: clientX, clientY: clientY, deltaY: deltaY}));"
    +"return;"
    +"}"
    +"}";

    WebElement parent = (WebElement) ((JavascriptExecutor) driver).executeScript("return arguments[0].parentNode;", element);
    ((JavascriptExecutor) driver).executeScript(script, parent, deltaY, offsetX, offsetY);
    }catch(WebDriverException e)
    {
    System.out.println("Exception caught in Catch block");
    }
    }

    }



  • Console Output:



    Mouse wheel UP or Zoom In through Wheel achieved !!!
    Mouse wheel DOWN or Zoom Out through Wheel achieved !!!
    Mouse Scroll through Wheel achieved !!!








share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 4 '18 at 9:34

























answered Dec 4 '18 at 8:37









DebanjanBDebanjanB

41.1k83878




41.1k83878













  • I tried this method but it doesn't do anything in the canvas of the application in which I am working.

    – DG4
    Dec 5 '18 at 4:04





















  • I tried this method but it doesn't do anything in the canvas of the application in which I am working.

    – DG4
    Dec 5 '18 at 4:04



















I tried this method but it doesn't do anything in the canvas of the application in which I am working.

– DG4
Dec 5 '18 at 4:04







I tried this method but it doesn't do anything in the canvas of the application in which I am working.

– DG4
Dec 5 '18 at 4:04















1














This will scroll specific in the current page using JS



JavascriptExecutor executor = (JavascriptExecutor) getDriver();
executor.executeScript("window.scrollBy(" + start + "," + end + ")");


else you can Scroll until a WebElement is found example:



WebElement x;
JavascriptExecutor executor = (JavascriptExecutor) getDriver();
getJs().executeScript("arguments[0].scrollIntoView();", x);


Thanks,






share|improve this answer


























  • This method is not working in my case.

    – DG4
    Dec 5 '18 at 4:30
















1














This will scroll specific in the current page using JS



JavascriptExecutor executor = (JavascriptExecutor) getDriver();
executor.executeScript("window.scrollBy(" + start + "," + end + ")");


else you can Scroll until a WebElement is found example:



WebElement x;
JavascriptExecutor executor = (JavascriptExecutor) getDriver();
getJs().executeScript("arguments[0].scrollIntoView();", x);


Thanks,






share|improve this answer


























  • This method is not working in my case.

    – DG4
    Dec 5 '18 at 4:30














1












1








1







This will scroll specific in the current page using JS



JavascriptExecutor executor = (JavascriptExecutor) getDriver();
executor.executeScript("window.scrollBy(" + start + "," + end + ")");


else you can Scroll until a WebElement is found example:



WebElement x;
JavascriptExecutor executor = (JavascriptExecutor) getDriver();
getJs().executeScript("arguments[0].scrollIntoView();", x);


Thanks,






share|improve this answer















This will scroll specific in the current page using JS



JavascriptExecutor executor = (JavascriptExecutor) getDriver();
executor.executeScript("window.scrollBy(" + start + "," + end + ")");


else you can Scroll until a WebElement is found example:



WebElement x;
JavascriptExecutor executor = (JavascriptExecutor) getDriver();
getJs().executeScript("arguments[0].scrollIntoView();", x);


Thanks,







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 29 '18 at 13:07

























answered Nov 29 '18 at 13:02









Neagu VNeagu V

271111




271111













  • This method is not working in my case.

    – DG4
    Dec 5 '18 at 4:30



















  • This method is not working in my case.

    – DG4
    Dec 5 '18 at 4:30

















This method is not working in my case.

– DG4
Dec 5 '18 at 4:30





This method is not working in my case.

– DG4
Dec 5 '18 at 4:30


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53409984%2fhow-to-perform-mouse-wheel-scrolling-over-html5-canvas-in-selenium%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

RAC Tourist Trophy