Skip to content

Commit

Permalink
fix: add parsing for visible, attributes, and properties (#419)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrandolf committed Dec 22, 2022
1 parent 67a1ee8 commit 856d6dd
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 7 deletions.
10 changes: 6 additions & 4 deletions src/PuppeteerRunnerExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,23 +457,25 @@ async function waitForElement(
if (result && (properties || attributes)) {
result = await elementsHandle.evaluate(
(elements, properties, attributes) => {
for (const element of elements) {
if (attributes) {
if (attributes) {
for (const element of elements) {
for (const [name, value] of Object.entries(attributes)) {
if (element.getAttribute(name) !== value) {
return false;
}
}
}
if (properties) {
}
if (properties) {
for (const element of elements) {
if (!isDeepMatch(properties, element)) {
return false;
}
}
}
return true;

function isDeepMatch(a: unknown, b: unknown) {
function isDeepMatch<T>(a: T, b: unknown): b is T {
if (a === b) {
return true;
}
Expand Down
31 changes: 31 additions & 0 deletions src/SchemaUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,13 @@ function parseOptionalString(step: object, prop: string): string | undefined {
return undefined;
}

function parseOptionalBoolean(step: object, prop: string): boolean | undefined {
if (hasProperty(step, prop)) {
return parseBoolean(step, prop);
}
return undefined;
}

function parseString(step: object, prop: string): string {
if (hasProperty(step, prop)) {
const maybeString = step[prop];
Expand Down Expand Up @@ -424,11 +431,35 @@ function parseWaitForElementStep(step: object): WaitForElementStep {
"WaitForElement step's operator is not one of '>=','==','<='"
);
}
if (hasProperty(step, 'attributes')) {
if (
!isObject(step.attributes) ||
Object.values(step.attributes).some(
(attribute) => typeof attribute !== 'string'
)
) {
throw new Error(
"WaitForElement step's attribute is not a dictionary of strings"
);
}
}
if (hasProperty(step, 'properties')) {
if (!isObject(step.properties)) {
throw new Error("WaitForElement step's attribute is not an object");
}
}
return {
...parseStepWithSelectors(StepType.WaitForElement, step),
type: StepType.WaitForElement,
operator: operator as '>=' | '==' | '<=' | undefined,
count: parseOptionalNumber(step, 'count'),
visible: parseOptionalBoolean(step, 'visible'),
attributes: hasProperty(step, 'attributes')
? (step.attributes as WaitForElementStep['attributes'])
: undefined,
properties: hasProperty(step, 'properties')
? (step.properties as WaitForElementStep['properties'])
: undefined,
};
}

Expand Down
31 changes: 31 additions & 0 deletions test/SchemaUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,9 @@ describe('SchemaUtils', () => {
selectors: [['aria/Test']],
operator: '==',
count: 1,
properties: {},
attributes: {},
visible: true,
},
],
}),
Expand All @@ -333,6 +336,9 @@ describe('SchemaUtils', () => {
selectors: [['aria/Test']],
operator: '==',
count: 1,
properties: {},
attributes: {},
visible: true,
},
],
}
Expand Down Expand Up @@ -762,6 +768,31 @@ describe('SchemaUtils', () => {
},
error: 'Timeout is not between 1 and 30000 milliseconds',
},
{
input: {
title: 'test',
steps: [
{
type: 'waitForElement',
attributes: { test: 5 },
},
],
},
error:
"WaitForElement step's attribute is not a dictionary of strings",
},
{
input: {
title: 'test',
steps: [
{
type: 'waitForElement',
properties: null,
},
],
},
error: "WaitForElement step's attribute is not an object",
},
];
for (const testCase of testCases) {
assert.throws(() => parse(testCase.input), testCase.error);
Expand Down
6 changes: 3 additions & 3 deletions test/resources/benchmark.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,11 @@
{
"type": "waitForElement",
"target": "main",
"selectors": ["button"],
"count": 2,
"selectors": ["#button"],
"count": 1,
"visible": true,
"properties": {
"value": ""
"id": "button"
},
"attributes": {
"id": "button"
Expand Down

0 comments on commit 856d6dd

Please sign in to comment.