Skip to content

Commit

Permalink
Merge pull request #451 from GuillaumeGomez/assert-window-property-null
Browse files Browse the repository at this point in the history
Add `null` support for `assert-document-property*` and `assert-window-property*`
  • Loading branch information
GuillaumeGomez authored Mar 31, 2023
2 parents 9e2e2e0 + f32d1f9 commit c5a5a27
Show file tree
Hide file tree
Showing 71 changed files with 832 additions and 21 deletions.
28 changes: 28 additions & 0 deletions goml-script.md
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,13 @@ assert-document-property: ({"URL": "https://some.where", "title": "a title"})
assert-document-property: {"URL": "https://some.where", "title": "a title"}
```

If you want to check that a property doesn't exist, you can use `null`:

```
// Checking that "property-name" doesn't exist.
assert-document-property: {"property-name": null}
```

You can use more specific checks as well by using one of the following identifiers: "CONTAINS", "ENDS_WITH", "STARTS_WITH", or "NEAR".

```
Expand All @@ -357,6 +364,13 @@ assert-document-property-false: ({"URL": "https://some.where", "title": "a title
assert-document-property-false: {"URL": "https://some.where", "title": "a title"}
```

If you want to check that a property does exist, you can use `null`:

```
// Checking that "property-name" does exist.
assert-document-property-false: {"property-name": null}
```

You can use more specific checks as well by using one of the following identifiers: "CONTAINS", "ENDS_WITH",
"STARTS_WITH" or "NEAR".

Expand Down Expand Up @@ -625,6 +639,13 @@ assert-window-property: ({"pageYOffset": "0", "location": "https://some.where"})
assert-window-property: {"pageYOffset": "0", "location": "https://some.where"}
```

If you want to check that a property doesn't exist, you can use `null`:

```
// Checking that "property-name" doesn't exist.
assert-window-property-false: {"property-name": null}
```

You can use more specific checks as well by using one of the following identifiers: "CONTAINS", "ENDS_WITH", "STARTS_WITH", or "NEAR".

```
Expand Down Expand Up @@ -653,6 +674,13 @@ assert-window-property-false: ({"location": "https://some.where", "pageYOffset":
assert-window-property-false: {"location": "https://some.where", "pageYOffset": "10"}
```

If you want to check that a property does exist, you can use `null`:

```
// Checking that "property-name" does exist.
assert-window-property-false: {"property-name": null}
```

You can use more specific checks as well by using one of the following identifiers: "CONTAINS", "ENDS_WITH", "STARTS_WITH" or "NEAR".

```
Expand Down
35 changes: 32 additions & 3 deletions src/commands/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ function parseAssertObjPropertyInner(parser, assertFalse, objName) {
json_dict = elems[0].getRaw();
}

const entries = validateJson(json_dict, {'string': [], 'number': []}, 'property');
const entries = validateJson(
json_dict, {'string': [], 'number': [], 'ident': ['null']}, 'property');
if (entries.error !== undefined) {
return entries;
}
Expand All @@ -201,8 +202,13 @@ function parseAssertObjPropertyInner(parser, assertFalse, objName) {
const varValue = varName + 'Value';
// JSON.stringify produces a problematic output so instead we use this.
const values = [];
const undefProps = [];
for (const [k, v] of Object.entries(entries.values)) {
values.push(`"${k}":"${v.value}"`);
if (v.kind !== 'ident') {
values.push(`"${k}":"${v.value}"`);
} else {
undefProps.push(`"${k}"`);
}
}

const checks = [];
Expand Down Expand Up @@ -272,6 +278,9 @@ if (Number.isNaN(${objName}[${varKey}])) {
}`);
}
}

// eslint-disable-next-line no-extra-parens
const hasSpecialChecks = (enabled_checks['ALL'] && checks.length > 1) || checks.length !== 0;
// If no check was enabled.
if (checks.length === 0) {
if (assertFalse) {
Expand All @@ -287,18 +296,38 @@ if (String(${objName}[${varKey}]) != ${varValue}) {
}`);
}
}
if (undefProps.length > 0 && hasSpecialChecks) {
const k = Object.entries(enabled_checks)
.filter(([k, v]) => v && k !== 'ALL')
.map(([k, _]) => k);
warnings.push(`Special checks (${k.join(', ')}) will be ignored for \`null\``);
}

