Skip to content

Commit

Permalink
Implement t.ctx
Browse files Browse the repository at this point in the history
  • Loading branch information
inikulin committed Jan 3, 2017
1 parent 0303e8d commit f62d05e
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/api/globals/testing-unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ export default class TestingUnit {
throw new Error('Not implemented');
}

_only$FLAG () {
_only$getter () {
this.only = true;

return this.apiOrigin;
}

_skip$FLAG () {
_skip$getter () {
this.skip = true;

return this.apiOrigin;
Expand Down
5 changes: 5 additions & 0 deletions src/api/test-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export default class TestController {
this.testRun = testRun;
this.executionChain = Promise.resolve();
this.callsiteWithoutAwait = null;
this.ctxStorage = testRun.ctxStorage;
}

// NOTE: we track missing `awaits` by exposing a special custom Promise to user code.
Expand Down Expand Up @@ -114,6 +115,10 @@ export default class TestController {
// We need implementation methods to obtain correct callsites. If we use plain API
// methods in chained wrappers then we will have callsite for the wrapped method
// in this file instead of chained method callsite in user code.
_ctx$getter () {
return this.ctxStorage;
}

_click$ (selector, options) {
return this._enqueueAction('click', ClickCommand, { selector, options });
}
Expand Down
2 changes: 2 additions & 0 deletions src/test-run/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ export default class TestRun extends Session {
this.pendingRequest = null;
this.pendingPageError = null;

this.ctxStorage = {};

this.errs = [];

this.lastDriverStatusId = null;
Expand Down
12 changes: 6 additions & 6 deletions src/utils/delegated-api.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const API_IMPLEMENTATION_METHOD_RE = /^_(\S+)\$(FLAG)?$/;
const API_IMPLEMENTATION_METHOD_RE = /^_(\S+)\$(getter)?$/;

export function getDelegatedAPIList (src) {
return Object
Expand All @@ -8,9 +8,9 @@ export function getDelegatedAPIList (src) {

if (match) {
return {
srcProp: prop,
apiProp: match[1],
isFlag: match[2]
srcProp: prop,
apiProp: match[1],
isGetter: match[2]
};
}

Expand All @@ -20,7 +20,7 @@ export function getDelegatedAPIList (src) {
}

export function delegateAPI (src, dest, apiList, proxyMethod, useDynamicMethodCtx) {
apiList.forEach(({ srcProp, apiProp, isFlag }) => {
apiList.forEach(({ srcProp, apiProp, isGetter }) => {
var fn = function (...args) {
if (proxyMethod)
proxyMethod();
Expand All @@ -30,7 +30,7 @@ export function delegateAPI (src, dest, apiList, proxyMethod, useDynamicMethodCt
return ctx[srcProp](...args);
};

if (isFlag)
if (isGetter)
Object.defineProperty(dest, apiProp, { get: fn });

else
Expand Down
4 changes: 3 additions & 1 deletion test/functional/fixtures/.eslintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"rules": {
"no-unused-expressions": 0
"no-unused-expressions": 0,
"no-spaced-func": 0,
"no-unexpected-multiline": 0
},
"globals": {
"fixture": true,
Expand Down
23 changes: 23 additions & 0 deletions test/functional/fixtures/api/es-next/hooks/test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var expect = require('chai').expect;
var uniq = require('lodash').uniq;

// NOTE: we run tests in chrome only, because we mainly test server API functionality.
describe('[API] fixture.beforeEach/fixture.afterEach hooks', function () {
Expand Down Expand Up @@ -64,5 +65,27 @@ describe('[API] test.before/test.after hooks', function () {
expect(errs[0]).contains('[testBefore][test][testAfter]');
});
});
});

describe('[API] t.ctx', function () {
it('Should pass context object to tests and hooks', function () {
return runTests('./testcafe-fixtures/run-all.js', 't.ctx', { shouldFail: true, only: 'chrome,ie,firefox' })
.catch(function (errs) {
var browsers = [];

Object.keys(errs).forEach(function (browser) {
var ctxJson = errs[browser][0].match(/###(.+)###/)[1];
var ctx = JSON.parse(ctxJson);

// NOTE: check that we have same browser for each stage
expect(uniq(ctx.browsers).length).eql(1);
expect(ctx.steps).eql(['before', 'test', 'after']);

browsers.push(ctx.browsers[0]);
});

// NOTE: check that each context is from different browsers
expect(uniq(browsers).length).eql(3);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,39 @@ test('Test2', async t => {
await t.click('#test');
});

test('Test3', async t => {
await t.click('#test');
}).before(async t => {
await t.click('#testBefore');
}).after(async t => {
await t
.click('#testAfter')
.click('#failAndReport');
});
test
.before(async t => {
await t.click('#testBefore');
})
('Test3', async t => {
await t.click('#test');
})
.after(async t => {
await t
.click('#testAfter')
.click('#failAndReport');
});

test
.before(async t => {
t.ctx.val = {
browsers: [],
steps: []
};

t.ctx.val.browsers.push(await t.eval(()=>navigator.userAgent));
t.ctx.val.steps.push('before');
})
('t.ctx', async t => {
// NOTE: check that context is correctly exposed in chained calls
const ctx = t.click('#test').ctx;

ctx.val.browsers.push(await t.eval(()=>navigator.userAgent));
ctx.val.steps.push('test');
})
.after(async t => {
t.ctx.val.browsers.push(await t.eval(()=>navigator.userAgent));
t.ctx.val.steps.push('after');

throw `###${JSON.stringify(t.ctx.val)}###`;
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ test('Incorrect protocol', async t => {
await t.navigateTo('ftp://localhost:3000/fixtures/api/es-next/navigate-to-and-test-page/pages/index.html');
});

test.page('http://localhost:3000/fixtures/api/es-next/navigate-to-and-test-page/pages/navigation.html')('Page directive', async t => {
test
.page `http://localhost:3000/fixtures/api/es-next/navigate-to-and-test-page/pages/navigation.html`
('Page directive', async t => {
await t.click('#button');
});

0 comments on commit f62d05e

Please sign in to comment.