feat(firefox): support Page.setUserAgent method (#3968)

This commit is contained in:
Andrey Lushnikov 2019-02-08 19:53:27 -08:00 committed by GitHub
parent 1ce578b787
commit fc18a79e99
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 7 deletions

View File

@ -88,6 +88,23 @@ class Page extends EventEmitter {
this._viewport = null;
}
/**
* @param {string} userAgent
*/
async setUserAgent(userAgent) {
await this._session.send('Page.setUserAgent', {userAgent});
}
/**
* @param {{viewport: !Puppeteer.Viewport, userAgent: string}} options
*/
async emulate(options) {
await Promise.all([
this.setViewport(options.viewport),
this.setUserAgent(options.userAgent),
]);
}
/**
* @return {BrowserContext}
*/

View File

@ -9,7 +9,7 @@
"node": ">=8.9.4"
},
"puppeteer": {
"firefox_revision": "9702d3d2dd86550e385fadec66eb28a5ff2b1322"
"firefox_revision": "ba500612786aac157fa44e6b4d04c852f51ea35c"
},
"scripts": {
"install": "node install.js",

View File

@ -82,12 +82,12 @@ module.exports.addTests = function({testRunner, expect, product}) {
});
});
describe_fails_ffox('Page.emulate', function() {
describe('Page.emulate', function() {
it('should work', async({page, server}) => {
await page.goto(server.PREFIX + '/mobile.html');
await page.emulate(iPhone);
expect(await page.evaluate(() => window.innerWidth)).toBe(375);
expect(await page.evaluate(() => navigator.userAgent)).toContain('Safari');
expect(await page.evaluate(() => navigator.userAgent)).toContain('iPhone');
});
it('should support clicking', async({page, server}) => {
await page.emulate(iPhone);

View File

@ -628,21 +628,30 @@ module.exports.addTests = function({testRunner, expect, headless, Errors, Device
});
});
describe_fails_ffox('Page.setUserAgent', function() {
describe('Page.setUserAgent', function() {
it('should work', async({page, server}) => {
expect(await page.evaluate(() => navigator.userAgent)).toContain('Mozilla');
page.setUserAgent('foobar');
await page.setUserAgent('foobar');
const [request] = await Promise.all([
server.waitForRequest('/empty.html'),
page.goto(server.EMPTY_PAGE),
]);
expect(request.headers['user-agent']).toBe('foobar');
});
it('should work for subframes', async({page, server}) => {
expect(await page.evaluate(() => navigator.userAgent)).toContain('Mozilla');
await page.setUserAgent('foobar');
const [request] = await Promise.all([
server.waitForRequest('/empty.html'),
utils.attachFrame(page, 'frame1', server.EMPTY_PAGE),
]);
expect(request.headers['user-agent']).toBe('foobar');
});
it('should emulate device user-agent', async({page, server}) => {
await page.goto(server.PREFIX + '/mobile.html');
expect(await page.evaluate(() => navigator.userAgent)).toContain('Chrome');
expect(await page.evaluate(() => navigator.userAgent)).not.toContain('iPhone');
await page.setUserAgent(iPhone.userAgent);
expect(await page.evaluate(() => navigator.userAgent)).toContain('Safari');
expect(await page.evaluate(() => navigator.userAgent)).toContain('iPhone');
});
});