Skip to content

Commit

Permalink
Merge pull request #623 from GuillaumeGomez/set-cmds
Browse files Browse the repository at this point in the history
Allow to have boolean values in set-property and set-attribute commands
  • Loading branch information
GuillaumeGomez authored Oct 25, 2024
2 parents ad898bc + 3f993d3 commit 7289128
Show file tree
Hide file tree
Showing 14 changed files with 92 additions and 14 deletions.
4 changes: 2 additions & 2 deletions goml-script.md
Original file line number Diff line number Diff line change
Expand Up @@ -1618,12 +1618,12 @@ set-local-storage: {"key": null}
**set-property** command allows to update an element's property. Example:

```
set-property: ("details", {"open": "false"})
set-property: ("details", {"open": false})
// Same but with a XPath:
set-property: ("//details", {"property-name": "property-value"})
// Setting multiple properties at once:
set-property: ("details", {"open": "false", "another": "x"})
set-property: ("details", {"open": false, "another": "x"})
// Same but with a XPath:
set-property: ("//details", {"open": "false", "another": "x"})
Expand Down
20 changes: 13 additions & 7 deletions src/commands/dom_modifiers.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ function innerParseCssAttribute(
jsonValidator.valueTypes.ident = {
allowed: ['null'],
};
jsonValidator.valueTypes.boolean = {};
}
const ret = validator(parser,
{
Expand All @@ -55,7 +56,7 @@ function innerParseCssAttribute(
}
const code = [];
for (const [key, value] of attributes) {
code.push(callback(key, value, value.kind === 'ident'));
code.push(callback(key, value, value.kind));
}
const func = allowObjectPath ? `
function setObjValue(object, path, value) {
Expand Down Expand Up @@ -92,9 +93,12 @@ ${indentString(code.join('\n'), 1)}
// * ("selector", JSON dict)
function parseSetAttribute(parser) {
return innerParseCssAttribute(parser, 'attribute', 'parseSetAttributeElem', true,
(key, value, isIdent) => {
if (!isIdent) {
return `e.setAttribute("${key}","${value.value}");`;
(key, value, kind) => {
if (kind !== 'ident') {
if (kind !== 'boolean' && kind !== 'number') {
return `e.setAttribute("${key}","${value.value}");`;
}
return `e.setAttribute("${key}",${value.value});`;
}
return `e.removeAttribute("${key}");`;
}, false);
Expand All @@ -106,9 +110,11 @@ function parseSetAttribute(parser) {
// * ("selector", JSON dict)
function parseSetProperty(parser) {
return innerParseCssAttribute(parser, 'property', 'parseSetPropertyElem', true,
(key, value, isIdent) => {
(key, value, kind) => {
const k_s = value.key.kind === 'object-path' ? key : `["${key}"]`;
const arg = isIdent ? 'undefined' : `"${value.value}"`;
const arg = kind === 'ident' ? 'undefined' :
// eslint-disable-next-line no-extra-parens
(kind === 'boolean' || kind === 'number' ? `${value.value}` : `"${value.value}"`);
return `setObjValue(e, ${k_s}, ${arg});`;
}, true);
}
Expand All @@ -119,7 +125,7 @@ function parseSetProperty(parser) {
// * ("selector", JSON dict)
function parseSetCss(parser) {
return innerParseCssAttribute(parser, 'CSS property', 'parseSetCssElem', false,
(key, value, _isIdent) => `e.style["${key}"] = "${value.value}";`, false);
(key, value, _kind) => `e.style["${key}"] = "${value.value}";`, false);
}

// Possible inputs:
Expand Down
1 change: 0 additions & 1 deletion src/commands/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ ${varValue} + '\` (for NEAR check)');
}`;
}
}
// eslint-disable-next-line no-extra-parens
const hasSpecialChecks = checks.length !== 0;
if (checks.length === 0) {
if (assertFalse) {
Expand Down
7 changes: 7 additions & 0 deletions tests/api-output/parseSetAttribute/basic-7.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
instructions = [
"""const parseSetAttributeElem = await page.$(\"a\");
if (parseSetAttributeElem === null) { throw '\"a\" not found'; }
await page.evaluate(e => {
e.setAttribute(\"b\",true);
}, parseSetAttributeElem);""",
]
7 changes: 7 additions & 0 deletions tests/api-output/parseSetAttribute/basic-8.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
instructions = [
"""const parseSetAttributeElem = await page.$(\"a\");
if (parseSetAttributeElem === null) { throw '\"a\" not found'; }
await page.evaluate(e => {
e.setAttribute(\"b\",12);
}, parseSetAttributeElem);""",
]
2 changes: 1 addition & 1 deletion tests/api-output/parseSetAttribute/err-10.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
error = """type \"json\" (`{\"a\": \"x\"}`) is not allowed as value in this JSON dict, allowed types are: [`ident`, `number`, `string`] (second element of the tuple)"""
error = """type \"json\" (`{\"a\": \"x\"}`) is not allowed as value in this JSON dict, allowed types are: [`boolean`, `ident`, `number`, `string`] (second element of the tuple)"""
2 changes: 1 addition & 1 deletion tests/api-output/parseSetAttribute/err-11.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
error = """type \"array\" (`[\"a\"]`) is not allowed as value in this JSON dict, allowed types are: [`ident`, `number`, `string`] (second element of the tuple)"""
error = """type \"array\" (`[\"a\"]`) is not allowed as value in this JSON dict, allowed types are: [`boolean`, `ident`, `number`, `string`] (second element of the tuple)"""
1 change: 1 addition & 0 deletions tests/api-output/parseSetCss/basic-7.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
error = """type \"boolean\" (`true`) is not allowed as value in this JSON dict, allowed types are: [`number`, `string`] (second element of the tuple)"""
7 changes: 7 additions & 0 deletions tests/api-output/parseSetCss/basic-8.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
instructions = [
"""const parseSetCssElem = await page.$(\"a\");
if (parseSetCssElem === null) { throw '\"a\" not found'; }
await page.evaluate(e => {
e.style[\"b\"] = \"12\";
}, parseSetCssElem);""",
]
24 changes: 24 additions & 0 deletions tests/api-output/parseSetProperty/basic-7.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
instructions = [
"""const parseSetPropertyElem = await page.$(\"a\");
if (parseSetPropertyElem === null) { throw '\"a\" not found'; }
await page.evaluate(e => {
function setObjValue(object, path, value) {
for (let i = 0; i < path.length - 1; ++i) {
const subPath = path[i];
if (object[subPath] === undefined || object[subPath] === null) {
if (value === undefined) {
return;
}
object[subPath] = {};
}
object = object[subPath];
}
if (value === undefined) {
delete object[path[path.length - 1]];
} else {
object[path[path.length - 1]] = value;
}
}
setObjValue(e, [\"b\"], true);
}, parseSetPropertyElem);""",
]
24 changes: 24 additions & 0 deletions tests/api-output/parseSetProperty/basic-8.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
instructions = [
"""const parseSetPropertyElem = await page.$(\"a\");
if (parseSetPropertyElem === null) { throw '\"a\" not found'; }
await page.evaluate(e => {
function setObjValue(object, path, value) {
for (let i = 0; i < path.length - 1; ++i) {
const subPath = path[i];
if (object[subPath] === undefined || object[subPath] === null) {
if (value === undefined) {
return;
}
object[subPath] = {};
}
object = object[subPath];
}
if (value === undefined) {
delete object[path[path.length - 1]];
} else {
object[path[path.length - 1]] = value;
}
}
setObjValue(e, [\"b\"], 12);
}, parseSetPropertyElem);""",
]
2 changes: 1 addition & 1 deletion tests/api-output/parseSetProperty/err-10.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
error = """type \"json\" (`{\"a\": \"x\"}`) is not allowed as value in this JSON dict, allowed types are: [`ident`, `number`, `string`] (second element of the tuple)"""
error = """type \"json\" (`{\"a\": \"x\"}`) is not allowed as value in this JSON dict, allowed types are: [`boolean`, `ident`, `number`, `string`] (second element of the tuple)"""
2 changes: 1 addition & 1 deletion tests/api-output/parseSetProperty/err-11.toml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
error = """type \"array\" (`[\"a\"]`) is not allowed as value in this JSON dict, allowed types are: [`ident`, `number`, `string`] (second element of the tuple)"""
error = """type \"array\" (`[\"a\"]`) is not allowed as value in this JSON dict, allowed types are: [`boolean`, `ident`, `number`, `string`] (second element of the tuple)"""
3 changes: 3 additions & 0 deletions tools/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,9 @@ function checkAttributeProperty(x, func) {
func('("a", {"b": "c", "d": "e"})', 'basic-4');
func('("a", {"b": null})', 'basic-5');
func('("a", {"b": null, "a": "b"})', 'basic-6');
// This one will fail for `set-css`.
func('("a", {"b": true})', 'basic-7');
func('("a", {"b": 12})', 'basic-8');

// XPath
func('("/a", "b", "c")', 'xpath-1');
Expand Down

0 comments on commit 7289128

Please sign in to comment.