dbffc3c35c
This patch teaches puppeteer to await promises returned from the inPageCallback.
94 lines
2.8 KiB
JavaScript
94 lines
2.8 KiB
JavaScript
var path = require('path');
|
|
var Browser = require('../lib/Browser');
|
|
|
|
var EMPTY_PAGE = 'file://' + path.join(__dirname, 'assets', 'empty.html');
|
|
|
|
describe('Puppeteer', function() {
|
|
var browser;
|
|
var page;
|
|
|
|
beforeAll(function() {
|
|
browser = new Browser();
|
|
});
|
|
|
|
afterAll(function() {
|
|
browser.close();
|
|
});
|
|
|
|
beforeEach(SX(async function() {
|
|
page = await browser.newPage();
|
|
}));
|
|
|
|
afterEach(function() {
|
|
page.close();
|
|
});
|
|
|
|
it('Page.evaluate', SX(async function() {
|
|
var result = await page.evaluate(() => 7 * 3);
|
|
expect(result).toBe(21);
|
|
}));
|
|
|
|
it('Page.evaluateAsync', SX(async function() {
|
|
var result = await page.evaluateAsync(() => Promise.resolve(8 * 7));
|
|
expect(result).toBe(56);
|
|
}));
|
|
|
|
it('Page Events: ConsoleMessage', SX(async function() {
|
|
var msgs = [];
|
|
page.on('consolemessage', msg => msgs.push(msg));
|
|
await page.evaluate(() => console.log('Message!'));
|
|
expect(msgs).toEqual(['Message!']);
|
|
}));
|
|
|
|
describe('Page.navigate', function() {
|
|
it('should fail when navigating to bad url', SX(async function() {
|
|
var success = await page.navigate('asdfasdf');
|
|
expect(success).toBe(false);
|
|
}));
|
|
it('should succeed when navigating to good url', SX(async function() {
|
|
var success = await page.navigate(EMPTY_PAGE);
|
|
expect(success).toBe(true);
|
|
}));
|
|
});
|
|
|
|
describe('Page.setInPageCallback', function() {
|
|
it('should work', SX(async function() {
|
|
await page.setInPageCallback('callController', function(a, b) {
|
|
return a * b;
|
|
});
|
|
|
|
var result = await page.evaluate(function() {
|
|
return callController(9, 4);
|
|
});
|
|
expect(result).toBe(36);
|
|
}));
|
|
it('should survive navigation', SX(async function() {
|
|
await page.setInPageCallback('callController', function(a, b) {
|
|
return a * b;
|
|
});
|
|
|
|
await page.navigate(EMPTY_PAGE);
|
|
var result = await page.evaluate(function() {
|
|
return callController(9, 4);
|
|
});
|
|
expect(result).toBe(36);
|
|
}));
|
|
it('should await returned promise', SX(async function() {
|
|
await page.setInPageCallback('callController', function(a, b) {
|
|
return Promise.resolve(a * b);
|
|
});
|
|
|
|
var result = await page.evaluate(function() {
|
|
return callController(3, 5);
|
|
});
|
|
expect(result).toBe(15);
|
|
}));
|
|
});
|
|
});
|
|
|
|
// Since Jasmine doesn't like async functions, they should be wrapped
|
|
// in a SX function.
|
|
function SX(fun) {
|
|
return done => Promise.resolve(fun()).then(done).catch(done.fail);
|
|
}
|