11---
22title : Convert Cypress to Playwright
3- tags : [hidden ]
3+ tags : [migration ]
44---
55
66Migrate from Cypress to Playwright.
@@ -13,10 +13,17 @@ pattern convert_cypress_assertions() {
1313 or {
1414 `expect($arg).to.not.be.null` => `expect($arg).not.toBeNull()`,
1515 `expect($arg).to.not.be.undefined` => `expect($arg).not.toBeUndefined()`,
16+ `expect($arg).to.include($item)` => `expect($arg).toContain($item)`,
17+ `expect($arg).to.eql($item)` => `expect($arg).toEqual($item)`,
1618 `$locator.should($cond1, $cond2)` as $should where {
1719 $pw_cond = "",
18- $cond1 <: `'contain'` where {
19- $pw_cond += `toContainText($cond2)`,
20+ $cond1 <: or {
21+ `'contain'` where {
22+ $pw_cond += `toContainText($cond2)`,
23+ },
24+ `'have.attr'` where {
25+ $pw_cond += `toHaveAttribute($cond2)`,
26+ },
2027 },
2128 $should => `await expect($locator).$pw_cond`,
2229 },
@@ -35,13 +42,28 @@ pattern convert_cypress_queries() {
3542 `cy.visit($loc)` => `await page.goto($loc)`,
3643 `cy.get($locator)` => `page.locator($locator)`,
3744 `cy.contains($text, $options)` => `await expect(page.getByText($text)).toBeVisible($options)`,
45+ `cy.get($locator).contains($text).$action()` => `await page.locator($locator, { hasText: $text }).$action()`,
46+ `cy.get($locator).contains($text)` => `page.locator($locator, { hasText: $text })`,
3847 `cy.contains($text)` => `await expect(page.getByText($text)).toBeVisible()`,
3948 `cy.log($log)` => `console.log($log)`,
4049 `cy.wait($timeout)` => `await page.waitForTimeout($timeout)`,
50+ `$locator.find($inner)` => `$locator.locator($inner)`,
51+ `$locator.eq($n)` => `$locator.nth($n)`,
52+ `$locator.click($opts)` => `await $locator.click($opts)`,
53+ `$locator.text()` => `await $locator.textContent()`,
4154 `Cypress.env('$var')` => `process.env.$var`,
4255 `cy.onlyOn($var === $cond)` => `if ($var !== $cond) {
4356 test.skip();
4457}`,
58+ `cy.$_($selector).each(($locator) => {
59+ $body
60+ })` as $loop where {
61+ $var = `$[locator]s`,
62+ $loop => `const $var = await page.locator($selector).all();
63+ for (const $locator of $var) {
64+ $body
65+ }`
66+ },
4567 `cy.request({ $opts })` as $req where {
4668 or {
4769 $opts <: contains pair(key=`method`, value=`"$method"`),
@@ -177,3 +199,34 @@ test.describe('Grouping', function () {
177199 });
178200});
179201```
202+
203+ ## Converts composite queries
204+
205+ ``` js
206+ describe (' Grouping' , function () {
207+ it (' my test' , async () => {
208+ cy .get (' .header' ).find (' .button' ).eq (1 ).click ({ force: true });
209+ cy .get (' .sidebar' ).contains (' Files' ).click ();
210+ cy .get (' .header' ).find (' .button' ).eq (1 ).should (' have.attr' , ' disabled' );
211+ cy .get (' .button' ).each ((button ) => {
212+ expect (button .text ()).to .eql (' Submit' );
213+ });
214+ });
215+ });
216+ ```
217+
218+ ``` ts
219+ import { expect , test } from ' @playwright/test' ;
220+
221+ test .describe (' Grouping' , function () {
222+ test (' my test' , async ({ page , request }) => {
223+ await page .locator (' .header' ).locator (' .button' ).nth (1 ).click ({ force: true });
224+ await page .locator (' .sidebar' , { hasText: ' Files' }).click ();
225+ await expect (page .locator (' .header' ).locator (' .button' ).nth (1 )).toHaveAttribute (' disabled' );
226+ const buttons = await page .locator (' .button' ).all ();
227+ for (const button of buttons ) {
228+ expect (await button .textContent ()).toEqual (' Submit' );
229+ }
230+ });
231+ });
232+ ```
0 commit comments