Home  >  Article  >  Web Front-end  >  Test canvas drawing with protractor (1)

Test canvas drawing with protractor (1)

黄舟
黄舟Original
2017-02-25 13:10:591503browse

Protractor is an e2e testing framework built by the Angular gang, which essentially uses webdriver.

Testing canvas is mainly about comparing images. After searching, I couldn't find a good nodejs library. I still used the resemblejs I used before.

resemblejs uses Image and Canvas, so it cannot be used directly in nodejs.

Someone on github made a node-resemble, but it used a node-canvas written in C. To compile this, you need to install the python and vc runtime libraries. It sounds very troublesome, no wonder Only 2 stars.

Wouldn’t it be ok to let resemble run directly in the browser?

This requires the use of the browser's executeAsyncScript method.

Let’s talk about executeAsyncScript first

This method is to put the js code to run in the browser environment, that is, run in the browser environment, and all dom things can be used. Image, Canvas, everything is a no-brainer.

And it is asynchronous. In fact, there is a synchronous executeScript, which has similar usage.

I won’t post the API, but just talk about the usage.

The executeAsyncScript method is a variable parameter method

The first parameter must be a method, which is the code to be executed in the browser.

For example:

function(){
    console.log("我是一个方法");
};


Then you can write any number of parameters later, and in the first parameter method, you can use the arguments array to obtain these parameters in sequence

For example:

function(){
console.log("我是一个方法,我有三个参数")
    var p1=arguments[0];
    var p2=arguments[1];
    var p3=arguments[2];
};

Because it is asynchronous, the execution result needs to be returned with a callback function. This callback function webdriver has already prepared, which is the arguments. The last parameter.

So, the complete appearance of the first parameter should be

function(){
    console.log("我是一个方法,我有三个参数")
    var p1=arguments[0];
    var p2=arguments[1];
    var p3=arguments[2];
    var callback=arguments[arguments.lenght-1];
    callback("返回");
};

The return value of the executeAsyncScript method is a promise

So the entire function call should be This is the result of the callback in the then method.

It should be the same if you don’t use protractor and only use webdriver.

Okay, I haven’t gotten to the point after writing for a long time, so I’ll just leave it like this and write it in the next article.

The above is the content of using protractor to test canvas drawing (1). For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


-->

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:What’s new in HTML5.1Next article:What’s new in HTML5.1