Skip to content

Commit

Permalink
Add ability to use RegExp pattern to define devices (#195)
Browse files Browse the repository at this point in the history
* Add ability to use Regexp pattern to define devices

* Add info about devices to Readme
  • Loading branch information
mmarkelov committed Jul 7, 2020
1 parent 82e9b19 commit 0e7228d
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 11 deletions.
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

0 comments on commit 0e7228d

Please sign in to comment.