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: initialize cypress to playwright #181

Merged
merged 6 commits into from
Apr 11, 2024
Merged
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
95 changes: 95 additions & 0 deletions .grit/patterns/js/cypress_to_playwright.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
---
title: Convert Cypress to Playwright
tags: [hidden]
---

Migrate from Cypress to Playwright.

```grit
engine marzano(0.1)
language js

pattern convert_cypress_assertions() {
or {
`expect($arg).to.not.be.null` => `expect($arg).not.toBeNull()`,
`expect($arg).to.not.be.undefined` => `expect($arg).not.toBeUndefined()`,
`$locator.should($condition)` as $should where {
$condition <: bubble or {
`'exist'` => `toBeAttached()`,
`'not.exist'` => `not.toBeAttached()`,
},
$should => `await expect($locator).$condition`,
},
`$locator.should($cond1, $cond2)` as $should where {
$pw_cond = "",
$cond1 <: `'contain'` where {
$pw_cond += `toContainText($cond2)`,
},
$should => `await expect($locator).$pw_cond`,
}
}
}

pattern convert_cypress_queries() {
or {
`cy.visit($loc)` => `await page.goto($loc)`,
`cy.get($locator)` => `page.locator($locator)`,
`cy.log($log)` => `console.log($log)`,
`Cypress.env('$var')` => `process.env.$var`,
`cy.onlyOn($var === $cond)` => `if ($var !== $cond) {
test.skip();
}`,
}
}

pattern convert_cypress_test() {
or {
`describe($description, $suite)` => `test.describe($description, $suite)`,
or {
`it($description, () => { $body })`,
`test($description, () => { $body })`
} => `test($description, async ({ page, request }) => {
$body
})`
}
}

contains bubble or {
convert_cypress_assertions(),
convert_cypress_queries(),
} where {
$program <: contains bubble convert_cypress_test(),
$expect = `expect`,
$expect <: ensure_import_from(source=`"@playwright/test"`),
$test = `test`,
$test <: ensure_import_from(source=`"@playwright/test"`),
}
```

## Converts basic test

```js
describe('A mock test', () => {
test('works', () => {
cy.onlyOn(Cypress.env('ENVIRONMENT') === 'local');
cy.visit('/');
cy.get('.button').should('exist');
cy.get('.button').should('contain', 'Hello world');
});
});
```

```ts
import { expect, test } from '@playwright/test';

test.describe('A mock test', () => {
test('works', async ({ page, request }) => {
if (process.env.ENVIRONMENT !== 'local') {
test.skip();
}
await page.goto('/');
await expect(page.locator('.button')).toBeAttached();
await expect(page.locator('.button')).toContainText('Hello world');
});
});
```
Loading