Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML Canvas Unit testing

How can I unit-test Javascript that draws on an HTML canvas? Drawing on the canvas should be checked.

like image 242
pcjuzer Avatar asked Dec 10 '10 08:12

pcjuzer


People also ask

Is HTML canvas still used?

The HTML5 canvas has the potential to become a staple of the web, enjoying ubiquitous browser and platform support in addition to widespread webpage support, as nearly 90% of websites have ported to HTML5.

What is HTMLCanvasElement?

The HTMLCanvasElement interface provides properties and methods for manipulating the layout and presentation of <canvas> elements. The HTMLCanvasElement interface also inherits the properties and methods of the HTMLElement interface.

How do you test canvas with Cypress?

A visual regression test For a proper visual test with cypress-plugin-snapshots we can take a snapshot of the page: describe("Some app", () => { it("should see a yellow canvas", () => { cy. visit("http://localhost:5000"); cy. get("canvas"); // Take a snapshot cy.

Which HTML element is used for canvas graphics?

The HTML <canvas> element is used to draw graphics, on the fly, via JavaScript. The <canvas> element is only a container for graphics.


1 Answers

I wrote an example for unit-testing canvas and other image-y types with Jasmine and js-imagediff.

Jasmine Canvas Unit Testing

I find this to be better than making sure specific methods on a mock Canvas have been invoked because different series of methods may produce the same method. Typically, I will create a canvas with the expected value or use a known-stable version of the code to test a development version against.

like image 100
cesutherland Avatar answered Oct 03 '22 07:10

cesutherland