This repository was archived by the owner on Dec 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
Headless test using puppeteer #13
Open
samarpanda
wants to merge
1
commit into
GoogleChromeLabs:master
Choose a base branch
from
samarpanda:add-test
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,20 @@ | ||
{ | ||
"env": { | ||
"browser": true, | ||
"es6": true | ||
"es6": true, | ||
"node": true | ||
}, | ||
"extends": [ | ||
"eslint:recommended", | ||
"google" | ||
], | ||
"rules": { | ||
"no-var": 0, | ||
"no-var": 0 | ||
}, | ||
"parserOptions": { | ||
"ecmaVersion": 8 | ||
}, | ||
"globals": { | ||
"perfMetrics": true | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
language: node_js | ||
node_js: | ||
- 10.13.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
const server = require('./server'); | ||
const puppeteer = require('puppeteer'); | ||
const assert = require('assert'); | ||
const PORT = 8080; | ||
let i = 0; | ||
|
||
/** | ||
* Puppeteer init method | ||
* @param {function} callback | ||
*/ | ||
async function init(callback) { | ||
const browser = await puppeteer.launch({}); | ||
for (i of [0, 1, 2, 3]) { | ||
await openPage(browser); | ||
} | ||
await browser.close(); | ||
callback(); | ||
} | ||
|
||
/** | ||
* Open url in browser | ||
* @param {*} browser | ||
*/ | ||
async function openPage(browser) { | ||
const page = await browser.newPage(); | ||
await page.goto(`http://localhost:${PORT}`, { | ||
waitUntil: 'networkidle0', | ||
}); | ||
const result = await page.evaluate(inputDelayHandler, i); | ||
assert(result, `Got input delay value as ${result} for mousedown`); | ||
await page.close(); | ||
} | ||
|
||
/** | ||
* FID value is returned | ||
* @param {number} i | ||
* @return {number} FID value is returned | ||
*/ | ||
function inputDelayHandler(i) { | ||
return new Promise((resolve, reject) => { | ||
perfMetrics.onFirstInputDelay(function(delay, event) { | ||
resolve(delay); | ||
}); | ||
const eventsArr = [ | ||
() => { | ||
let mEvent = document.createEvent('MouseEvents'); | ||
mEvent.initMouseEvent('touchstart', true, true, window); | ||
document.body.dispatchEvent(mEvent); | ||
}, | ||
() => { | ||
let mEvent = document.createEvent('MouseEvents'); | ||
mEvent.initMouseEvent('click', true, true, window); | ||
document.body.dispatchEvent(mEvent); | ||
}, | ||
() => { | ||
let mEvent = document.createEvent('MouseEvents'); | ||
mEvent.initMouseEvent('mousedown', true, true, window); | ||
document.body.dispatchEvent(mEvent); | ||
}, | ||
() => { | ||
let kEvent = document.createEvent('KeyboardEvent'); | ||
kEvent.initKeyboardEvent( | ||
'keydown', true, true, window, false, false, false, false, 40, 0 | ||
); | ||
document.body.dispatchEvent(kEvent); | ||
}, | ||
]; | ||
eventsArr[i](); | ||
|
||
/** | ||
* Page timeout | ||
*/ | ||
setTimeout(() => { | ||
reject(false); | ||
}, 1000); | ||
}); | ||
} | ||
|
||
server.start(PORT); | ||
init(() => { | ||
server.stop(); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
let http = require('http'); | ||
let fs = require('fs'); | ||
let fidScript; | ||
let indexHtml; | ||
|
||
fidScript = fs.readFileSync('./src/first-input-delay.js', 'utf8'); | ||
|
||
indexHtml = ` | ||
<html> | ||
<head> | ||
<title>FID Page</title> | ||
<script>${fidScript}</script> | ||
</head> | ||
<h1>FID</h1> | ||
<p>First Input Delay</p> | ||
<span id="link1">Some link</span> | ||
</html>`; | ||
|
||
let server = http.createServer((req, res) => { | ||
res.setHeader('content-type', 'text/html'); | ||
res.writeHead(200, {'Content-Type': 'text/html'}); | ||
res.end(indexHtml); | ||
}); | ||
|
||
server.on('close', () => { | ||
process.exit(); | ||
}); | ||
|
||
module.exports.start = (port) => { | ||
server.listen(port); | ||
}; | ||
|
||
module.exports.stop = () => { | ||
server.close(); | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.