fix page.injectFile and add test. (#52)
This line within `injectFile` wasn't doing much of anything: ```js let expression = fs.readFile(filePath, 'utf8', (err, data) => callback({err, data})); ``` * That's fixed. * A path error in examples/features.js is fixed. * Test added for injectFile.
This commit is contained in:
parent
3d3e8dd038
commit
6c7ae41ae6
@ -19,7 +19,7 @@ var Browser = require('../lib/Browser');
|
|||||||
var browser = new Browser();
|
var browser = new Browser();
|
||||||
|
|
||||||
browser.newPage().then(async page => {
|
browser.newPage().then(async page => {
|
||||||
var modernizrPath = path.join('..', 'third_party', 'phantomjs', 'examples', 'modernizr.js');
|
var modernizrPath = path.join(__dirname, '../third_party/phantomjs/examples/modernizr.js');
|
||||||
await page.injectFile(modernizrPath);
|
await page.injectFile(modernizrPath);
|
||||||
page.on('consolemessage', console.log);
|
page.on('consolemessage', console.log);
|
||||||
await page.evaluate(detectFeatures);
|
await page.evaluate(detectFeatures);
|
||||||
|
12
lib/Page.js
12
lib/Page.js
@ -128,11 +128,13 @@ class Page extends EventEmitter {
|
|||||||
* @return {!Promise}
|
* @return {!Promise}
|
||||||
*/
|
*/
|
||||||
async injectFile(filePath) {
|
async injectFile(filePath) {
|
||||||
let callback;
|
const contents = await new Promise((resolve, reject) => {
|
||||||
let promise = new Promise(fulfill => callback = fulfill);
|
fs.readFile(filePath, 'utf8', (err, data) => {
|
||||||
let expression = fs.readFile(filePath, 'utf8', (err, data) => callback({err, data}));
|
if (err) return reject(err);
|
||||||
await promise;
|
resolve(data);
|
||||||
return this._client.send('Runtime.evaluate', { expression, returnByValue: true });
|
});
|
||||||
|
});
|
||||||
|
return this._client.send('Runtime.evaluate', { expression: contents, returnByValue: true });
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
1
test/assets/injectedfile.js
Normal file
1
test/assets/injectedfile.js
Normal file
@ -0,0 +1 @@
|
|||||||
|
__injected = 42;
|
@ -104,6 +104,15 @@ describe('Puppeteer', function() {
|
|||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('Page.injectFile', function() {
|
||||||
|
it('should fail when navigating to bad url', SX(async function() {
|
||||||
|
const helloPath = path.join(__dirname, './assets/injectedfile.js');
|
||||||
|
await page.injectFile(helloPath);
|
||||||
|
const result = await page.evaluate(() => __injected);
|
||||||
|
expect(result).toBe(42);
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
|
||||||
describe('Frame.evaluate', function() {
|
describe('Frame.evaluate', function() {
|
||||||
let FrameUtils = require('./frame-utils');
|
let FrameUtils = require('./frame-utils');
|
||||||
it('should have different execution contexts', SX(async function() {
|
it('should have different execution contexts', SX(async function() {
|
||||||
|
Loading…
Reference in New Issue
Block a user