Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interacting with a Canvas with Selenium

I've done a bit of reading on this, but haven't been able to find something specific. I have a canvas object that I need to interact with, with Selenium. However as we all know canvas objects are like a closed box. I read that you can add 'hooks' in the JS code that draws inside the canvas to allow Selenium to catch these hooks and work with certain things.

But I'm not sure how I could go about this. Does anyone have any experience or a small example they would be willing to share?

like image 504
checkdgt Avatar asked Aug 15 '13 14:08

checkdgt


People also ask

What is Canvas element selenium?

This element is used to build drawing and charting applications by using JavaScript. Canvas has several methods for drawing paths, boxes, circles, characters, and adding images. In this recipe, we will automate a simple drawing application through the Selenium WebDriver action class for mouse movements.

Can selenium interact with desktop applications?

The simple answer is no. Selenium testing is designed to automate web applications, not desktop applications. Automating desktop applications requires a different type of automation tool that's designed for desktop automation.

Can we automate graphs using selenium?

If you need to automate the generation of chart images or PDF, Selenium Webdriver might be the best option. It can handle different browsers even though this example only shows it using Firefox.


1 Answers

I was also looking into the same issue, after searching for some time I understood that Selenium accesses canvas element. but it can't access the inner elements/child elements of it. Because selenium used DOM model and canvas's child elements wont visible in DOM. After looking into one example mentioned here, we can interact with the canvas using coordinates. But that's absurd, most of the times we will draw elements dynamically, and how can we get perticular shape's coordinates. If you are having coordinates of the shapes you can play with them using above link.

like image 110
Shree Avatar answered Oct 14 '22 00:10

Shree