Interacting with a Canvas with Selenium

16,645

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.

Share:
16,645
checkdgt
Author by

checkdgt

I'm a software QA engineer, working mostly with Selenium and Java now to validate against web based cloud applications.

Updated on June 15, 2022

Comments

  • checkdgt
    checkdgt almost 2 years

    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?

  • checkdgt
    checkdgt about 10 years
    Yeah the Canvas object proved a real pain. I ended up putting together a screen validation setup which works on stored images. You can validate against a specific portion of the screen (the canvas) This seems to work well enough in practice, as long as the app your testing isn't going through too many graphical changes