Skip to content

Commit

Permalink
Merge pull request #28 from NimaSoroush/freezeimage
Browse files Browse the repository at this point in the history
Freezeimage
  • Loading branch information
NimaSoroush authored Aug 3, 2017
2 parents 1c95854 + 3723e44 commit 2034a2c
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 3 deletions.
6 changes: 4 additions & 2 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
|----|----|-----|
|`goto`|`string`|Url|
|`wait`|`string`, `integer` or `func`|waiting time in millisecond `or` waiting for a selector `or` waiting until the function you supplied is evaluated as true|
|`evaluate`|`func`|Evaluates a expression in the browser context|
|`execute`|`func`|execute an expression in the browser context|
|`freezeImage`|`string`|Selector name of a <img> tag containing animated image to be freezed before taking screenshot|
|`capture`|`string`|`undefiend`, `document` or selector name|

#### Steps example
Expand All @@ -23,7 +24,8 @@
steps: [
{ name: 'goto', value: 'http://www.example.com' },
{ name: 'wait', value: 5000 },
{ name: 'evaluate', value: () => { alert("Hello! I am an alert box!!") } },
{ name: 'freezeImage', value: '#myImage' },
{ name: 'execute', value: () => { alert("Hello! I am an alert box!!") } },
{ name: 'capture', value: 'document' },
],
...
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## [0.0.15] - 2017-08-03
### Added
- Adding evaluate functionality to evaluate expression on browser
- Adding freeze image functionality to freeze animations

## [0.0.14] - 2017-08-01
### Added
- Assign free ports to run chrome instances
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "differencify",
"version": "0.0.14",
"version": "0.0.15",
"description": "Perceptual diffing tool",
"main": "dist/index.js",
"scripts": {
Expand Down
1 change: 1 addition & 0 deletions src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export default {
test: 'test',
wait: 'wait',
evaluate: 'evaluate',
freezeImage: 'freezeImge',
};
14 changes: 14 additions & 0 deletions src/chromyRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import fs from 'fs';
import check from 'check-types';
import logger from './logger';
import compareImage from './compareImage';
import freezeImage from './freezeImage';
import actions from './actions';
import { configTypes } from './defaultConfig';

Expand Down Expand Up @@ -101,6 +102,19 @@ const run = async (chromy, options, test) => {
return false;
}
break;
case actions.freezeImage:
try {
prefixedLogger.log(`Freezing image ${action.value} in browser`);
const result = await chromy.evaluate(freezeImage(action.value));
if (!result) {
prefixedLogger.log(`Tag with selector ${action.value} is not a valid image`);
return false;
}
} catch (error) {
prefixedLogger.error(error);
return false;
}
break;
default:
break;
}
Expand Down
18 changes: 18 additions & 0 deletions src/freezeImage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const freezeImage = (selector) => {
const image = document.querySelector(selector);
if (image.tagName !== 'IMG') {
return false;
}
const className = image.className;
const canvas = document.createElement('canvas');
canvas.className = className;
const context = canvas.getContext('2d');
image.parentNode.replaceChild(canvas, image);

canvas.width = image.width;
canvas.height = image.height;
context.drawImage(image, 0, 0);
return true;
};

export default freezeImage;
34 changes: 34 additions & 0 deletions src/freezeImage.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import freezeImage from './freezeImage';

const replaceChild = jest.fn();
document.querySelector = jest.fn(() => ({
tagName: 'IMG',
parentNode: {
replaceChild,
},
}));

const drawImage = jest.fn();
const getContext = jest.fn(() => ({
drawImage,
}));
document.createElement = jest.fn(() => ({
className: 'class',
getContext,
}));

describe('Freeze Image', () => {
beforeEach(() => {
document.querySelector.mockClear();
document.createElement.mockClear();
});

it('calls document with correct value', () => {
freezeImage('test');
expect(document.querySelector).toHaveBeenCalledWith('test');
expect(document.createElement).toHaveBeenCalledWith('canvas');
expect(getContext).toHaveBeenCalledWith('2d');
expect(drawImage).toHaveBeenCalledTimes(1);
expect(replaceChild).toHaveBeenCalledTimes(1);
});
});

0 comments on commit 2034a2c

Please sign in to comment.