Image assertion with node js
Recently I started with automation testing. I got a scenario where we need to assert the images. That became very tough situation for us and we just ignored that for now. Then I started few research like whether we can assert images with Node js. Yes!! we can do it with the help of looks-same
To get it started, you need to first install node js in your machine. You may need the npm package — looks-same.
Get the Visual studio code and open a new terminal inside and type below to initiate node package manager,
npm init -y
Now, lets install the image assertion npm package called looks-same by typing following in terminal,
npm i looks-same
That’s it! Now you can make use of this package inside your code.
var looksSame = require('looks-same');
Syntax:
looksSame('<image1 location>', 'image2 location', function(error, {equal}) {// equal will be true, if images looks the same});
Get the two images, which you want to compare and paste their location inside the function. You can use console.log() to print the assertion result using the variable ‘equal’ and save it as .js file,
var looksSame = require('looks-same');looksSame('image1.png', 'image2.png', function(error, {equal}) {// equal will be true, if images looks the sameconsole.log(equal)});
Now, you can run the javascript file with below command,
node <scriptName>
Yeah!!! you did it. Based upon your input, it may return true or false.