Provides device emulation via -d/--device flag using playwright#80
Provides device emulation via -d/--device flag using playwright#80
Conversation
sergeychernyshev
left a comment
There was a problem hiding this comment.
Not sure why, but npm test prog has some outstanding promises.
c92a410 to
5177303
Compare
441c132 to
f2ed6ba
Compare
20e81b0 to
07e2d02
Compare
| import { launchTest } from '../index.js'; | ||
| import fs from 'fs'; | ||
| import { devices } from 'playwright/test'; | ||
|
|
||
| import { BrowserConfig } from '../lib/browsers.js'; | ||
| import { normalizeCLIConfig } from '../lib/config.js'; | ||
| const browsers = BrowserConfig.getBrowsers(); | ||
|
|
||
| const device = 'Nexus 10' | ||
| const browser = 'chrome' | ||
|
|
||
|
|
||
|
|
||
| test('Setting device emulation updates the config', async () => { | ||
| let options = { | ||
| browser: 'chrome', | ||
| device: device, | ||
| url: 'https://www.example.com', | ||
| }; | ||
| var config = normalizeCLIConfig(options); | ||
|
|
||
| const result = await launchTest(config); | ||
|
|
||
| expect(result).toHaveProperty('success'); | ||
| expect(result).toHaveProperty('testId'); | ||
| expect(result).toHaveProperty('resultsPath'); | ||
| expect(result.success).toBe(true); | ||
| expect(fs.existsSync(result.resultsPath)).toBe(true); | ||
| }); | ||
|
|
||
|
|
||
| // describe.each(browsers)('Programmatic API (%s)', browser => { | ||
| // describe.each(Object.keys(devices))( | ||
| // 'launchTest executes and returns result object when emulating device: %s', | ||
| // device => { | ||
| // test('Setting device emulation updates the config', async () => { | ||
| // console.log(devices) | ||
| // let options = { | ||
| // browser, | ||
| // device: device, | ||
| // url: '../tests/sandbox/index.html', | ||
| // }; | ||
| // options = normalizeCLIConfig(options); | ||
| // let config = new BrowserConfig().getBrowserConfig(browser, options); | ||
| // expect(config && typeof config === 'object').toBe(true); | ||
| // // test collecting device data for desktop devices | ||
| // if (device.toLowerCase().includes('desktop')) { | ||
| // expect(config.isMobile).toBe(false); | ||
| // expect(config.hasTouch).toBe(false); | ||
| // } | ||
| // // test collecting device data for non-desktop devices | ||
| // else { | ||
| // expect(config.isMobile).toBe(true); | ||
| // expect(config.hasTouch).toBe(true); | ||
| // expect(config.deviceScaleFactor).toBeDefined(); | ||
| // } | ||
|
|
||
| // const result = await launchTest(options); | ||
|
|
||
| // expect(result).toHaveProperty('success'); | ||
| // expect(result).toHaveProperty('testId'); | ||
| // expect(result).toHaveProperty('resultsPath'); | ||
| // expect(result.success).toBe(true); | ||
| // expect(fs.existsSync(result.resultsPath)).toBe(true); | ||
| // }); | ||
| // }, | ||
| // ); | ||
| // }); |
There was a problem hiding this comment.
| import { launchTest } from '../index.js'; | |
| import fs from 'fs'; | |
| import { devices } from 'playwright/test'; | |
| import { BrowserConfig } from '../lib/browsers.js'; | |
| import { normalizeCLIConfig } from '../lib/config.js'; | |
| const browsers = BrowserConfig.getBrowsers(); | |
| const device = 'Nexus 10' | |
| const browser = 'chrome' | |
| test('Setting device emulation updates the config', async () => { | |
| let options = { | |
| browser: 'chrome', | |
| device: device, | |
| url: 'https://www.example.com', | |
| }; | |
| var config = normalizeCLIConfig(options); | |
| const result = await launchTest(config); | |
| expect(result).toHaveProperty('success'); | |
| expect(result).toHaveProperty('testId'); | |
| expect(result).toHaveProperty('resultsPath'); | |
| expect(result.success).toBe(true); | |
| expect(fs.existsSync(result.resultsPath)).toBe(true); | |
| }); | |
| // describe.each(browsers)('Programmatic API (%s)', browser => { | |
| // describe.each(Object.keys(devices))( | |
| // 'launchTest executes and returns result object when emulating device: %s', | |
| // device => { | |
| // test('Setting device emulation updates the config', async () => { | |
| // console.log(devices) | |
| // let options = { | |
| // browser, | |
| // device: device, | |
| // url: '../tests/sandbox/index.html', | |
| // }; | |
| // options = normalizeCLIConfig(options); | |
| // let config = new BrowserConfig().getBrowserConfig(browser, options); | |
| // expect(config && typeof config === 'object').toBe(true); | |
| // // test collecting device data for desktop devices | |
| // if (device.toLowerCase().includes('desktop')) { | |
| // expect(config.isMobile).toBe(false); | |
| // expect(config.hasTouch).toBe(false); | |
| // } | |
| // // test collecting device data for non-desktop devices | |
| // else { | |
| // expect(config.isMobile).toBe(true); | |
| // expect(config.hasTouch).toBe(true); | |
| // expect(config.deviceScaleFactor).toBeDefined(); | |
| // } | |
| // const result = await launchTest(options); | |
| // expect(result).toHaveProperty('success'); | |
| // expect(result).toHaveProperty('testId'); | |
| // expect(result).toHaveProperty('resultsPath'); | |
| // expect(result.success).toBe(true); | |
| // expect(fs.existsSync(result.resultsPath)).toBe(true); | |
| // }); | |
| // }, | |
| // ); | |
| // }); | |
| import { launchTest } from '../index.js'; | |
| import { BrowserConfig } from '../lib/browsers.js'; | |
| import { devices } from 'playwright/test'; | |
| import { normalizeCLIConfig } from '../lib/config.js'; | |
| const browsers = BrowserConfig.getBrowsers(); | |
| const availableDevices = devices.filter(device => browsers.contains(device.defaultBrowserType)); | |
| describe.each(availableDevices)('Setting device emulation updates the config: %s', device => { | |
| test('Setting device emulation updates the config', () => { | |
| let options = { | |
| browser: device.defaultBrowserType, | |
| device: device.name, | |
| url: '../tests/sandbox/index.html', | |
| }; | |
| options = normalizeCLIConfig(options); | |
| let config = new BrowserConfig().getBrowserConfig(device.defaultBrowserType, options); | |
| expect(config && typeof config === 'object').toBe(true); | |
| expect(config.isMobile).toBe(device.isMobile); | |
| expect(config.hasTouch).toBe(device.hasTouch); | |
| expect(config.width).toBe(device.viewport.width); | |
| expect(config.height).toBe(device.viewport.height); | |
| expect(config.deviceScaleFactor).toBeDefined(); | |
| }); | |
| }); | |
| describe.each(availableDevices)('Setting device emulation with overrides updates the config: %s', device => { | |
| test('Setting device emulation updates the config', () => { | |
| let options = { | |
| browser: device.defaultBrowserType, | |
| device: device.name, | |
| width: 9999, | |
| height: 9999, | |
| url: '../tests/sandbox/index.html', | |
| }; | |
| options = normalizeCLIConfig(options); | |
| let config = new BrowserConfig().getBrowserConfig(device.defaultBrowserType, options); | |
| expect(config && typeof config === 'object').toBe(true); | |
| expect(config.isMobile).toBe(device.isMobile); | |
| expect(config.hasTouch).toBe(device.hasTouch); | |
| expect(config.width).toBe(9999); | |
| expect(config.height).toBe(9999); | |
| }); | |
| }); | |
| describe.each(availableDevices)('Device emulation: %s', device => { | |
| test('Device emulation successfully runs tests', async () => { | |
| let options = { | |
| browser: device.defaultBrowserType, | |
| device: device.name, | |
| url: '../tests/sandbox/index.html', | |
| }; | |
| options = normalizeCLIConfig(options); | |
| let config = new BrowserConfig().getBrowserConfig(device.defaultBrowserType, options); | |
| const result = await launchTest(config); | |
| expect(result).toHaveProperty('success'); | |
| expect(result).toHaveProperty('testId'); | |
| expect(result).toHaveProperty('resultsPath'); | |
| expect(result.success).toBe(true); | |
| expect(fs.existsSync(result.resultsPath)).toBe(true); | |
| }); | |
| }); |
| describe.each(Object.keys(devices))( | ||
| 'Setting device emulation updates the config for device: %s', | ||
| device => { | ||
| test('Setting device emulation updates the config', () => { | ||
| let options = { | ||
| browser, | ||
| device: device, | ||
| url: '../tests/sandbox/index.html', | ||
| }; | ||
| options = normalizeCLIConfig(options); | ||
| let config = new BrowserConfig().getBrowserConfig(browser, options); | ||
| expect(config && typeof config === 'object').toBe(true); | ||
| // test collecting device data for desktop devices | ||
| if (device.toLowerCase().includes('desktop')) { | ||
| expect(config.isMobile).toBe(false); | ||
| expect(config.hasTouch).toBe(false); | ||
| } | ||
| // test collecting device data for non-desktop devices | ||
| else { | ||
| expect(config.isMobile).toBe(true); | ||
| expect(config.hasTouch).toBe(true); | ||
| expect(config.deviceScaleFactor).toBeDefined(); | ||
| } | ||
| }); | ||
| }, | ||
| ); |
There was a problem hiding this comment.
emulation.test.js can cover launchTest
| describe.each(Object.keys(devices))( | |
| 'Setting device emulation updates the config for device: %s', | |
| device => { | |
| test('Setting device emulation updates the config', () => { | |
| let options = { | |
| browser, | |
| device: device, | |
| url: '../tests/sandbox/index.html', | |
| }; | |
| options = normalizeCLIConfig(options); | |
| let config = new BrowserConfig().getBrowserConfig(browser, options); | |
| expect(config && typeof config === 'object').toBe(true); | |
| // test collecting device data for desktop devices | |
| if (device.toLowerCase().includes('desktop')) { | |
| expect(config.isMobile).toBe(false); | |
| expect(config.hasTouch).toBe(false); | |
| } | |
| // test collecting device data for non-desktop devices | |
| else { | |
| expect(config.isMobile).toBe(true); | |
| expect(config.hasTouch).toBe(true); | |
| expect(config.deviceScaleFactor).toBeDefined(); | |
| } | |
| }); | |
| }, | |
| ); |
| // `npm test emlation` fails to finish | ||
| browserConfig.isMobile = true | ||
|
|
||
| // `npm test emulation` succeeds and finishes | ||
| // browserConfig.isMobile = false | ||
|
|
| String(DEFAULT_OPTIONS.height), | ||
| ), | ||
| ) | ||
| .addOption(new Option('--width <int>', 'Viewport width, in pixels')) |
There was a problem hiding this comment.
| .addOption(new Option('--width <int>', 'Viewport width, in pixels')) | |
| .addOption(new Option('--width <int>', 'Viewport width, in pixels. If both width and device are provided, the width value will override device emulation viewport width.')).default( | |
| String(DEFAULT_OPTIONS.width), | |
| ), |
| ), | ||
| ) | ||
| .addOption(new Option('--width <int>', 'Viewport width, in pixels')) | ||
| .addOption(new Option('--height <int>', 'Viewport height, in pixels')) |
There was a problem hiding this comment.
| .addOption(new Option('--height <int>', 'Viewport height, in pixels')) | |
| .addOption(new Option('--height <int>', 'Viewport height, in pixels. If both height and device are provided, the height value will override device emulation viewport height.')).default( | |
| String(DEFAULT_OPTIONS.height), | |
| ), |
| '--dry', | ||
| 'Dry run (do not run test, just save config and cleanup)', | ||
| ).default(DEFAULT_OPTIONS.dry), | ||
| ) |
There was a problem hiding this comment.
Readd the default option for dry.
| new Option( | ||
| '-d, --device <string>', | ||
| 'Device to emulate; devices are based on the Playwright device list (see https://github.com/microsoft/playwright/blob/main/packages/playwright-core/src/server/deviceDescriptorsSource.json)', | ||
| ) |
There was a problem hiding this comment.
Add default option for device.
| // set width and height from options before assigning device viewport or defaults | ||
| if (options.width) { | ||
| config.width = parseInt(options.width); | ||
| } else if ( | ||
| config.device && | ||
| config.device.viewport && | ||
| config.device.viewport.width | ||
| ) { | ||
| config.width = config.device.viewport.width; | ||
| } else { | ||
| config.width = DEFAULT_OPTIONS.width; | ||
| } |
There was a problem hiding this comment.
Does the config.width need to be set if there is no override for the config.device.viewport.width value?
| // set width and height from options before assigning device viewport or defaults | |
| if (options.width) { | |
| config.width = parseInt(options.width); | |
| } else if ( | |
| config.device && | |
| config.device.viewport && | |
| config.device.viewport.width | |
| ) { | |
| config.width = config.device.viewport.width; | |
| } else { | |
| config.width = DEFAULT_OPTIONS.width; | |
| } | |
| // set width and height from options before assigning device viewport or defaults | |
| if (options.width) { | |
| config.width = parseInt(options.width); | |
| } |
| if (options.height) { | ||
| config.height = parseInt(options.height); | ||
| } else if ( | ||
| config.device && | ||
| config.device.viewport && | ||
| config.device.viewport.height | ||
| ) { | ||
| config.height = config.device.viewport.height; | ||
| } else { | ||
| config.height = DEFAULT_OPTIONS.height; | ||
| } |
There was a problem hiding this comment.
Does the config.height need to be set if there is no override for the config.device.viewport.height value?
| if (options.height) { | |
| config.height = parseInt(options.height); | |
| } else if ( | |
| config.device && | |
| config.device.viewport && | |
| config.device.viewport.height | |
| ) { | |
| config.height = config.device.viewport.height; | |
| } else { | |
| config.height = DEFAULT_OPTIONS.height; | |
| } | |
| if (options.height) { | |
| config.height = parseInt(options.height); | |
| } |
| // Dry run (false = no dry run) | ||
| dry: false, | ||
| // Device to emulate (empty string = no emulation) | ||
| device: '', |
There was a problem hiding this comment.
It would be clearer is device defaulted to null.
Uh oh!
There was an error while loading. Please reload this page.