Skip to content

Commit 7289128

Browse files
Merge pull request #623 from GuillaumeGomez/set-cmds
Allow to have boolean values in set-property and set-attribute commands
2 parents ad898bc + 3f993d3 commit 7289128

File tree

14 files changed

+92
-14
lines changed

14 files changed

+92
-14
lines changed

goml-script.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1618,12 +1618,12 @@ set-local-storage: {"key": null}
16181618
**set-property** command allows to update an element's property. Example:
16191619

16201620
```
1621-
set-property: ("details", {"open": "false"})
1621+
set-property: ("details", {"open": false})
16221622
// Same but with a XPath:
16231623
set-property: ("//details", {"property-name": "property-value"})
16241624
16251625
// Setting multiple properties at once:
1626-
set-property: ("details", {"open": "false", "another": "x"})
1626+
set-property: ("details", {"open": false, "another": "x"})
16271627
// Same but with a XPath:
16281628
set-property: ("//details", {"open": "false", "another": "x"})
16291629

src/commands/dom_modifiers.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ function innerParseCssAttribute(
2929
jsonValidator.valueTypes.ident = {
3030
allowed: ['null'],
3131
};
32+
jsonValidator.valueTypes.boolean = {};
3233
}
3334
const ret = validator(parser,
3435
{
@@ -55,7 +56,7 @@ function innerParseCssAttribute(
5556
}
5657
const code = [];
5758
for (const [key, value] of attributes) {
58-
code.push(callback(key, value, value.kind === 'ident'));
59+
code.push(callback(key, value, value.kind));
5960
}
6061
const func = allowObjectPath ? `
6162
function setObjValue(object, path, value) {
@@ -92,9 +93,12 @@ ${indentString(code.join('\n'), 1)}
9293
// * ("selector", JSON dict)
9394
function parseSetAttribute(parser) {
9495
return innerParseCssAttribute(parser, 'attribute', 'parseSetAttributeElem', true,
95-
(key, value, isIdent) => {
96-
if (!isIdent) {
97-
return `e.setAttribute("${key}","${value.value}");`;
96+
(key, value, kind) => {
97+
if (kind !== 'ident') {
98+
if (kind !== 'boolean' && kind !== 'number') {
99+
return `e.setAttribute("${key}","${value.value}");`;
100+
}
101+
return `e.setAttribute("${key}",${value.value});`;
98102
}
99103
return `e.removeAttribute("${key}");`;
100104
}, false);
@@ -106,9 +110,11 @@ function parseSetAttribute(parser) {
106110
// * ("selector", JSON dict)
107111
function parseSetProperty(parser) {
108112
return innerParseCssAttribute(parser, 'property', 'parseSetPropertyElem', true,
109-
(key, value, isIdent) => {
113+
(key, value, kind) => {
110114
const k_s = value.key.kind === 'object-path' ? key : `["${key}"]`;
111-
const arg = isIdent ? 'undefined' : `"${value.value}"`;
115+
const arg = kind === 'ident' ? 'undefined' :
116+
// eslint-disable-next-line no-extra-parens
117+
(kind === 'boolean' || kind === 'number' ? `${value.value}` : `"${value.value}"`);
112118
return `setObjValue(e, ${k_s}, ${arg});`;
113119
}, true);
114120
}
@@ -119,7 +125,7 @@ function parseSetProperty(parser) {
119125
// * ("selector", JSON dict)
120126
function parseSetCss(parser) {
121127
return innerParseCssAttribute(parser, 'CSS property', 'parseSetCssElem', false,
122-
(key, value, _isIdent) => `e.style["${key}"] = "${value.value}";`, false);
128+
(key, value, _kind) => `e.style["${key}"] = "${value.value}";`, false);
123129
}
124130

125131
// Possible inputs:

src/commands/utils.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ ${varValue} + '\` (for NEAR check)');
126126
}`;
127127
}
128128
}
129-
// eslint-disable-next-line no-extra-parens
130129
const hasSpecialChecks = checks.length !== 0;
131130
if (checks.length === 0) {
132131
if (assertFalse) {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
instructions = [
2+
"""const parseSetAttributeElem = await page.$(\"a\");
3+
if (parseSetAttributeElem === null) { throw '\"a\" not found'; }
4+
await page.evaluate(e => {
5+
e.setAttribute(\"b\",true);
6+
}, parseSetAttributeElem);""",
7+
]
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
instructions = [
2+
"""const parseSetAttributeElem = await page.$(\"a\");
3+
if (parseSetAttributeElem === null) { throw '\"a\" not found'; }
4+
await page.evaluate(e => {
5+
e.setAttribute(\"b\",12);
6+
}, parseSetAttributeElem);""",
7+
]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +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)"""
1+
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)"""
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +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)"""
1+
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)"""
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
error = """type \"boolean\" (`true`) is not allowed as value in this JSON dict, allowed types are: [`number`, `string`] (second element of the tuple)"""
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
instructions = [
2+
"""const parseSetCssElem = await page.$(\"a\");
3+
if (parseSetCssElem === null) { throw '\"a\" not found'; }
4+
await page.evaluate(e => {
5+
e.style[\"b\"] = \"12\";
6+
}, parseSetCssElem);""",
7+
]
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
instructions = [
2+
"""const parseSetPropertyElem = await page.$(\"a\");
3+
if (parseSetPropertyElem === null) { throw '\"a\" not found'; }
4+
await page.evaluate(e => {
5+
function setObjValue(object, path, value) {
6+
for (let i = 0; i < path.length - 1; ++i) {
7+
const subPath = path[i];
8+
if (object[subPath] === undefined || object[subPath] === null) {
9+
if (value === undefined) {
10+
return;
11+
}
12+
object[subPath] = {};
13+
}
14+
object = object[subPath];
15+
}
16+
if (value === undefined) {
17+
delete object[path[path.length - 1]];
18+
} else {
19+
object[path[path.length - 1]] = value;
20+
}
21+
}
22+
setObjValue(e, [\"b\"], true);
23+
}, parseSetPropertyElem);""",
24+
]

0 commit comments

Comments
 (0)