Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add parsing for visible, attributes, and properties #419

Merged
merged 1 commit into from
Dec 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: {},
jrandolf-2 marked this conversation as resolved.
Show resolved Hide resolved
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