Skip to content

Commit

Permalink
feat: add @Preview decorator (#42)
Browse files Browse the repository at this point in the history
* feat: add `@preview` decorator

* chore: generate changeset

* chore: revert changes in examples configuration
  • Loading branch information
SebastianSedzik committed Jan 4, 2024
1 parent a50e25b commit a2e7892
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .changeset/ninety-pants-speak.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Add `@debug` decorator

Runs a `@test`(s) or `@suite`(s) in debug mode.
`@test`(s) or `@suite`(s) lacking the `@debug` decorator will be excluded.
Tests or suites without the `@debug` decorator will not be excluded.
Learn more about debug mode: https://playwright.dev/docs/debug

```ts
Expand Down
28 changes: 28 additions & 0 deletions .changeset/stale-avocados-mix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
'playwright-decorators': minor
---

Add `@preview` decorator

Runs a `@test`(s) or `@suite`(s) in preview (headed browser) mode, simulating user interaction (slowing down each operation by 1000ms).
Tests or suites without the `@preview` decorator will not be excluded.

```ts
import { suite, test, preview, TestArgs } from 'playwright-decorators';

// Preview selected test suite(s)
@preview() // <-- Decorate suite with @preview()
@suite()
class PreviewTestSuite {
}

// Or preview selected test(s)
@suite()
class TestSuite {
@preview() // <-- Decorate test with @preview()
@test()
async test({ page }: TestArgs) {
// ...
}
}
```
33 changes: 31 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ class MyTestSuite {

### Run test(s) or suite(s) in debug mode: `@debug()`
Runs a `@test`(s) or `@suite`(s) in debug mode.
`@test`(s) or `@suite`(s) lacking the `@debug` decorator will be excluded.
Tests or suites without the `@debug` decorator will not be excluded.
Learn more about debug mode: https://playwright.dev/docs/debug

```ts
Expand All @@ -361,7 +361,36 @@ class TestSuite {
```

Then run playwright tests as usual, i.e. `npx playwright test`.
> For debugging purposes, running tests only for one project/browser is recommended.
> For debugging purposes, running tests only on local machine for one project/browser is recommended.

### Run test(s) or suite(s) in preview mode: `@preview()`
Runs a `@test`(s) or `@suite`(s) in preview (headed browser) mode, simulating user interaction (slowing down each operation by 1000ms).
Tests or suites without the `@preview` decorator will not be excluded.

```ts
import { suite, test, preview, TestArgs } from 'playwright-decorators';

// Preview selected test suite(s)
@preview() // <-- Decorate suite with @preview()
@suite()
class PreviewTestSuite {
}

// Or preview selected test(s)
@suite()
class TestSuite {
@preview() // <-- Decorate test with @preview()
@test()
async test({ page }: TestArgs) {
// ...
}
}
```

Then run playwright tests as usual, i.e. `npx playwright test`.
> For preview purposes, running tests only for one project/browser is recommended.

### Custom decorators
Custom decorators can be created using `createTestDecorator` and `createSuiteDecorator` functions.
Expand Down
2 changes: 1 addition & 1 deletion lib/debug.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createSuiteAndTestDecorator } from './custom'

/**
* Runs a @test or @suite in debug mode.
* @test(s) or @suite(s) lacking the @debug decorator will be excluded.
* Tests or suites without the @debug decorator will not be excluded.
* Learn more about debug mode: https://playwright.dev/docs/debug
*/
export const debug = () =>
Expand Down
1 change: 1 addition & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export { tag } from './tag.decorator'

// helpers
export { debug } from './debug.decorator'
export { preview } from './preview.decorator'

// common
export { type TestInfo, type TestArgs } from './common'
Expand Down
33 changes: 33 additions & 0 deletions lib/preview.decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { createSuiteAndTestDecorator } from './custom'
import playwright from '@playwright/test'

const playwrightPreviewPreset = () => {
// disable timeout, as all operations are slowed down by 1000ms
playwright.describe.configure({ timeout: 0 })

playwright.use({
// enable headed mode
headless: false,
launchOptions: {
// slow down every operation by 1000ms
slowMo: 1000
}
})
}

/**
* Runs a @test(s) or @suite(s) in preview (headed browser) mode, simulating user interaction (slowing down each operation by 1000ms).
* Tests or suites without the @preview decorator will not be excluded.
*/
export const preview = () =>
createSuiteAndTestDecorator(
'preview',
({ suite }) => {
playwrightPreviewPreset()
suite.only = true
},
({ test }) => {
playwrightPreviewPreset()
test.only = true
}
)

0 comments on commit a2e7892

Please sign in to comment.