Skip to content

Commit

Permalink
Implemented headless browser redirection tests.
Browse files Browse the repository at this point in the history
Change-Id: I1e772fc31ede9a85d6cea1726c3c7fa9d3cd34ca
Reviewed-on: https://chromium-review.googlesource.com/1147741
Reviewed-by: Andrey Kosyakov <caseq@chromium.org>
Commit-Queue: Peter Kvitek <kvitekp@chromium.org>
Cr-Commit-Position: refs/heads/master@{#578737}
  • Loading branch information
Peter Kvitek authored and Commit Bot committed Jul 27, 2018
1 parent d858be3 commit 49da670
Show file tree
Hide file tree
Showing 50 changed files with 1,170 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
}

getFrameId_(frameId) {
return frameId === this.mainFrameId_ ? 'MainFrame' : frameId
return frameId === this.mainFrameId_ ? 'MainFrame' : `<${typeof frameId}>`
}

});
74 changes: 72 additions & 2 deletions headless/test/data/protocol/helpers/http-interceptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
constructor(testRunner, dp) {
this.testRunner_ = testRunner;
this.dp_ = dp;
this.disabledRequestedUrlsLogging = false;
this.responses_ = new Map();
this.requestedUrls_ = [];
this.requestedMethods_ = [];
}

/**
Expand All @@ -29,9 +32,17 @@
{ patterns: [{ urlPattern: '*' }] });

this.dp_.Network.onRequestIntercepted(event => {
const url = event.params.request.url;
const method = event.params.request.method;
this.requestedMethods_.push(method);

const url = event.params.request.url
+ (event.params.request.urlFragment || '');
this.requestedUrls_.push(url);

if (this.responses_.has(url)) {
this.testRunner_.log(`requested url: ${url}`);
if (!this.disabledRequestedUrlsLogging) {
this.testRunner_.log(`requested url: ${url}`);
}
} else {
this.testRunner_.log(`requested url: ${url} is not known`);
this.logResponses();
Expand All @@ -48,15 +59,74 @@
return this;
}

/**
* Prevents requested urls from being logged. This helps to stabilize tests
* that request urls in arbitrary order. Use hasRequestedUrls(urls) function
* to check if expected urls has been requested.
*
* @param {boolean} value True if requested url logging is disabled, false
* otherwise.
*/
setDisableRequestedUrlsLogging(value) {
this.disabledRequestedUrlsLogging = value;
}

/**
* Adds request response.
*
* @param {!string} url Request url, including #fragment.
* @param {?string} body Request response body, optional.
* @param {?[string]} headers Request response headers, optional.
*/
addResponse(url, body, headers) {
this.responses_.set(url, {body, headers});
}

/**
* Logs registered request responses.
*/
logResponses() {
this.testRunner_.log(`Responses: ${this.responses_.size}`);
for (const [url, value] of this.responses_.entries()) {
this.testRunner_.log(
`url=${url}\nbody=${value.body}\nheaders=${value.headers}`);
}
}

/**
* Logs requested methods in the order requests have been received.
*/
logRequestedMethods() {
this.testRunner_.log(`Requested methods: ${this.requestedMethods_.length}`);
for (const method of this.requestedMethods_) {
this.testRunner_.log(` ${method}`);
}
}

/**
* Logs requested urls in the order requests have been received.
*/
logRequestedUrls() {
this.testRunner_.log(`Requested urls: ${this.requestedUrls_.length}`);
for (const url of this.requestedUrls_) {
this.testRunner_.log(` ${url}`);
}
}

