Skip to content

Commit b9d4e22

Browse files
authored
Merge pull request #18 from NimaSoroush/wait
Adding wait functionality
2 parents f1bddd4 + 1f808cf commit b9d4e22

File tree

7 files changed

+149
-17
lines changed

7 files changed

+149
-17
lines changed

API.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,18 @@
55
|`update`|[TestOptions](https://github.com/NimaSoroush/differencify#testoptions)|Creates reference screenshots|
66
|`test`|[TestOptions](https://github.com/NimaSoroush/differencify#testoptions)|Validate your changes by testing against reference screenshots|
77
|`cleanup`|no argument|Closes all leftover browser instances|
8+
9+
### Steps Methods
10+
11+
|name|type|value|
12+
|----|----|-----|
13+
|`goto`|`string`|Url|
14+
|`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|
15+
|`capture`|`string`|`undefiend`, `document` or selector name|
16+
17+
##### Coming steps
18+
- click
19+
- setCookie
20+
- deleteCookie
21+
- clearCookies
22+
- emulate (Emulates phone)

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
## [0.0.12] - 2017-07-24
2+
### Added
3+
- Added wait functionality
4+
- Create prefix logger for tests (timestamped)
5+
- Refactor compare image to async function with better error handling
6+
- Some cleanups
7+
18
## [0.0.11] - 2017-07-21
29
### Added
310
- Added cleanup functionality

README.md

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Status: [![CircleCI](https://circleci.com/gh/NimaSoroush/differencify/tree/maste
1010

1111
## About
1212

13-
Differencify is library for visual regression testing by comparing your locall changes with reference screenshots of your website.
13+
Differencify is library for visual regression testing by comparing your local changes with reference screenshots of your website.
1414

1515
## Requirements
1616
- Node > 6
@@ -65,21 +65,9 @@ See [API.md](API.md) for full list of API calls
6565
|`resolution`|`object`|no|Browser width and height|800 * 600|
6666
|`steps`|`object`|yes|Steps before capturing screenshot|null|
6767

68-
### Steps
69-
70-
|name|type|value|
71-
|----|----|-----|
72-
|`goto`|`string`|Url|
73-
|`capture`|`string`|`undefiend`, `document` or selector name|
74-
75-
##### Coming steps
76-
- wait
77-
- click
78-
- setCookie
79-
- deleteCookie
80-
- clearCookies
81-
- emulate (Emulates phone)
68+
### Steps API
8269

70+
See [API.md](API.md) for full list of Steps API calls
8371

8472
### TestOptions example
8573

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "differencify",
3-
"version": "0.0.11",
3+
"version": "0.0.12",
44
"description": "Perceptual diffing tool",
55
"main": "dist/index.js",
66
"scripts": {
@@ -47,7 +47,8 @@
4747
},
4848
"dependencies": {
4949
"chalk": "^2.0.1",
50-
"chromy": "^0.3.2",
50+
"check-types": "^7.2.0",
51+
"chromy": "^0.3.6",
5152
"fs": "0.0.1-security",
5253
"jimp": "^0.2.28"
5354
},

src/actions.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ export default {
22
goto: 'goto',
33
capture: 'capture',
44
test: 'test',
5+
wait: 'wait',
56
};

src/chromyRunner.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import fs from 'fs';
2+
import check from 'check-types';
23
import logger from './logger';
34
import compareImage from './compareImage';
45
import actions from './actions';
@@ -68,6 +69,24 @@ const run = async (chromy, options, test) => {
6869
return false;
6970
}
7071
break;
72+
case actions.wait:
73+
try {
74+
if (check.number(action.value)) {
75+
prefixedLogger.log(`waiting for ${action.value} ms`);
76+
} else if (check.function(action.value)) {
77+
prefixedLogger.log('waiting for function execution');
78+
} else if (check.string(action.value)) {
79+
prefixedLogger.log(`waiting for ${action.value} selector`);
80+
} else {
81+
prefixedLogger.log('failed to detect waiting mechanism');
82+
return false;
83+
}
84+
await chromy.wait(action.value);
85+
} catch (error) {
86+
prefixedLogger.error(error);
87+
return false;
88+
}
89+
break;
7190
default:
7291
break;
7392
}

src/chromyRunner.test.js

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ jest.mock('chromy', () => jest.fn(() =>
1212
screenshotDocument: jest.fn(() => 'png file'),
1313
screenshotSelector: jest.fn(() => 'png file'),
1414
screenshot: jest.fn(() => 'png file'),
15+
wait: jest.fn(),
1516
}),
1617
));
1718

@@ -41,11 +42,17 @@ describe('ChromyRunner', () => {
4142
afterEach(() => {
4243
loggerCalls = [];
4344
writeFileSyncCalls = [];
45+
chromy.screenshotDocument.mockClear();
46+
chromy.screenshot.mockClear();
47+
chromy.screenshotSelector.mockClear();
48+
chromy.wait.mockClear();
4449
});
4550
it('run update', async () => {
4651
testConfig.type = configTypes.update;
4752
const result = await run(chromy, globalConfig, testConfig);
4853
expect(result).toEqual(true);
54+
expect(chromy.goto).toHaveBeenCalledWith('www.example.com');
55+
expect(chromy.screenshotDocument).toHaveBeenCalledTimes(1);
4956
expect(loggerCalls[0]).toEqual('goto -> www.example.com');
5057
expect(loggerCalls[1]).toEqual('capturing screenshot of whole DOM');
5158
expect(loggerCalls[2]).toEqual('screenshot saved in -> ./screenshots/default.png');
@@ -55,6 +62,8 @@ describe('ChromyRunner', () => {
5562
testConfig.type = configTypes.test;
5663
const result = await run(chromy, globalConfig, testConfig);
5764
expect(result).toEqual(true);
65+
expect(chromy.goto).toHaveBeenCalledWith('www.example.com');
66+
expect(chromy.screenshotDocument).toHaveBeenCalledTimes(1);
5867
expect(loggerCalls[0]).toEqual('goto -> www.example.com');
5968
expect(loggerCalls[1]).toEqual('capturing screenshot of whole DOM');
6069
expect(loggerCalls[2]).toEqual('screenshot saved in -> ./differencify_report/default.png');
@@ -67,6 +76,8 @@ describe('ChromyRunner', () => {
6776
const result = await run(chromy, globalConfig, testConfig);
6877
testConfig.steps.pop({ name: actions.test, value: globalConfig.testReportPath });
6978
expect(result).toEqual(true);
79+
expect(chromy.goto).toHaveBeenCalledWith('www.example.com');
80+
expect(chromy.screenshotDocument).toHaveBeenCalledTimes(1);
7081
expect(loggerCalls[0]).toEqual('goto -> www.example.com');
7182
expect(loggerCalls[1]).toEqual('capturing screenshot of whole DOM');
7283
expect(loggerCalls[2]).toEqual('screenshot saved in -> ./differencify_report/default.png');
@@ -76,6 +87,8 @@ describe('ChromyRunner', () => {
7687
testConfig.type = configTypes.update;
7788
const result = await run(chromy, globalConfig, testConfig);
7889
expect(result).toEqual(true);
90+
expect(chromy.goto).toHaveBeenCalledWith('www.example.com');
91+
expect(chromy.screenshotDocument).toHaveBeenCalledTimes(1);
7992
expect(loggerCalls[0]).toEqual('goto -> www.example.com');
8093
expect(loggerCalls[1]).toEqual('capturing screenshot of whole DOM');
8194
expect(loggerCalls[2]).toEqual('screenshot saved in -> ./screenshots/default.png');
@@ -98,6 +111,8 @@ describe('ChromyRunner', () => {
98111
newConfig.type = configTypes.test;
99112
const result = await run(chromy, globalConfig, newConfig);
100113
expect(result).toEqual(true);
114+
expect(chromy.goto).toHaveBeenCalledWith('www.example.com');
115+
expect(chromy.screenshot).toHaveBeenCalledTimes(1);
101116
expect(loggerCalls[0]).toEqual('goto -> www.example.com');
102117
expect(loggerCalls[1]).toEqual('capturing screenshot of chrome window');
103118
expect(loggerCalls[2]).toEqual('screenshot saved in -> ./differencify_report/default.png');
@@ -118,6 +133,8 @@ describe('ChromyRunner', () => {
118133
newConfig.type = configTypes.test;
119134
const result = await run(chromy, globalConfig, newConfig);
120135
expect(result).toEqual(true);
136+
expect(chromy.goto).toHaveBeenCalledWith('www.example.com');
137+
expect(chromy.screenshotDocument).toHaveBeenCalledTimes(1);
121138
expect(loggerCalls[0]).toEqual('goto -> www.example.com');
122139
expect(loggerCalls[1]).toEqual('capturing screenshot of whole DOM');
123140
expect(loggerCalls[2]).toEqual('screenshot saved in -> ./differencify_report/default.png');
@@ -138,10 +155,94 @@ describe('ChromyRunner', () => {
138155
newConfig.type = configTypes.test;
139156
const result = await run(chromy, globalConfig, newConfig);
140157
expect(result).toEqual(true);
158+
expect(chromy.goto).toHaveBeenCalledWith('www.example.com');
159+
expect(chromy.screenshotSelector).toHaveBeenCalledTimes(1);
141160
expect(loggerCalls[0]).toEqual('goto -> www.example.com');
142161
expect(loggerCalls[1]).toEqual('capturing screenshot of #form selector');
143162
expect(loggerCalls[2]).toEqual('screenshot saved in -> ./differencify_report/default.png');
144163
expect(writeFileSyncCalls).toEqual(['./differencify_report/default.png', 'png file']);
145164
});
146165
});
166+
describe('Chromy runner', () => {
167+
it('Wait: millisecond', async () => {
168+
const newConfig = {
169+
name: 'default',
170+
resolution: {
171+
width: 800,
172+
height: 600,
173+
},
174+
steps: [
175+
{ name: 'goto', value: 'www.example.com' },
176+
{ name: 'wait', value: 10 },
177+
],
178+
};
179+
newConfig.type = configTypes.test;
180+
const result = await run(chromy, globalConfig, newConfig);
181+
expect(result).toEqual(true);
182+
expect(chromy.goto).toHaveBeenCalledWith('www.example.com');
183+
expect(chromy.wait).toHaveBeenCalledWith(10);
184+
expect(loggerCalls[0]).toEqual('goto -> www.example.com');
185+
expect(loggerCalls[1]).toEqual('waiting for 10 ms');
186+
});
187+
it('Wait: selector', async () => {
188+
const newConfig = {
189+
name: 'default',
190+
resolution: {
191+
width: 800,
192+
height: 600,
193+
},
194+
steps: [
195+
{ name: 'goto', value: 'www.example.com' },
196+
{ name: 'wait', value: 'selector name' },
197+
],
198+
};
199+
newConfig.type = configTypes.test;
200+
const result = await run(chromy, globalConfig, newConfig);
201+
expect(result).toEqual(true);
202+
expect(chromy.goto).toHaveBeenCalledWith('www.example.com');
203+
expect(chromy.wait).toHaveBeenCalledWith('selector name');
204+
expect(loggerCalls[0]).toEqual('goto -> www.example.com');
205+
expect(loggerCalls[1]).toEqual('waiting for selector name selector');
206+
});
207+
it('Wait: function', async () => {
208+
const newConfig = {
209+
name: 'default',
210+
resolution: {
211+
width: 800,
212+
height: 600,
213+
},
214+
steps: [
215+
{ name: 'goto', value: 'www.example.com' },
216+
{ name: 'wait', value: () => {} },
217+
],
218+
};
219+
newConfig.type = configTypes.test;
220+
const result = await run(chromy, globalConfig, newConfig);
221+
expect(result).toEqual(true);
222+
expect(chromy.goto).toHaveBeenCalledWith('www.example.com');
223+
expect(chromy.wait).toHaveBeenCalledTimes(1);
224+
expect(loggerCalls[0]).toEqual('goto -> www.example.com');
225+
expect(loggerCalls[1]).toEqual('waiting for function execution');
226+
});
227+
it('Wait: not valid', async () => {
228+
const newConfig = {
229+
name: 'default',
230+
resolution: {
231+
width: 800,
232+
height: 600,
233+
},
234+
steps: [
235+
{ name: 'goto', value: 'www.example.com' },
236+
{ name: 'wait', value: true },
237+
],
238+
};
239+
newConfig.type = configTypes.test;
240+
const result = await run(chromy, globalConfig, newConfig);
241+
expect(result).toEqual(false);
242+
expect(chromy.goto).toHaveBeenCalledWith('www.example.com');
243+
expect(chromy.wait).toHaveBeenCalledTimes(0);
244+
expect(loggerCalls[0]).toEqual('goto -> www.example.com');
245+
expect(loggerCalls[1]).toEqual('failed to detect waiting mechanism');
246+
});
247+
});
147248
});

0 commit comments

Comments
 (0)