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

feat: add @preview decorator #42

Merged
merged 3 commits into from
Jan 4, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
)
Loading