Home > Web Front-end > H5 Tutorial > body text

Test canvas drawing with protractor (1)

黄舟
Release: 2017-02-25 13:10:59
Original
1514 people have browsed it

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("我是一个方法");
};
Copy after login


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(){
Copy after login
console.log("我是一个方法,我有三个参数")
    var p1=arguments[0];
    var p2=arguments[1];
    var p3=arguments[2];
};
Copy after login

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("返回");
};
Copy after login

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)!


-->

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!