/**
* Checks if specified urls have been requested.
*
* @param {[string]} urls Array of urls to check against requested urls.
*/
hasRequestedUrls(urls) {
this.testRunner_.log(`Expected requested urls:`);
for (const url of urls) {
if (this.requestedUrls_.indexOf(url) >= 0) {
this.testRunner_.log(` ${url}`);
} else {
this.testRunner_.log(` ${url} is MISSING`);
}
}
}

});
7 changes: 6 additions & 1 deletion headless/test/data/protocol/helpers/renderer-test-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
* virtualTimeController references.
*/
async init() {
await this.dp_.Target.enable();
await this.dp_.Page.enable();

let HttpInterceptor = await this.testRunner_.loadScript(
Expand All @@ -45,6 +44,12 @@
let virtualTimeController =
new VirtualTimeController(this.testRunner_, this.dp_, 25);

this.dp_.Runtime.enable();
this.dp_.Runtime.onConsoleAPICalled(data => {
const text = data.params.args[0].value;
this.testRunner_.log(text);
});

return {httpInterceptor, frameNavigationHelper, virtualTimeController};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@
let {httpInterceptor, frameNavigationHelper, virtualTimeController} =
await (new RendererTestHelper(testRunner, dp, page)).init();

dp.Runtime.enable();
dp.Runtime.onConsoleAPICalled(data => {
const text = data.params.args[0].value;
testRunner.log(text);
});

// Only first 3 scripts of 4 on the page are white listed for execution.
// Therefore only 3 lines in the log are expected.
httpInterceptor.addResponse(`http://example.com/`,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Tests renderer: canvas.
requested url: http://www.example.com/
requested url: http://www.example.com/circle.svg
requested url: http://www.example.com/blur.svg
requested url: http://www.example.com/blur.svg#blur
Screenshot size: 100 x 100
rgba @(0,0): 163,163,163,255
rgba @(1,1): 222,225,231,255
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@

// A blur filter stored inside an svg file.
httpInterceptor.addResponse(
`http://www.example.com/blur.svg`,
`http://www.example.com/blur.svg#blur`,
`<svg width="100" height="100" version="1.1"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Tests renderer: double redirection.
requested url: http://www.example.com/
requested url: http://www.example.com/2
<p>Pass</p>
Frames: 1
frameId=MainFrame
url=http://www.example.com/
url=http://www.example.com/2
ScheduledNavigations: 1
frameId=MainFrame
url=http://www.example.com/1 reason=scriptInitiated
url=http://www.example.com/2 reason=scriptInitiated
42 changes: 42 additions & 0 deletions headless/test/data/protocol/sanity/renderer-double-redirect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

(async function(testRunner) {
let {page, session, dp} = await testRunner.startBlank(
'Tests renderer: double redirection.');

let RendererTestHelper =
await testRunner.loadScript('../helpers/renderer-test-helper.js');
let {httpInterceptor, frameNavigationHelper, virtualTimeController} =
await (new RendererTestHelper(testRunner, dp, page)).init();

// Two navigations have been scheduled while the document was loading, but
// only the second one was started. It canceled the first one.
httpInterceptor.addResponse('http://www.example.com/',
`<html>
<head>
<title>Hello, World 1</title>
<script>
document.location='http://www.example.com/1';
document.location='http://www.example.com/2';
</script>
</head>
<body>http://www.example.com/1</body>
</html>`);

httpInterceptor.addResponse('http://www.example.com/2',
'<p>Pass</p>');

await virtualTimeController.grantInitialTime(1000, 1000,
null,
async () => {
testRunner.log(await session.evaluate('document.body.innerHTML'));
frameNavigationHelper.logFrames();
frameNavigationHelper.logScheduledNavigations();
testRunner.completeTest();
}
);

await frameNavigationHelper.navigate('http://www.example.com/');
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Tests renderer: frame load events.
FRAME A 1
IFRAME 1
Frames: 4
frameId=MainFrame
url=http://example.com/1
frameId=<string>
url=http://example.com/frameA/
url=http://example.com/frameA/1
frameId=<string>
url=http://example.com/frameB/
url=http://example.com/frameB/1
frameId=<string>
url=http://example.com/frameB/1/iframe/
url=http://example.com/frameB/1/iframe/1
ScheduledNavigations: 3
frameId=<string>
url=http://example.com/frameA/1 reason=scriptInitiated
frameId=<string>
url=http://example.com/frameB/1 reason=scriptInitiated
frameId=<string>
url=http://example.com/frameB/1/iframe/1 reason=scriptInitiated
Expected requested urls:
http://example.com/
http://example.com/1
http://example.com/frameA/
http://example.com/frameA/1
http://example.com/frameB/
http://example.com/frameB/1
http://example.com/frameB/1/iframe/
http://example.com/frameB/1/iframe/1
31 changes: 26 additions & 5 deletions headless/test/data/protocol/sanity/renderer-frame-load-events.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

(async function(testRunner) {
let {page, session, dp} = await testRunner.startBlank(
'Tests renderer: content security policy.');
'Tests renderer: frame load events.');

let RendererTestHelper =
await testRunner.loadScript('../helpers/renderer-test-helper.js');
Expand All @@ -16,8 +16,8 @@

httpInterceptor.addResponse(`http://example.com/1`,
`<html><frameset>
<frame src="http://example.com/frameA/" id="frameA">
<frame src="http://example.com/frameB/" id="frameB">
<frame id="frameA" src="http://example.com/frameA/">
<frame id="frameB" src="http://example.com/frameB/">
</frameset></html>`);

httpInterceptor.addResponse(`http://example.com/frameA/`,
Expand All @@ -35,7 +35,7 @@

httpInterceptor.addResponse(`http://example.com/frameB/1`,
`<html><body>FRAME B 1
<iframe src="http://example.com/frameB/1/iframe/" id="iframe"></iframe>
<iframe id="iframe" src="http://example.com/frameB/1/iframe/"></iframe>
</body></html>`);

httpInterceptor.addResponse(`http://example.com/frameB/1/iframe/`,
Expand All @@ -46,12 +46,33 @@
httpInterceptor.addResponse(`http://example.com/frameB/1/iframe/1`,
`<html><body>IFRAME 1</body><html>`);

// Frame redirection requests are handled in an arbitrary order, so disable
// requested url logging to ensure test's stability.
httpInterceptor.setDisableRequestedUrlsLogging(true);

await virtualTimeController.grantInitialTime(500, 1000,
null,
async () => {
testRunner.log(await session.evaluate('document.body.innerHTML'));
testRunner.log(await session.evaluate(
`document.getElementById('frameA').contentDocument.body.innerText`));
testRunner.log(await session.evaluate(
`document.getElementById('frameB').contentDocument.` +
`getElementById('iframe').contentDocument.body.innerHTML`));

frameNavigationHelper.logFrames();
frameNavigationHelper.logScheduledNavigations();

httpInterceptor.hasRequestedUrls([
'http://example.com/',
'http://example.com/1',
'http://example.com/frameA/',
'http://example.com/frameA/1',
'http://example.com/frameB/',
'http://example.com/frameB/1',
'http://example.com/frameB/1/iframe/',
'http://example.com/frameB/1/iframe/1'
]);

testRunner.completeTest();
}
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Tests renderer: frames redirection chain.
<p>FRAME A</p>
<p>IFRAME B</p>
Frames: 4
frameId=MainFrame
url=http://www.example.com/1
frameId=<string>
url=http://www.example.com/frameA/
url=http://www.example.com/frameA/2
frameId=<string>
url=http://www.example.com/frameB/
frameId=<string>
url=http://www.example.com/iframe/
url=http://www.example.com/iframe/3
ScheduledNavigations: 2
frameId=<string>
url=http://www.example.com/frameA/1 reason=scriptInitiated
frameId=<string>
url=http://www.example.com/iframe/1 reason=scriptInitiated
Expected requested urls:
http://www.example.com/
http://www.example.com/1
http://www.example.com/frameA/
http://www.example.com/frameA/1
http://www.example.com/frameA/2
http://www.example.com/frameB/
http://www.example.com/iframe/
http://www.example.com/iframe/1
http://www.example.com/iframe/2
http://www.example.com/iframe/3
Loading

0 comments on commit 49da670

Please sign in to comment.