|
| 1 | +--- |
| 2 | +title: Convert Cypress to Playwright |
| 3 | +tags: [hidden] |
| 4 | +--- |
| 5 | + |
| 6 | +Migrate from Cypress to Playwright. |
| 7 | + |
| 8 | +```grit |
| 9 | +engine marzano(0.1) |
| 10 | +language js |
| 11 | +
|
| 12 | +pattern convert_cypress_assertions() { |
| 13 | + or { |
| 14 | + `expect($arg).to.not.be.null` => `expect($arg).not.toBeNull()`, |
| 15 | + `expect($arg).to.not.be.undefined` => `expect($arg).not.toBeUndefined()`, |
| 16 | + `$locator.should($condition)` as $should where { |
| 17 | + $condition <: bubble or { |
| 18 | + `'exist'` => `toBeAttached()`, |
| 19 | + `'not.exist'` => `not.toBeAttached()`, |
| 20 | + }, |
| 21 | + $should => `await expect($locator).$condition`, |
| 22 | + }, |
| 23 | + `$locator.should($cond1, $cond2)` as $should where { |
| 24 | + $pw_cond = "", |
| 25 | + $cond1 <: `'contain'` where { |
| 26 | + $pw_cond += `toContainText($cond2)`, |
| 27 | + }, |
| 28 | + $should => `await expect($locator).$pw_cond`, |
| 29 | + } |
| 30 | + } |
| 31 | +} |
| 32 | +
|
| 33 | +pattern convert_cypress_queries() { |
| 34 | + or { |
| 35 | + `cy.visit($loc)` => `await page.goto($loc)`, |
| 36 | + `cy.get($locator)` => `page.locator($locator)`, |
| 37 | + `cy.log($log)` => `console.log($log)`, |
| 38 | + `Cypress.env('$var')` => `process.env.$var`, |
| 39 | + `cy.onlyOn($var === $cond)` => `if ($var !== $cond) { |
| 40 | + test.skip(); |
| 41 | +}`, |
| 42 | + } |
| 43 | +} |
| 44 | +
|
| 45 | +pattern convert_cypress_test() { |
| 46 | + or { |
| 47 | + `describe($description, $suite)` => `test.describe($description, $suite)`, |
| 48 | + or { |
| 49 | + `it($description, () => { $body })`, |
| 50 | + `test($description, () => { $body })` |
| 51 | + } => `test($description, async ({ page, request }) => { |
| 52 | + $body |
| 53 | + })` |
| 54 | + } |
| 55 | +} |
| 56 | +
|
| 57 | +contains bubble or { |
| 58 | + convert_cypress_assertions(), |
| 59 | + convert_cypress_queries(), |
| 60 | +} where { |
| 61 | + $program <: contains bubble convert_cypress_test(), |
| 62 | + $expect = `expect`, |
| 63 | + $expect <: ensure_import_from(source=`"@playwright/test"`), |
| 64 | + $test = `test`, |
| 65 | + $test <: ensure_import_from(source=`"@playwright/test"`), |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +## Converts basic test |
| 70 | + |
| 71 | +```js |
| 72 | +describe('A mock test', () => { |
| 73 | + test('works', () => { |
| 74 | + cy.onlyOn(Cypress.env('ENVIRONMENT') === 'local'); |
| 75 | + cy.visit('/'); |
| 76 | + cy.get('.button').should('exist'); |
| 77 | + cy.get('.button').should('contain', 'Hello world'); |
| 78 | + }); |
| 79 | +}); |
| 80 | +``` |
| 81 | + |
| 82 | +```ts |
| 83 | +import { expect, test } from '@playwright/test'; |
| 84 | + |
| 85 | +test.describe('A mock test', () => { |
| 86 | + test('works', async ({ page, request }) => { |
| 87 | + if (process.env.ENVIRONMENT !== 'local') { |
| 88 | + test.skip(); |
| 89 | + } |
| 90 | + await page.goto('/'); |
| 91 | + await expect(page.locator('.button')).toBeAttached(); |
| 92 | + await expect(page.locator('.button')).toContainText('Hello world'); |
| 93 | + }); |
| 94 | +}); |
| 95 | +``` |
0 commit comments