Skip to content
This repository has been archived by the owner on Dec 8, 2023. It is now read-only.

Commit

Permalink
feat: initial codecept scenario conversion (#245)
Browse files Browse the repository at this point in the history
  • Loading branch information
seren5240 authored Nov 22, 2023
1 parent 85bc6c0 commit ce6606c
Showing 1 changed file with 90 additions and 4 deletions.
94 changes: 90 additions & 4 deletions .grit/patterns/codecept_to_playwright.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,26 @@ tags: #hidden
engine marzano(0.1)
language js
pattern convert_test() {
`Scenario($description, async ({ I }) => { $body })` as $scenario where {
$program <: maybe contains call_expression($function) as $tagger where {
$function <: contains $scenario,
$tagger => $scenario,
},
$pages = [],
$body <: maybe contains bubble($pages) r"[a-zA-Z]*Page" as $page where {
$page <: identifier(),
$page_class = capitalize(string=$page),
$pages += `var $page = new $page_class(page, context)`,
},
$pages = distinct(list=$pages),
$pages = join(list=$pages, separator=`;\n`),
$body => `$pages\n$body`,
} => `test($description, async ({ page, factory, context }) => {
$body
})`
}
pattern convert_base_page() {
`export default { $properties }` where {
$program <: contains `const { I } = inject();` => .,
Expand All @@ -18,6 +38,7 @@ pattern convert_base_page() {
$pair => `get $key() { return $value }`
},
`locate($locator).as($_)` => `this.page.locator($locator)`,
`locate($locator)` => `this.page.locator($locator)`,
method_definition($async, $static) as $method where {
$async <: false,
$method => `async $method`,
Expand All @@ -29,10 +50,15 @@ pattern convert_base_page() {
ignoreCase: true,
})`,
`I.see($text, $target)` => `await expect($target).toContainText($text)`,
`I.waitForVisible($target, $timeout)` => `await $target.waitFor({ state: 'visible', timeout: $timeout * 1000 })`,
`I.waitForVisible($target)` => `await $target.waitFor({ state: 'visible' })`,
`I.waitForInvisible($target, $timeout)` => `await $target.waitFor({ state: 'hidden', timeout: $timeout * 1000 })`,
`I.waitForInvisible($target)` => `await $target.waitFor({ state: 'hidden' })`,
`$locator.withText($text)` => `$locator.and(this.page.getByText($text))`,
`I.click($target)` => `await $target.click()`,
},
$filename <: r".*?/?([^/]+)\.[a-zA-Z]*"($base_name),
$base_name = capitalize(string=$base_name),
} => `export default class $base_name extends BasePage {
$properties
}`
Expand All @@ -49,8 +75,11 @@ pattern remove_commas() {
}
sequential {
contains bubble convert_base_page(),
contains bubble remove_commas(),
contains bubble or {
convert_test(),
convert_base_page(),
},
maybe contains bubble remove_commas(),
}
```

Expand All @@ -70,7 +99,7 @@ export default {
```js
// @file test.js

export default class test extends BasePage {
export default class Test extends BasePage {
get url() {
return 'https://grit.io';
}
Expand Down Expand Up @@ -105,7 +134,7 @@ export default {
```js
// @file someFolder/test.js

export default class test extends BasePage {
export default class Test extends BasePage {
get url() {
return 'https://grit.io';
}
Expand All @@ -122,3 +151,60 @@ export default class test extends BasePage {
}
}
```

## Converts complex locators

```js
// @file someFolder/test.js
const { I } = inject();

export default {
studio: locate('.studio'),
message: 'Hello world',

waitForGrit() {
I.waitForVisible(this.studio.withText(this.message), 5);
},
};
```

```js
// @file someFolder/test.js

export default class Test extends BasePage {
get studio() {
return this.page.locator('.studio');
}
get message() {
return 'Hello world';
}

async waitForGrit() {
await this.studio
.and(this.page.getByText(this.message))
.waitFor({ state: 'visible', timeout: 5 * 1000 });
}
}
```

## Converts Codecept scenario

```js
Scenario('Trivial test', async ({ I }) => {
projectPage.open();
expect(true).toBe(true);
projectPage.close();
})
.tag('Email')
.tag('Studio')
.tag('Projects');
```

```js
test('Trivial test', async ({ page, factory, context }) => {
var projectPage = new ProjectPage(page, context);
projectPage.open();
expect(true).toBe(true);
projectPage.close();
});
```

0 comments on commit ce6606c

Please sign in to comment.