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: migrate composite cypress queries #188

Merged
merged 6 commits into from
Apr 13, 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
3 changes: 2 additions & 1 deletion .grit/patterns/js/codecept_to_playwright.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
title: Convert CodeceptJS to Playwright
tags: [migration]
---

Migrate from CodeceptJS to Playwright.
Expand All @@ -17,7 +18,7 @@ predicate convert_tags($scenario, $description) {
$tags += `@$fragment`,
},
},

$tags = join($tags, ` `),
$description => trim(`$description $tags`, " "),
}
Expand Down
59 changes: 56 additions & 3 deletions .grit/patterns/js/cypress_to_playwright.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Convert Cypress to Playwright
tags: [hidden]
tags: [migration]
---

Migrate from Cypress to Playwright.
Expand All @@ -13,10 +13,17 @@ 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()`,
`expect($arg).to.include($item)` => `expect($arg).toContain($item)`,
`expect($arg).to.eql($item)` => `expect($arg).toEqual($item)`,
`$locator.should($cond1, $cond2)` as $should where {
$pw_cond = "",
$cond1 <: `'contain'` where {
$pw_cond += `toContainText($cond2)`,
$cond1 <: or {
`'contain'` where {
$pw_cond += `toContainText($cond2)`,
},
`'have.attr'` where {
$pw_cond += `toHaveAttribute($cond2)`,
},
},
$should => `await expect($locator).$pw_cond`,
},
Expand All @@ -35,13 +42,28 @@ pattern convert_cypress_queries() {
`cy.visit($loc)` => `await page.goto($loc)`,
`cy.get($locator)` => `page.locator($locator)`,
`cy.contains($text, $options)` => `await expect(page.getByText($text)).toBeVisible($options)`,
`cy.get($locator).contains($text).$action()` => `await page.locator($locator, { hasText: $text }).$action()`,
`cy.get($locator).contains($text)` => `page.locator($locator, { hasText: $text })`,
`cy.contains($text)` => `await expect(page.getByText($text)).toBeVisible()`,
`cy.log($log)` => `console.log($log)`,
`cy.wait($timeout)` => `await page.waitForTimeout($timeout)`,
`$locator.find($inner)` => `$locator.locator($inner)`,
`$locator.eq($n)` => `$locator.nth($n)`,
`$locator.click($opts)` => `await $locator.click($opts)`,
`$locator.text()` => `await $locator.textContent()`,
`Cypress.env('$var')` => `process.env.$var`,
`cy.onlyOn($var === $cond)` => `if ($var !== $cond) {
test.skip();
}`,
`cy.$_($selector).each(($locator) => {
$body
})` as $loop where {
$var = `$[locator]s`,
$loop => `const $var = await page.locator($selector).all();
for (const $locator of $var) {
$body
}`
},
`cy.request({ $opts })` as $req where {
or {
$opts <: contains pair(key=`method`, value=`"$method"`),
Expand Down Expand Up @@ -177,3 +199,34 @@ test.describe('Grouping', function () {
});
});
```

## Converts composite queries

```js
describe('Grouping', function () {
it('my test', async () => {
cy.get('.header').find('.button').eq(1).click({ force: true });
cy.get('.sidebar').contains('Files').click();
cy.get('.header').find('.button').eq(1).should('have.attr', 'disabled');
cy.get('.button').each((button) => {
expect(button.text()).to.eql('Submit');
});
});
});
```

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

test.describe('Grouping', function () {
test('my test', async ({ page, request }) => {
await page.locator('.header').locator('.button').nth(1).click({ force: true });
await page.locator('.sidebar', { hasText: 'Files' }).click();
await expect(page.locator('.header').locator('.button').nth(1)).toHaveAttribute('disabled');
const buttons = await page.locator('.button').all();
for (const button of buttons) {
expect(await button.textContent()).toEqual('Submit');
}
});
});
```
Loading