Skip to content

Commit

Permalink
Pass options to element actions and gotourl (#20)
Browse files Browse the repository at this point in the history
* Pass options to element actions and gotourl

* Bump version
  • Loading branch information
abdala authored Nov 6, 2024
1 parent 505eef0 commit 6afb6d4
Show file tree
Hide file tree
Showing 14 changed files with 170 additions and 137 deletions.
2 changes: 1 addition & 1 deletion assert/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cgauge/assert",
"version": "0.7.1",
"version": "0.8.0",
"description": "Extra assert library",
"type": "module",
"repository": {
Expand Down
8 changes: 4 additions & 4 deletions dtc-aws-plugin/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cgauge/dtc-aws-plugin",
"version": "0.7.1",
"version": "0.8.0",
"description": "AWS plugin for Declarative TestCases",
"repository": {
"type": "git",
Expand All @@ -15,9 +15,9 @@
"author": "Abdala Cerqueira",
"license": "LGPL-3.0-or-later",
"dependencies": {
"@cgauge/assert": "^0.7.0",
"@cgauge/dtc": "^0.7.0",
"@cgauge/nock-aws": "^0.7.0",
"@cgauge/assert": "^0.8.0",
"@cgauge/dtc": "^0.8.0",
"@cgauge/nock-aws": "^0.8.0",
"@aws-sdk/client-dynamodb": "^3.645.0",
"@aws-sdk/client-eventbridge": "^3.645.0",
"@aws-sdk/client-lambda": "^3.645.0",
Expand Down
6 changes: 3 additions & 3 deletions dtc-mysql-plugin/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cgauge/dtc-mysql-plugin",
"version": "0.7.1",
"version": "0.8.0",
"description": "MySQL plugin for Declarative TestCases",
"repository": {
"type": "git",
Expand All @@ -15,8 +15,8 @@
"author": "Abdala Cerqueira",
"license": "LGPL-3.0-or-later",
"dependencies": {
"@cgauge/assert": "^0.7.0",
"@cgauge/dtc": "^0.7.0",
"@cgauge/assert": "^0.8.0",
"@cgauge/dtc": "^0.8.0",
"mysql2": "^3.11.0",
"node-sql-parser": "^5.1.0"
},
Expand Down
6 changes: 3 additions & 3 deletions dtc-playwright-plugin/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cgauge/dtc-playwright-plugin",
"version": "0.7.1",
"version": "0.8.0",
"description": "Playwright plugin for Declarative TestCases",
"repository": {
"type": "git",
Expand All @@ -15,8 +15,8 @@
"author": "Abdala Cerqueira",
"license": "LGPL-3.0-or-later",
"dependencies": {
"@cgauge/assert": "^0.7.0",
"@cgauge/dtc": "^0.7.0",
"@cgauge/assert": "^0.8.0",
"@cgauge/dtc": "^0.8.0",
"@playwright/test": "^1.47.0"
},
"scripts": {
Expand Down
7 changes: 5 additions & 2 deletions dtc-playwright-plugin/src/browser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ for (const {filePath, testCase} of testCaseExecutions) {
if (testCase.use) {
test.use(testCase.use)
}

test(testCase.name, async ({page}) => executeTestCase(testCase, plugins, filePath, {page}))

test(testCase.name, async ({page}) => {
testCase.timeout && test.setTimeout(testCase.timeout)
await executeTestCase(testCase, plugins, filePath, {page})
})
}
62 changes: 32 additions & 30 deletions dtc-playwright-plugin/src/playwright-plugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {isRecord, sleep} from '@cgauge/dtc'
import {isRecord} from '@cgauge/dtc'
import {Page, expect, Locator} from '@playwright/test'

type PlaywrightActionTarget = {
Expand All @@ -17,14 +17,23 @@ type PlaywrightAction = {
fill?: string
click?: boolean
toBeVisible?: boolean
options?: Record<string, unknown>
}

export type Playwright = {
url: string
actions?: PlaywrightAction[]
options?: Record<string, unknown>
}

export type PlaywrightAssert = {
url?: string
actions?: PlaywrightAction[]
options?: Record<string, unknown>
}

const isPlaywright = (v: unknown): v is Playwright => isRecord(v) && 'url' in v
const isPlaywrightAssert = (v: unknown): v is Playwright => isRecord(v) && 'actions' in v

const executeActions = async (actions: PlaywrightAction[], page: Page) => {
for (const act of actions) {
Expand All @@ -36,28 +45,15 @@ const executeActions = async (actions: PlaywrightAction[], page: Page) => {
if (selectorMatch && selectorMatch.length > 0) {
element = page.locator(act.target)
} else {
const targetWithoutSpaces = act.target.replaceAll(/\s/g, '')
element =
(await page.getByPlaceholder(act.target).count()) > 0
? page.getByPlaceholder(act.target)
: (await page.getByText(act.target).count()) > 0
? page.getByText(act.target)
: (await page.getByTitle(act.target).count()) > 0
? page.getByTitle(act.target)
: (await page.getByLabel(act.target).count()) > 0
? page.getByLabel(act.target)
: (await page.getByTestId(targetWithoutSpaces).count()) > 0
? page.getByTestId(targetWithoutSpaces)
: (await page.getByRole(targetWithoutSpaces as 'status').count()) > 0
? page.getByRole(targetWithoutSpaces as 'status')
: page.locator(act.target)
element = page.getByTestId(act.target)
}
} else {
if (typeof page[act.target.name] === 'function') {
element = page[act.target.name].apply(page, act.target.args)
}
}


if (element) {
if (act.action) {
if (typeof act.action === 'string') {
Expand All @@ -69,12 +65,12 @@ const executeActions = async (actions: PlaywrightAction[], page: Page) => {

//@ts-ignore
await element[actionName].apply(element, actionArgs)
} else if (act.fill) {
await element.fill(act.fill)
} else if (act.click) {
await element.click({timeout: 5000})
} else if (act.toBeVisible) {
await expect(element.first()).toBeVisible()
} else if (act.fill !== undefined) {
await element.fill(act.fill, act.options)
} else if (act.click !== undefined) {
await element.click(act.options)
} else if (act.toBeVisible !== undefined) {
await expect(element.first()).toBeVisible({visible: act.toBeVisible, ...act.options})
}
}
}
Expand All @@ -84,16 +80,16 @@ export const arrange = async (args: unknown, _basePath: string, {page}: {page: P
if (!page) {
throw new Error('Page not defined')
}

if (!isRecord(args) || !('playwright' in args)) {
return
}

if (!isPlaywright(args.playwright)) {
return
}

await page.goto(args.playwright.url)
await page.goto(args.playwright.url, args.playwright.options)

if (!args.playwright.actions) {
return
Expand All @@ -111,9 +107,7 @@ export const act = async (args: unknown, _basePath: string, {page}: {page: Page}
return
}

await sleep(300)

await page.goto(args.url)
await page.goto(args.url, args.options)

if (!args.actions) {
return
Expand All @@ -131,9 +125,17 @@ export const assert = async (args: unknown, _basePath: string, {page}: {page: Pa
return
}

if (!args.playwright || !Array.isArray(args.playwright)) {
if (!isPlaywrightAssert(args.playwright)) {
return
}

await executeActions(args.playwright, page)
if (args.playwright.url) {
await page.goto(args.playwright.url, args.playwright.options)
}

if (!args.playwright.actions) {
return
}

await executeActions(args.playwright.actions, page)
}
2 changes: 1 addition & 1 deletion dtc-playwright-plugin/test/fixtures/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<body>
<form action="index.html" method="post">
<label for="username">Username</label>
<input type="text" name="username" id="username" required>
<input type="text" data-testid="username-test-id" name="username" id="username" required>
<br>
<label for="username">Password</label>
<input type="text" placeholder="Your password" name="password" required>
Expand Down
88 changes: 51 additions & 37 deletions dtc-playwright-plugin/test/fixtures/t1.dtc.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,66 @@ const __dirname = dirname(fileURLToPath(import.meta.url))

export default {
name: 'Test case',
timeout: 1000,
arrange: {
url: `file://${__dirname}/login.html`,
actions: [
{
target: 'Username',
fill: '',
},
{
target: {
name: 'getByPlaceholder',
args: ['Your password'],
playwright: {
url: `file://${__dirname}/login.html`,
actions: [
{
target: 'username-test-id',
fill: '',
options: {timeout: 1000},
},
action: {
name: 'fill',
args: [''],
{
target: {
name: 'getByPlaceholder',
args: ['Your password'],
},
action: {
name: 'fill',
args: ['', {timeout: 1000}],
},
},
},
{
target: {
name: 'getByRole',
args: [
'button',
{
type: 'submit',
},
],
{
target: {
name: 'getByRole',
args: [
'button',
{
type: 'submit',
},
],
},
action: {
name: 'click',
},
},
action: {
name: 'click',
{
target: 'input#username[required]:invalid',
toBeVisible: true,
},
},
{
target: "input#username[required]:invalid",
toBeVisible: true,
},
],
],
},
},
act: {
url: `file://${__dirname}/index.html`,
options: {timeout: 1000},
},
assert: {
playwright: [
{
target: "Index page",
toBeVisible: true,
},
],
playwright: {
actions: [
{
target: {
name: 'getByText',
args: ['Index page'],
},
toBeVisible: true,
},
{
target: 'invalid-id',
toBeVisible: false,
},
],
},
},
}
Loading

0 comments on commit 6afb6d4

Please sign in to comment.