Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to use RegExp pattern to define devices #195

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 31 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,36 @@ You can specify a `jest-playwright.config.js` at the root of the project or defi
- `chromium` Each test runs Chromium (default).
- `firefox` Each test runs Firefox.
- `webkit` Each test runs Webkit.
- `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:
- `devices` <[(string | object)[] | RegExp]>. 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).

- `exitOnPageError` <[boolean]>. Exits process on any page error. Defaults to `true`.
- `collectCoverage` <[boolean]>. Enables the coverage collection of the `saveCoverage(page)` calls to the `.nyc_output/coverage.json` file.
- `serverOptions` <[object]>. [All `jest-dev-server` options](https://github.com/smooth-code/jest-puppeteer/tree/master/packages/jest-dev-server#options).
- `selectors` <[array]>. Define [selectors](https://github.com/microsoft/playwright/blob/v0.11.1/docs/api.md#class-selectors). Each selector must be an object with name and script properties.

### Device configuration

There are different ways to define browsers in your tests:

- You can you array of device names:

```js
module.exports = {
devices: ["iPhone 6", "Pixel 2"],
...
}
```

- You can use **RegExp**:

```js
module.exports = {
devices: /iPhone 8/,
...
}
```

- Also you can define custom device:

```js
{
Expand All @@ -104,12 +133,7 @@ You can specify a `jest-playwright.config.js` at the root of the project or defi
}
```

- `exitOnPageError` <[boolean]>. Exits process on any page error. Defaults to `true`.
- `collectCoverage` <[boolean]>. Enables the coverage collection of the `saveCoverage(page)` calls to the `.nyc_output/coverage.json` file.
- `serverOptions` <[object]>. [All `jest-dev-server` options](https://github.com/smooth-code/jest-puppeteer/tree/master/packages/jest-dev-server#options).
- `selectors` <[array]>. Define [selectors](https://github.com/microsoft/playwright/blob/v0.11.1/docs/api.md#class-selectors). Each selector must be an object with name and script properties.

Usage with [query-selector-shadow-dom](https://github.com/Georgegriff/query-selector-shadow-dom) in `jest-playwright.config.js`:
### Usage with [query-selector-shadow-dom](https://github.com/Georgegriff/query-selector-shadow-dom) in `jest-playwright.config.js`:

```javascript
const {
Expand Down
16 changes: 14 additions & 2 deletions src/PlaywrightRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {
import type { Config as JestConfig } from '@jest/types'
import type {
BrowserType,
CustomDeviceType,
DeviceType,
WsEndpointType,
JestPlaywrightTest,
Expand Down Expand Up @@ -73,6 +74,7 @@ class PlaywrightRunner extends JestRunner {

async getTests(tests: Test[], config: JestPlaywrightConfig): Promise<Test[]> {
const { browsers, devices, launchType, launchOptions } = config
let resultDevices: (string | CustomDeviceType)[] = []
const pwTests: Test[] = []
for (const test of tests) {
for (const browser of browsers) {
Expand All @@ -89,8 +91,18 @@ class PlaywrightRunner extends JestRunner {
wsEndpoint = this.browser2Server[browser]!.wsEndpoint()
}

if (devices && devices.length) {
devices.forEach((device: DeviceType) => {
if (devices instanceof RegExp) {
resultDevices = Object.keys(availableDevices).filter((item) =>
item.match(devices),
)
} else {
if (devices) {
resultDevices = devices
}
}

if (resultDevices.length) {
resultDevices.forEach((device: DeviceType) => {
if (typeof device === 'string') {
const availableDeviceNames = Object.keys(availableDevices)
checkDeviceEnv(device, availableDeviceNames)
Expand Down
4 changes: 2 additions & 2 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export type BrowserType = typeof CHROMIUM | typeof FIREFOX | typeof WEBKIT

export type SkipOption = {
browser: BrowserType
device?: string
device?: string | RegExp
}

export type CustomDeviceType = BrowserContextOptions & {
Expand Down Expand Up @@ -69,7 +69,7 @@ export interface JestPlaywrightConfig {
userDataDir?: string
exitOnPageError: boolean
browsers: BrowserType[]
devices?: (string | CustomDeviceType)[]
devices?: (string | CustomDeviceType)[] | RegExp
serverOptions?: JestProcessManagerOptions
selectors?: SelectorType[]
collectCoverage: boolean
Expand Down
6 changes: 6 additions & 0 deletions src/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,12 @@ describe('getSkipFlag', () => {
})

it('should return true if skipOption.browser = browserName & skipOption.device = deviceName', async () => {
const skipOptions = { browser: CHROMIUM as BrowserType, device: /Pixel/ }
const skipFlag = getSkipFlag(skipOptions, CHROMIUM, 'Pixel 2')
expect(skipFlag).toBe(true)
})

it('should return true if skipOption.device is RegExp', async () => {
const skipOptions = { browser: CHROMIUM as BrowserType, device: 'Pixel 2' }
const skipFlag = getSkipFlag(skipOptions, CHROMIUM, 'Pixel 2')
expect(skipFlag).toBe(true)
Expand Down
3 changes: 3 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ export const getSkipFlag = (
if (!device) {
return browser === browserName
} else {
if (device instanceof RegExp) {
return browser === browserName && device.test(deviceName!)
}
return browser === browserName && device === deviceName
}
}
Expand Down