Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configuration for snapshots - allowing to disable them #25

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 44 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,53 +15,77 @@ Add environment variable with your eyes api key (key here is only example, get y
export EYES_API_KEY=6QGH9IA5nkK1wRt60I1EWybFMWTJ2R1kcwu07y41lYh0LNWu3r
```

#### Default usage
In your protractor tests:
```js
const eyes = require('eyes.it');

// or import in TS or JS ES6:
import eyes from 'eyes.it';

eyes.it('should run tests with eyes', () => {
browser.get('/');
$('input').sendKeys('123');
$('button').click();
expect($('span').text()).toBe('123');
eyes.it('should run tests with eyes', async () => {
await browser.get('/');
await $('input').sendKeys('123');
await $('button').click();
expect(await $('span').text()).toBe('123');
});
```

You can set a default window size
This will take a 2 snapshots by default:
- Immediately After the `browser.get()`
- At the end of the test.

#### Configure (disable) default snapshots
You can disable both default snapshots, and take a snapshot manually using `eyes.checkWindow()`
```js
const eyes = require('eyes.it');

eyes.it('should run tests with eyes', async () => {
await browser.get('/');

await $('input').sendKeys('123');
await $('button').click();
expect(await $('span').text()).toBe('123');
await eyes.checkWindow('should be 123');

await $('input').sendKeys('456');
await $('button').click();
expect(await $('span').text()).toBe('456');
await eyes.checkWindow('should be 456');
}, {browserGetSnapshotEnabled: false, afterSnapshotEnabled: false});
```

#### Set default window size

```js
const eyes = require('eyes.it');

eyes.defaultWindowSize = {width: 1024, height: 768};

eyes.it('should run tests with eyes', () => {
browser.get('/');
$('input').sendKeys('123');
$('button').click();
expect($('span').text()).toBe('123');
eyes.it('should run tests with eyes', async () => {
await browser.get('/');
await $('input').sendKeys('123');
await $('button').click();
expect(await $('span').text()).toBe('123');
});
```

Or set window size for a single spec
#### Set window size for a single spec

```js
const eyes = require('eyes.it');

eyes.it('should run tests with eyes', () => {
browser.get('/');
$('input').sendKeys('123');
$('button').click();
expect($('span').text()).toBe('123');
eyes.it('should run tests with eyes', async () => {
await browser.get('/');
await $('input').sendKeys('123');
await $('button').click();
expect(await $('span').text()).toBe('123');
}, {width: 1024, height: 768});
```

In case you require more screenshots in addition to the default ones that happen after browser.get() and at the end of the test, you can always call `eyes.checkWindow(testName);` in your test on your own.

#### Debugging (Focused tests)
You can also use `eyes.fit` in case you need to use focused tests.

#### Running locally
If you do not have `EYES_API_KEY` environment variable, `eyes.it` will behave just like regular `it`.

You can simulate an Applitools Github integration for pull requests (see [here](https://applitools.com/docs/topics/integrations/github-integration.html])), by adding an `APPLITOOLS_BATCH_ID` environment variable. `APPLITOOLS_BATCH_ID` should be the commit hash of the branch HEAD. This will be set as the batch id of tests.
Expand Down
66 changes: 42 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,7 @@ var Eyes = require('eyes.selenium').Eyes;
var appName = require(path.join(process.cwd(), 'package.json')).name;
var eyes = new Eyes();

var eyesOpen = false;
var hooked = browser.get;
browser.get = function (address) {
return hooked.apply(this, arguments).then(function (result) {
const res = eyesOpen ? eyes.checkWindow('get ' + address) : Promise.resolve();
return res.then(() => result);
});
};
var originalBrowserGet = browser.get;

function handleError(err, done) {
fail(err);
Expand Down Expand Up @@ -45,12 +38,13 @@ function eyesWith(fn) {
return function () {
var windowSize = eyes.defaultWindowSize;
var specVersion;
var enableSnapshotAtEnd;
var enableSnapshotAtBrowserGet;

if (isPassedParameterArgument(arguments)) {
var params = arguments[2];
var width = params.width;
var height = params.height;
var version = params.version;
enableSnapshotAtEnd = params.enableSnapshotAtEnd === undefined ? true : params.enableSnapshotAtEnd;
enableSnapshotAtBrowserGet = params.enableSnapshotAtBrowserGet === undefined ? true : params.enableSnapshotAtBrowserGet;

// width or height of 0 will make the params window size to be ignored
if (params.width && params.height) {
Expand All @@ -64,24 +58,45 @@ function eyesWith(fn) {
delete arguments[2];
}

var eyesOpen = false;

if (enableSnapshotAtBrowserGet) {
browser.get = function (address) {
return originalBrowserGet.apply(this, arguments).then(function (result) {
const res = eyesOpen ? eyes.checkWindow('get ' + address) : Promise.resolve();
return res.then(() => result);
});
};
} else {
browser.get = originalBrowserGet;
}

var spec = fn.apply(this, arguments);
var hooked = spec.beforeAndAfterFns;
spec.beforeAndAfterFns = function () {
var result = hooked.apply(this, arguments);
result.befores.unshift({fn: function (done) {
eyesOpen = true;
eyes.open(browser, appName, buildSpecName(spec, specVersion), windowSize).then(done);
}, timeout: () => 30000});
result.afters.unshift({fn: function(done) {
eyesOpen = false;
eyes
.checkWindow('end')
.then(() => eyes.close())
.then(done)
.catch(err => handleError(err, done));
},
timeout: () => 30000
result.befores.unshift({
fn: function (done) {
eyesOpen = true;
eyes.open(browser, appName, buildSpecName(spec, specVersion), windowSize).then(done);
}, timeout: () => 30000
});
result.afters.unshift(
{
fn: function (done) {
eyesOpen = false;
Promise.resolve()
.then(() => {
if (enableSnapshotAtEnd) {
return eyes.checkWindow('end');
}
})
.then(() => eyes.close())
.then(done)
.catch(err => handleError(err, done));
},
timeout: () => 30000
});
return result;
};
return spec;
Expand Down Expand Up @@ -118,6 +133,9 @@ function _init() {
eyes.it = eyesWithout(it);
eyes.fit = eyesWithout(fit);
eyes.checkWindow = () => Promise.resolve();
eyes.checkRegionBy = () => Promise.resolve();
eyes.checkRegionByElement = () => Promise.resolve();
eyes.checkRegion = () => Promise.resolve();
}

eyes.defaultWindowSize = null;
Expand Down