Skip to content

Commit 3792885

Browse files
authored
mockRequests method to wrap Mockeer functionality (#123)
1 parent 475f2ce commit 3792885

14 files changed

+2143
-1974
lines changed

API.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
|`launch`|`Object` [puppeteer.launch options](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#puppeteerlaunchoptions)|launches new browser and returns browser object|
1717
|`connect`|`Object` [puppeteer.connect options](https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#puppeteerconnectoptions)|Attaches to an existing browser instance and returns browser object|
1818
|`freezeImage`|`string`|Selector name of a `<img>` tag containing animated image to be freezed before taking screenshot|
19+
|`mockRequests`|`Object`|Runs chrome headless browser in isolation using [Mockeer](https://github.com/NimaSoroush/Mockeer)|
1920

2021
## Puppeteer methods
2122

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## [1.5.3] - 2018-11-11
2+
### Added
3+
- `mockRequests` method to wrap Mockeer functionality
4+
15
## [1.5.2] - 2018-11-11
26
### Updated
37
- Added example of using `.click(selector)` to `API.md`

README.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ Differencify matches [Puppeteer](https://github.com/GoogleChrome/puppeteer/blob/
4343
.init(TestOptions)
4444
.launch()
4545
.newPage()
46+
.mockRequests()
4647
.setViewport({ width: 1600, height: 1200 })
4748
.goto('https://github.com/NimaSoroush/differencify')
4849
.waitFor(1000)
@@ -59,6 +60,7 @@ Differencify matches [Puppeteer](https://github.com/GoogleChrome/puppeteer/blob/
5960
const target = differencify.init({ chain: false });
6061
await target.launch();
6162
const page = await target.newPage();
63+
await target.mockRequests();
6264
await page.setViewport({ width: 1600, height: 1200 });
6365
await page.goto('https://github.com/NimaSoroush/differencify');
6466
await page.waitFor(1000);
@@ -158,6 +160,44 @@ To Create/Update reference screenshots, simply set environment variable `update=
158160
> update=true node test.js
159161
```
160162

163+
## Mocking browser requests
164+
Differencify uses [Mockeer](https://github.com/NimaSoroush/Mockeer) to run chrome headless browser in isolation. This will help with more consistent and stable results when it comes dealing with a website that has inconsistent downstream dependencies. (e.g. unique API call returns different results based on request time). More details [here](https://github.com/NimaSoroush/Mockeer)
165+
166+
To use this feature call `mockRequests` during your tests.
167+
168+
```js
169+
(async () => {
170+
const result = await differencify
171+
.init(TestOptions)
172+
.launch()
173+
.newPage()
174+
.mockRequests()
175+
.goto('https://github.com/NimaSoroush/differencify')
176+
.screenshot()
177+
.toMatchSnapshot()
178+
.result((result) => {
179+
console.log(result);
180+
})
181+
.close()
182+
.end();
183+
184+
// or unchained
185+
186+
const target = differencify.init({ chain: false });
187+
await target.launch();
188+
const page = await target.newPage();
189+
await target.mockRequests();
190+
await page.goto('https://github.com/NimaSoroush/differencify');
191+
const image = await page.screenshot();
192+
const result = await target.toMatchSnapshot(image)
193+
await page.close();
194+
await target.close();
195+
196+
console.log(result);
197+
})();
198+
```
199+
More examples [here](src/integration.tests/integration.test.js)
200+
161201
## Debugging
162202
It is possible to debug your tests execution by passing `debug:true` as global config in Differencify class. See full list of configs [below](https://github.com/NimaSoroush/differencify#globaloptions)
163203

0 commit comments

Comments
 (0)