let err = '';
let unexpectedPropError = '';
let expectedPropError = '';
if (!assertFalse) {
err = '\n';
err += indentString(`nonMatchingProps.push('Unknown ${objName} property \`' + ${varKey} + \
'\`');`, 3);
unexpectedPropError = `
nonMatchingProps.push("Expected property \`" + prop + "\` to not exist, found: \
\`" + ${objName}[prop] + "\`");`;
} else {
expectedPropError = `
nonMatchingProps.push("Property named \`" + prop + "\` doesn't exist");`;
}

const instructions = [`\
await page.evaluate(() => {
const nonMatchingProps = [];
const ${varDict} = {${values.join(',')}};
const undefProps = [${undefProps.join(',')}];
for (const prop of undefProps) {
if (${objName}[prop] !== undefined) {${unexpectedPropError}
continue;
}${expectedPropError}
}
for (const [${varKey}, ${varValue}] of Object.entries(${varDict})) {
if (${objName}[${varKey}] === undefined) {${err}
continue;
Expand Down Expand Up @@ -531,7 +560,7 @@ the check will be performed on the element itself`);
\`" + e[prop] + "\`");`;
} else {
expectedPropError = `
nonMatchingProps.push("Property named \`" + prop + "\` doesn't exist");`;
nonMatchingProps.push("Property named \`" + prop + "\` doesn't exist");`;
}

const checkAllElements = enabled_checks['ALL'] === true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ instructions = [
"""await page.evaluate(() => {
const nonMatchingProps = [];
const parseAssertDictPropDict = {\"a\":\"b\"};
const undefProps = [];
for (const prop of undefProps) {
if (document[prop] !== undefined) {
nonMatchingProps.push(\"Expected property `\" + prop + \"` to not exist, found: `\" + document[prop] + \"`\");
continue;
}
}
for (const [parseAssertDictPropKey, parseAssertDictPropValue] of Object.entries(parseAssertDictPropDict)) {
if (document[parseAssertDictPropKey] === undefined) {
nonMatchingProps.push('Unknown document property `' + parseAssertDictPropKey + '`');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ instructions = [
"""await page.evaluate(() => {
const nonMatchingProps = [];
const parseAssertDictPropDict = {\"a\":\"b\"};
const undefProps = [];
for (const prop of undefProps) {
if (document[prop] !== undefined) {
nonMatchingProps.push(\"Expected property `\" + prop + \"` to not exist, found: `\" + document[prop] + \"`\");
continue;
}
}
for (const [parseAssertDictPropKey, parseAssertDictPropValue] of Object.entries(parseAssertDictPropDict)) {
if (document[parseAssertDictPropKey] === undefined) {
nonMatchingProps.push('Unknown document property `' + parseAssertDictPropKey + '`');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ instructions = [
"""await page.evaluate(() => {
const nonMatchingProps = [];
const parseAssertDictPropDict = {\"a\":\"b\"};
const undefProps = [];
for (const prop of undefProps) {
if (document[prop] !== undefined) {
nonMatchingProps.push(\"Expected property `\" + prop + \"` to not exist, found: `\" + document[prop] + \"`\");
continue;
}
}
for (const [parseAssertDictPropKey, parseAssertDictPropValue] of Object.entries(parseAssertDictPropDict)) {
if (document[parseAssertDictPropKey] === undefined) {
nonMatchingProps.push('Unknown document property `' + parseAssertDictPropKey + '`');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ instructions = [
"""await page.evaluate(() => {
const nonMatchingProps = [];
const parseAssertDictPropDict = {\"a\":\"b\"};
const undefProps = [];
for (const prop of undefProps) {
if (document[prop] !== undefined) {
nonMatchingProps.push(\"Expected property `\" + prop + \"` to not exist, found: `\" + document[prop] + \"`\");
continue;
}
}
for (const [parseAssertDictPropKey, parseAssertDictPropValue] of Object.entries(parseAssertDictPropDict)) {
if (document[parseAssertDictPropKey] === undefined) {
nonMatchingProps.push('Unknown document property `' + parseAssertDictPropKey + '`');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ instructions = [
"""await page.evaluate(() => {
const nonMatchingProps = [];
const parseAssertDictPropDict = {\"a\":\"b\"};
const undefProps = [];
for (const prop of undefProps) {
if (document[prop] !== undefined) {
nonMatchingProps.push(\"Expected property `\" + prop + \"` to not exist, found: `\" + document[prop] + \"`\");
continue;
}
}
for (const [parseAssertDictPropKey, parseAssertDictPropValue] of Object.entries(parseAssertDictPropDict)) {
if (document[parseAssertDictPropKey] === undefined) {
nonMatchingProps.push('Unknown document property `' + parseAssertDictPropKey + '`');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ instructions = [
"""await page.evaluate(() => {
const nonMatchingProps = [];
const parseAssertDictPropDict = {\"a\":\"b\"};
const undefProps = [];
for (const prop of undefProps) {
if (document[prop] !== undefined) {
nonMatchingProps.push(\"Expected property `\" + prop + \"` to not exist, found: `\" + document[prop] + \"`\");
continue;
}
}
for (const [parseAssertDictPropKey, parseAssertDictPropValue] of Object.entries(parseAssertDictPropDict)) {
if (document[parseAssertDictPropKey] === undefined) {
nonMatchingProps.push('Unknown document property `' + parseAssertDictPropKey + '`');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ instructions = [
"""await page.evaluate(() => {
const nonMatchingProps = [];
const parseAssertDictPropDict = {\"a\":\"b\"};
const undefProps = [];
for (const prop of undefProps) {
if (document[prop] !== undefined) {
nonMatchingProps.push(\"Expected property `\" + prop + \"` to not exist, found: `\" + document[prop] + \"`\");
continue;
}
}
for (const [parseAssertDictPropKey, parseAssertDictPropValue] of Object.entries(parseAssertDictPropDict)) {
if (document[parseAssertDictPropKey] === undefined) {
nonMatchingProps.push('Unknown document property `' + parseAssertDictPropKey + '`');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
error = """Forbidden `ident` used (`undefined`). Allowed idents: [null]"""
28 changes: 28 additions & 0 deletions tests/test-js/api-output/parseAssertDocumentProperty/null-2.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
instructions = [
"""await page.evaluate(() => {
const nonMatchingProps = [];
const parseAssertDictPropDict = {};
const undefProps = [\"a\"];
for (const prop of undefProps) {
if (document[prop] !== undefined) {
nonMatchingProps.push(\"Expected property `\" + prop + \"` to not exist, found: `\" + document[prop] + \"`\");
continue;
}
}
for (const [parseAssertDictPropKey, parseAssertDictPropValue] of Object.entries(parseAssertDictPropDict)) {
if (document[parseAssertDictPropKey] === undefined) {
nonMatchingProps.push('Unknown document property `' + parseAssertDictPropKey + '`');
continue;
}
if (String(document[parseAssertDictPropKey]) != parseAssertDictPropValue) {
nonMatchingProps.push('Expected `' + parseAssertDictPropValue + '` for property `' + parseAssertDictPropKey + '`, found `' + document[parseAssertDictPropKey] + '`');
}
}
if (nonMatchingProps.length !== 0) {
const props = nonMatchingProps.join(\", \");
throw \"The following errors happened: [\" + props + \"]\";
}
});""",
]
wait = false
checkResult = true
31 changes: 31 additions & 0 deletions tests/test-js/api-output/parseAssertDocumentProperty/null-3.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
instructions = [
"""await page.evaluate(() => {
const nonMatchingProps = [];
const parseAssertDictPropDict = {};
const undefProps = [\"a\"];
for (const prop of undefProps) {
if (document[prop] !== undefined) {
nonMatchingProps.push(\"Expected property `\" + prop + \"` to not exist, found: `\" + document[prop] + \"`\");
continue;
}
}
for (const [parseAssertDictPropKey, parseAssertDictPropValue] of Object.entries(parseAssertDictPropDict)) {
if (document[parseAssertDictPropKey] === undefined) {
nonMatchingProps.push('Unknown document property `' + parseAssertDictPropKey + '`');
continue;
}
if (!String(document[parseAssertDictPropKey]).endsWith(parseAssertDictPropValue)) {
nonMatchingProps.push('Property `' + parseAssertDictPropKey + '` (`' + document[parseAssertDictPropKey] + '`) does not end with `' + parseAssertDictPropValue + '`');
}
}
if (nonMatchingProps.length !== 0) {
const props = nonMatchingProps.join(\", \");
throw \"The following errors happened: [\" + props + \"]\";
}
});""",
]
wait = false
warnings = [
"""Special checks (ENDS_WITH) will be ignored for `null`""",
]
checkResult = true
36 changes: 36 additions & 0 deletions tests/test-js/api-output/parseAssertDocumentProperty/null-4.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
instructions = [
"""await page.evaluate(() => {
const nonMatchingProps = [];
const parseAssertDictPropDict = {};
const undefProps = [\"a\"];
for (const prop of undefProps) {
if (document[prop] !== undefined) {
nonMatchingProps.push(\"Expected property `\" + prop + \"` to not exist, found: `\" + document[prop] + \"`\");
continue;
}
}
for (const [parseAssertDictPropKey, parseAssertDictPropValue] of Object.entries(parseAssertDictPropDict)) {
if (document[parseAssertDictPropKey] === undefined) {
nonMatchingProps.push('Unknown document property `' + parseAssertDictPropKey + '`');
continue;
}
if (!String(document[parseAssertDictPropKey]).startsWith(parseAssertDictPropValue)) {
nonMatchingProps.push('Property `' + parseAssertDictPropKey + '` (`' + document[parseAssertDictPropKey] + '`) does not start with `' + parseAssertDictPropValue + '`');
}
if (Number.isNaN(document[parseAssertDictPropKey])) {
nonMatchingProps.push('Property `' + parseAssertDictPropKey + '` (`' + document[parseAssertDictPropKey] + '`) is NaN (for NEAR check)');
} else if (Math.abs(document[parseAssertDictPropKey] - parseAssertDictPropValue) > 1) {
nonMatchingProps.push('Property `' + parseAssertDictPropKey + '` (`' + document[parseAssertDictPropKey] + '`) is not within 1 of `' + parseAssertDictPropValue + '` (for NEAR check)');
}
}
if (nonMatchingProps.length !== 0) {
const props = nonMatchingProps.join(\", \");
throw \"The following errors happened: [\" + props + \"]\";
}
});""",
]
wait = false
warnings = [
"""Special checks (STARTS_WITH, NEAR) will be ignored for `null`""",
]
checkResult = true
36 changes: 36 additions & 0 deletions tests/test-js/api-output/parseAssertDocumentProperty/null-5.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
instructions = [
"""await page.evaluate(() => {
const nonMatchingProps = [];
const parseAssertDictPropDict = {\"b\":\"12\"};
const undefProps = [\"a\"];
for (const prop of undefProps) {
if (document[prop] !== undefined) {
nonMatchingProps.push(\"Expected property `\" + prop + \"` to not exist, found: `\" + document[prop] + \"`\");
continue;
}
}
for (const [parseAssertDictPropKey, parseAssertDictPropValue] of Object.entries(parseAssertDictPropDict)) {
if (document[parseAssertDictPropKey] === undefined) {
nonMatchingProps.push('Unknown document property `' + parseAssertDictPropKey + '`');
continue;
}
if (!String(document[parseAssertDictPropKey]).startsWith(parseAssertDictPropValue)) {
nonMatchingProps.push('Property `' + parseAssertDictPropKey + '` (`' + document[parseAssertDictPropKey] + '`) does not start with `' + parseAssertDictPropValue + '`');
}
if (Number.isNaN(document[parseAssertDictPropKey])) {
nonMatchingProps.push('Property `' + parseAssertDictPropKey + '` (`' + document[parseAssertDictPropKey] + '`) is NaN (for NEAR check)');
} else if (Math.abs(document[parseAssertDictPropKey] - parseAssertDictPropValue) > 1) {
nonMatchingProps.push('Property `' + parseAssertDictPropKey + '` (`' + document[parseAssertDictPropKey] + '`) is not within 1 of `' + parseAssertDictPropValue + '` (for NEAR check)');
}
}
if (nonMatchingProps.length !== 0) {
const props = nonMatchingProps.join(\", \");
throw \"The following errors happened: [\" + props + \"]\";
}
});""",
]
wait = false
warnings = [
"""Special checks (STARTS_WITH, NEAR) will be ignored for `null`""",
]
checkResult = true
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ instructions = [
"""await page.evaluate(() => {
const nonMatchingProps = [];
const parseAssertDictPropDict = {\"a\":\"b\"};
const undefProps = [];
for (const prop of undefProps) {
if (document[prop] !== undefined) {
continue;
}
nonMatchingProps.push(\"Property named `\" + prop + \"` doesn't exist\");
}
for (const [parseAssertDictPropKey, parseAssertDictPropValue] of Object.entries(parseAssertDictPropDict)) {
if (document[parseAssertDictPropKey] === undefined) {
continue;
Expand Down
Loading

0 comments on commit c5a5a27

Please sign in to comment.