Skip to content
This repository was archived by the owner on Sep 11, 2025. It is now read-only.

Commit cc30ce4

Browse files
authored
Ability to use custom devices (#155)
* Small Readme fix * Remove unnecessary await in return * Pass all device properties to context * Add ability to support custom devices * Update snyk * Remove unnecessary type * Fix types * Add eslint disable * Add some readme about custom devices * Fix types * Approve custom device type
1 parent 33ae117 commit cc30ce4

File tree

6 files changed

+70
-16
lines changed

6 files changed

+70
-16
lines changed

README.md

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ or with `jest.config.js`:
3838

3939
```javascript
4040
module.exports = {
41-
preset: "jest-playwright-preset",
41+
preset: 'jest-playwright-preset',
4242
}
4343
```
4444

@@ -82,7 +82,28 @@ You can specify a `jest-playwright.config.js` at the root of the project or defi
8282
- `chromium` Each test runs Chromium (default).
8383
- `firefox` Each test runs Firefox.
8484
- `webkit` Each test runs Webkit.
85-
- `devices` <[string[]]>. Define a [devices](https://github.com/microsoft/playwright/blob/master/docs/api.md#browsertypedevices) to run tests in. Actual list of devices can be found [here](https://github.com/Microsoft/playwright/blob/master/src/deviceDescriptors.ts).
85+
- `devices` <[string[]]>. Define a [devices](https://github.com/microsoft/playwright/blob/master/docs/api.md#browsertypedevices) to run tests in. Actual list of devices can be found [here](https://github.com/Microsoft/playwright/blob/master/src/deviceDescriptors.ts). Also you can define custom device:
86+
87+
```js
88+
{
89+
// Name of device
90+
name: string
91+
// Page width and height
92+
viewport: {
93+
width: number
94+
height: number
95+
}
96+
// user agent
97+
userAgent: string
98+
// device scale factor
99+
deviceScaleFactor: number
100+
// is device is mobile
101+
isMobile: boolean
102+
// support of touch events
103+
hasTouch: boolean
104+
}
105+
```
106+
86107
- `exitOnPageError` <[boolean]>. Exits process on any page error. Defaults to `true`.
87108
- `collectCoverage` <[boolean]>. Enables the coverage collection of the `saveCoverage(page)` calls to the `.nyc_output/coverage.json` file.
88109
- `serverOptions` <[object]>. [All `jest-dev-server` options](https://github.com/smooth-code/jest-puppeteer/tree/master/packages/jest-dev-server#options).
@@ -104,10 +125,10 @@ module.exports = {
104125

105126
```
106127

107-
**Note**:
128+
### Notes
108129

109-
- You can also specify browser with the `BROWSER` environment variable. You should do it only if you are using the whole playwright package.
110-
- You can specify device with `DEVICE` environment variable.
130+
You can also specify browser with the `BROWSER` environment variable. You should do it only if you are using the whole playwright package.
131+
You can specify device with `DEVICE` environment variable.
111132

112133
## Globals
113134

src/PlaywrightEnvironment.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ const getBrowserPerProcess = async (
4040
}
4141

4242
if (connectOptions) {
43-
return await playwrightInstance.connect(connectOptions)
43+
return playwrightInstance.connect(connectOptions)
4444
} else {
45-
return await playwrightInstance.launch(launchOptions)
45+
return playwrightInstance.launch(launchOptions)
4646
}
4747
}
4848

@@ -86,9 +86,14 @@ export const getPlaywrightEnv = (basicEnv = 'node') => {
8686
}
8787
}
8888

89-
if (device) {
90-
const { viewport, userAgent } = devices[device]
91-
contextOptions = { viewport, userAgent, ...contextOptions }
89+
if (device !== null) {
90+
if (typeof device === 'string') {
91+
contextOptions = { ...devices[device], ...contextOptions }
92+
} else {
93+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
94+
const { name, ...deviceProps } = device
95+
contextOptions = { ...deviceProps, ...contextOptions }
96+
}
9297
}
9398
this.global.browserName = browserType
9499
this.global.deviceName = device

src/PlaywrightRunner.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,11 @@ class PlaywrightRunner extends JestRunner {
8686
const wsEndpoint = this.browser2Server[browser]!.wsEndpoint()
8787

8888
if (devices && devices.length) {
89-
devices.forEach((device) => {
90-
const availableDeviceNames = Object.keys(availableDevices)
91-
checkDeviceEnv(device, availableDeviceNames)
89+
devices.forEach((device: DeviceType) => {
90+
if (typeof device === 'string') {
91+
const availableDeviceNames = Object.keys(availableDevices)
92+
checkDeviceEnv(device, availableDeviceNames)
93+
}
9294
pwTests.push(
9395
getBrowserTest(
9496
test as JestPlaywrightTest,

src/types.d.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type {
55
ChromiumBrowser,
66
FirefoxBrowser,
77
BrowserType as PlaywrightBrowserType,
8+
ViewportSize,
89
devices,
910
} from 'playwright-core'
1011
import type { Config as JestConfig } from '@jest/types'
@@ -15,7 +16,11 @@ import { CHROMIUM, FIREFOX, IMPORT_KIND_PLAYWRIGHT, WEBKIT } from './constants'
1516

1617
export type BrowserType = typeof CHROMIUM | typeof FIREFOX | typeof WEBKIT
1718

18-
export type DeviceType = string | null
19+
export type CustomDeviceType = BrowserContextOptions & {
20+
name: string
21+
}
22+
23+
export type DeviceType = CustomDeviceType | string | null
1924

2025
export type Packages = Partial<Record<BrowserType, BrowserType>>
2126

@@ -42,7 +47,7 @@ export interface JestPlaywrightConfig {
4247
contextOptions?: BrowserContextOptions
4348
exitOnPageError: boolean
4449
browsers: BrowserType[]
45-
devices?: string[]
50+
devices?: (string | CustomDeviceType)[]
4651
serverOptions?: JestProcessManagerOptions
4752
selectors?: SelectorType[]
4853
collectCoverage: boolean

src/utils.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,19 @@ describe('getDisplayName', () => {
107107
'browser: chromium device: iPhone 6',
108108
)
109109
})
110+
111+
it('should return right display name for passed browser and custom device', () => {
112+
const customDevice = {
113+
name: 'Custom device',
114+
viewport: { width: 1920, height: 1080 },
115+
deviceScaleFactor: 1,
116+
isMobile: false,
117+
hasTouch: false,
118+
}
119+
expect(getDisplayName('chromium', customDevice)).toBe(
120+
'browser: chromium device: Custom device',
121+
)
122+
})
110123
})
111124

112125
describe('getBrowserType', () => {

src/utils.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,15 @@ export const getDisplayName = (
4646
browser: BrowserType,
4747
device: DeviceType,
4848
): string => {
49-
return `browser: ${browser}${device ? ` device: ${device}` : ''}`
49+
if (device !== null) {
50+
if (typeof device === 'string') {
51+
return `browser: ${browser} device: ${device}`
52+
}
53+
if (device.name) {
54+
return `browser: ${browser} device: ${device.name}`
55+
}
56+
}
57+
return `browser: ${browser}`
5058
}
5159

5260
export const getDeviceType = (device: DeviceType): DeviceType => {

0 commit comments

Comments
 (0)