Skip to content

Commit

Permalink
Disable page caching (close #3780) (#4013)
Browse files Browse the repository at this point in the history
* initial

* fix linting

* small refactoring

* fix lint

* fix review issues

* add lost option name

* try to fix tests
  • Loading branch information
miherlosev authored Aug 16, 2019
1 parent 0e2f80f commit 62bf5a2
Show file tree
Hide file tree
Showing 20 changed files with 163 additions and 39 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@
"source-map-support": "^0.5.5",
"strip-bom": "^2.0.0",
"testcafe-browser-tools": "1.6.8",
"testcafe-hammerhead": "14.7.4",
"testcafe-hammerhead": "14.8.0",
"testcafe-legacy-api": "3.1.11",
"testcafe-reporter-json": "^2.1.0",
"testcafe-reporter-list": "^2.1.0",
Expand Down
3 changes: 3 additions & 0 deletions src/api/structure/test-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ export default class TestFile {

if (test.disablePageReloads === void 0)
test.disablePageReloads = test.fixture.disablePageReloads;

if (!test.disablePageCaching)
test.disablePageCaching = test.fixture.disablePageCaching;
});

return this.collectedTests;
Expand Down
7 changes: 7 additions & 0 deletions src/api/structure/testing-unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export default class TestingUnit {
this.clientScripts = [];

this.disablePageReloads = void 0;
this.disablePageCaching = false;

this.apiMethodWasCalled = new FlagList([OPTION_NAMES.clientScripts, OPTION_NAMES.requestHooks]);

Expand Down Expand Up @@ -99,6 +100,12 @@ export default class TestingUnit {
return this.apiOrigin;
}

_disablePageCaching$getter () {
this.disablePageCaching = true;

return this.apiOrigin;
}

static _makeAPIListForChildClass (ChildClass) {
ChildClass.API_LIST = TestingUnit.API_LIST.concat(getDelegatedAPIList(ChildClass.prototype));
}
Expand Down
1 change: 1 addition & 0 deletions src/cli/argument-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export default class CLIArgumentParser {
.option('--sf, --stop-on-first-fail', 'stop an entire test run if any test fails')
.option('--ts-config-path <path>', 'use a custom TypeScript configuration file and specify its location')
.option('--cs, --client-scripts <paths>', 'inject scripts into tested pages', this._parseList, [])
.option('--disable-page-caching', 'disable page caching during test execution')

// NOTE: these options will be handled by chalk internally
.option('--color', 'force colors in command line')
Expand Down
4 changes: 3 additions & 1 deletion src/configuration/option-names.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,7 @@ export default {
retryTestPages: 'retryTestPages',
hostname: 'hostname',
port1: 'port1',
port2: 'port2'
port2: 'port2',
developmentMode: 'developmentMode',
disablePageCaching: 'disablePageCaching'
};
5 changes: 4 additions & 1 deletion src/configuration/testcafe-configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ const OPTION_FLAG_NAMES = [
OPTION_NAMES.debugOnFail,
OPTION_NAMES.skipUncaughtErrors,
OPTION_NAMES.stopOnFirstFail,
OPTION_NAMES.takeScreenshotsOnFails
OPTION_NAMES.takeScreenshotsOnFails,
OPTION_NAMES.disablePageCaching,
OPTION_NAMES.developmentMode,
OPTION_NAMES.retryTestPages,
];

export default class TestCafeConfiguration extends Configuration {
Expand Down
30 changes: 1 addition & 29 deletions src/runner/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -431,35 +431,7 @@ export default class Runner extends EventEmitter {

run (options = {}) {
this.apiMethodWasCalled.reset();

const {
skipJsErrors,
disablePageReloads,
quarantineMode,
debugMode,
selectorTimeout,
assertionTimeout,
pageLoadTimeout,
speed,
debugOnFail,
skipUncaughtErrors,
stopOnFirstFail
} = options;

this.configuration.mergeOptions({
skipJsErrors: skipJsErrors,
disablePageReloads: disablePageReloads,
quarantineMode: quarantineMode,
debugMode: debugMode,
debugOnFail: debugOnFail,
selectorTimeout: selectorTimeout,
assertionTimeout: assertionTimeout,
pageLoadTimeout: pageLoadTimeout,
speed: speed,
skipUncaughtErrors: skipUncaughtErrors,
stopOnFirstFail: stopOnFirstFail
});

this.configuration.mergeOptions(options);
this._setBootstrapperOptions();

const runTaskPromise = Promise.resolve()
Expand Down
1 change: 1 addition & 0 deletions src/test-run/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export default class TestRun extends AsyncEventEmitter {
this.pageLoadTimeout = this.opts.pageLoadTimeout;

this.disablePageReloads = test.disablePageReloads || opts.disablePageReloads && test.disablePageReloads !== false;
this.disablePageCaching = test.disablePageCaching || opts.disablePageCaching;

this.session = SessionController.getSession(this);

Expand Down
2 changes: 2 additions & 0 deletions src/test-run/session-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ export default class SessionController extends Session {
session.currentTestRun = testRun;
}

session.disablePageCaching = testRun.disablePageCaching;

sessionInfo = {
session: session,
proxy: null,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { ClientFunction, Role, Selector, t } from 'testcafe';

const getPageLocation = ClientFunction(() => window.location.toString());

const url = 'http://localhost:3000/fixtures/run-options/disable-page-caching/pages/index.html';

const expectedRoleLastPageLocation = 'http://localhost:3000/fixtures/run-options/disable-page-caching/pages/third.html';

const role = Role(url, async () => {
await t
.click(Selector('#first'))
.click(Selector('#second'))
.click(Selector('#third'));

role.lastPageLocation = await getPageLocation();
});

export {
role,
url,
expectedRoleLastPageLocation
};

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<button id="first">1. Set value in local storage</button>
<button id="second">2. Open link in a new window.</button>
<script>
var firstBtn = document.getElementById("first");
var secondBtn = document.getElementById("second");

firstBtn.addEventListener('click', function () {
window.localStorage.setItem(
'testItem',
JSON.stringify({ link: './third.html' })
);
});

secondBtn.addEventListener('click', function () {
window.open('./second.html', '_blank');
});
</script>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<a id="third">3. Link to the third page</a>
<script>
var data = JSON.parse(localStorage.getItem('testItem'));
var link = document.getElementById('third');

if (data)
link.href = data.link;
</script>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>Third page</h1>
</body>
</html>
13 changes: 13 additions & 0 deletions test/functional/fixtures/run-options/disable-page-caching/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
describe('Disable page caching', () => {
it('Test run', () => {
return runTests('testcafe-fixtures/test-run.js', null, { disablePageCaching: true });
});

it('Fixture', () => {
return runTests('testcafe-fixtures/fixture.js');
});

it('Single test', () => {
return runTests('testcafe-fixtures/single-test.js');
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { role, url, expectedRoleLastPageLocation } from '../common/index';

fixture
.disablePageCaching `Fixture`
.page(url)
.beforeEach( async t => {
await t.useRole(role);
});

test('test', async t => {
await t.expect(role.lastPageLocation).eql(expectedRoleLastPageLocation);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { role, url, expectedRoleLastPageLocation } from '../common/index';

fixture `Fixture`;

test
.disablePageCaching
.page(url)
.before(async t => {
await t.useRole(role);
})
('test', async t => {
await t.expect(role.lastPageLocation).eql(expectedRoleLastPageLocation);
});

Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { role, url, expectedRoleLastPageLocation } from '../common/index';

fixture `Fixture`
.page(url)
.beforeEach( async t => {
await t.useRole(role);
});

test('test', async t => {
await t.expect(role.lastPageLocation).eql(expectedRoleLastPageLocation);
});
6 changes: 4 additions & 2 deletions test/functional/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@ before(function () {
useProxy: proxy,
only: onlyOption,
skip: skipOption,
stopOnFirstFail
stopOnFirstFail,
disablePageCaching
} = opts;

const actualBrowsers = browsersInfo.filter(browserInfo => {
Expand Down Expand Up @@ -259,7 +260,8 @@ before(function () {
pageLoadTimeout,
speed,
stopOnFirstFail,
skipUncaughtErrors
skipUncaughtErrors,
disablePageCaching
})
.then(failedCount => {
if (customReporters)
Expand Down
5 changes: 4 additions & 1 deletion test/functional/site/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ const NON_CACHEABLE_PAGES = [

const UPLOAD_SUCCESS_PAGE_TEMPLATE = readSync('./views/upload-success.html.mustache');

const shouldCachePage = function (reqUrl) {
return NON_CACHEABLE_PAGES.every(pagePrefix => !reqUrl.startsWith(pagePrefix));
};

const Server = module.exports = function (port, basePath) {
const server = this;
Expand Down Expand Up @@ -73,7 +76,7 @@ Server.prototype._setupRoutes = function () {
.then(function (content) {
res.setHeader('content-type', CONTENT_TYPES[path.extname(resourcePath)]);

if (NON_CACHEABLE_PAGES.every(pagePrefix => !reqPath.startsWith(pagePrefix)))
if (shouldCachePage(reqPath))
res.setHeader('cache-control', 'max-age=3600');

setTimeout(function () {
Expand Down
10 changes: 6 additions & 4 deletions test/server/cli-argument-parser-test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const expect = require('chai').expect;
const { expect } = require('chai');
const path = require('path');
const fs = require('fs');
const tmp = require('tmp');
const find = require('lodash').find;
const { find } = require('lodash');
const CliArgumentParser = require('../../lib/cli/argument-parser');
const nanoid = require('nanoid');

Expand Down Expand Up @@ -519,7 +519,7 @@ describe('CLI argument parser', function () {
});

it('Should parse command line arguments', function () {
return parse('-r list -S -q -e --hostname myhost --proxy localhost:1234 --proxy-bypass localhost:5678 --qr-code --app run-app --speed 0.5 --debug-on-fail --disable-page-reloads --dev --sf ie test/server/data/file-list/file-1.js')
return parse('-r list -S -q -e --hostname myhost --proxy localhost:1234 --proxy-bypass localhost:5678 --qr-code --app run-app --speed 0.5 --debug-on-fail --disable-page-reloads --dev --sf --disable-page-caching ie test/server/data/file-list/file-1.js')
.then(parser => {
expect(parser.browsers).eql(['ie']);
expect(parser.src).eql(['test/server/data/file-list/file-1.js']);
Expand All @@ -538,6 +538,7 @@ describe('CLI argument parser', function () {
expect(parser.opts.proxyBypass).to.be.ok;
expect(parser.opts.debugOnFail).to.be.ok;
expect(parser.opts.stopOnFirstFail).to.be.ok;
expect(parser.opts.disablePageCaching).to.be.ok;
});
});

Expand Down Expand Up @@ -584,7 +585,8 @@ describe('CLI argument parser', function () {
{ long: '--video-options' },
{ long: '--video-encoding-options' },
{ long: '--ts-config-path' },
{ long: '--client-scripts', short: '--cs' }
{ long: '--client-scripts', short: '--cs' },
{ long: '--disable-page-caching' }
];

const parser = new CliArgumentParser('');
Expand Down

0 comments on commit 62bf5a2

Please sign in to comment.