diff --git a/.grit/patterns/js/codecept_to_playwright.md b/.grit/patterns/js/codecept_to_playwright.md index f79bb727..2f280507 100644 --- a/.grit/patterns/js/codecept_to_playwright.md +++ b/.grit/patterns/js/codecept_to_playwright.md @@ -1,5 +1,6 @@ --- title: Convert CodeceptJS to Playwright +tags: [migration] --- Migrate from CodeceptJS to Playwright. @@ -17,7 +18,7 @@ predicate convert_tags($scenario, $description) { $tags += `@$fragment`, }, }, - + $tags = join($tags, ` `), $description => trim(`$description $tags`, " "), } diff --git a/.grit/patterns/js/cypress_to_playwright.md b/.grit/patterns/js/cypress_to_playwright.md index 765a41a9..ad725552 100644 --- a/.grit/patterns/js/cypress_to_playwright.md +++ b/.grit/patterns/js/cypress_to_playwright.md @@ -1,6 +1,6 @@ --- title: Convert Cypress to Playwright -tags: [hidden] +tags: [migration] --- Migrate from Cypress to Playwright. @@ -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`, }, @@ -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"`), @@ -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'); + } + }); +}); +```