Skip to content

Commit

Permalink
Merge pull request #452 from GuillaumeGomez/property
Browse files Browse the repository at this point in the history
Add `property` command
  • Loading branch information
GuillaumeGomez authored Mar 31, 2023
2 parents c5a5a27 + aef56e3 commit c8b1339
Show file tree
Hide file tree
Showing 26 changed files with 124 additions and 2 deletions.
19 changes: 19 additions & 0 deletions goml-script.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ Here's the command list:
* [`pause-on-error`](#pause-on-error)
* [`permissions`](#permissions)
* [`press-key`](#press-key)
* [`property`](#property)
* [`reload`](#reload)
* [`screenshot`](#screenshot)
* [`screenshot-comparison`](#screenshot-comparison)
Expand Down Expand Up @@ -1279,6 +1280,24 @@ press-key: 27 // Same but with an integer
press-key: ('Escape', 1000) // The keyup event will be send after 1000 ms.
```

#### property

**property** command allows to update an element's property. Example:

```
property: ("details", "open", "false")
// Same but with a XPath:
property: ("//details", "attribute-name", "attribute-value")
```

To set multiple properties at a time, you can use a JSON object:

```
property: ("details", {"open": "false", "another": "x"})
// Same but with a XPath:
property: ("//details", {"open": "false", "another": "x"})
```

#### reload

**reload** command reloads the current page. Example:
Expand Down
1 change: 1 addition & 0 deletions src/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const ORDERS = {
'pause-on-error': commands.parsePauseOnError,
'permissions': commands.parsePermissions,
'press-key': commands.parsePressKey,
'property': commands.parseProperty,
'reload': commands.parseReload,
'screenshot': commands.parseScreenshot,
'screenshot-comparison': commands.parseScreenshotComparison,
Expand Down
1 change: 1 addition & 0 deletions src/commands/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ module.exports = {
'parsePauseOnError': context_setters.parsePauseOnError,
'parsePermissions': emulation.parsePermissions,
'parsePressKey': input.parsePressKey,
'parseProperty': dom_modifiers.parseProperty,
'parseReload': navigation.parseReload,
'parseScreenshot': general.parseScreenshot,
'parseScreenshotComparison': context_setters.parseScreenshotComparison,
Expand Down
12 changes: 12 additions & 0 deletions src/commands/dom_modifiers.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,17 @@ function parseAttribute(parser) {
(key, value) => `e.setAttribute("${key}","${value}");`);
}

// Possible inputs:
//
// * ("CSS selector", "property name", "property value")
// * ("XPath", "attribute name", "property value")
// * ("CSS selector", JSON dict)
// * ("XPath", JSON dict)
function parseProperty(parser) {
return innerParseCssAttribute(parser, 'property', 'parsePropertyElem',
(key, value) => `e["${key}"] = "${value}";`);
}

// Possible inputs:
//
// * ("CSS selector", "CSS property name", "CSS property value")
Expand Down Expand Up @@ -153,5 +164,6 @@ function parseText(parser) {
module.exports = {
'parseAttribute': parseAttribute,
'parseCss': parseCss,
'parseProperty': parseProperty,
'parseText': parseText,
};
7 changes: 7 additions & 0 deletions tests/test-js/api-output/parseProperty/basic-1.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
instructions = [
"""let parsePropertyElem = await page.$(\"a\");
if (parsePropertyElem === null) { throw '\"a\" not found'; }
await page.evaluate(e => {
e[\"b\"] = \"c\";
}, parsePropertyElem);""",
]
7 changes: 7 additions & 0 deletions tests/test-js/api-output/parseProperty/basic-2.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
instructions = [
"""let parsePropertyElem = await page.$(\"a\");
if (parsePropertyElem === null) { throw '\"a\" not found'; }
await page.evaluate(e => {
e[\"\\\"b\"] = \"c\";
}, parsePropertyElem);""",
]
7 changes: 7 additions & 0 deletions tests/test-js/api-output/parseProperty/basic-3.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
instructions = [
"""let parsePropertyElemJson = await page.$(\"a\");
if (parsePropertyElemJson === null) { throw '\"a\" not found'; }
await page.evaluate(e => {
e[\"b\"] = \"c\";
}, parsePropertyElemJson);""",
]
10 changes: 10 additions & 0 deletions tests/test-js/api-output/parseProperty/basic-4.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
instructions = [
"""let parsePropertyElemJson = await page.$(\"a\");
if (parsePropertyElemJson === null) { throw '\"a\" not found'; }
await page.evaluate(e => {
e[\"b\"] = \"c\";
}, parsePropertyElemJson);
await page.evaluate(e => {
e[\"d\"] = \"e\";
}, parsePropertyElemJson);""",
]
1 change: 1 addition & 0 deletions tests/test-js/api-output/parseProperty/err-1.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
error = """expected `\"` at the end of the string"""
6 changes: 6 additions & 0 deletions tests/test-js/api-output/parseProperty/err-10.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
instructions = [
]
wait = false
warnings = [
"""Ignoring recursive entry with key `\"a\"`""",
]
1 change: 1 addition & 0 deletions tests/test-js/api-output/parseProperty/err-2.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
error = """expected `)` or `,` after `\"b\"`"""
1 change: 1 addition & 0 deletions tests/test-js/api-output/parseProperty/err-3.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
error = """expected `(\"CSS selector\" or \"XPath\", \"property name\", \"property value\")` or `(\"CSS selector\" or \"XPath\", [JSON object])`"""
1 change: 1 addition & 0 deletions tests/test-js/api-output/parseProperty/err-4.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
error = """expected `(\"CSS selector\" or \"XPath\", \"property name\", \"property value\")` or `(\"CSS selector\" or \"XPath\", [JSON object])`"""
1 change: 1 addition & 0 deletions tests/test-js/api-output/parseProperty/err-5.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
error = """expected json as second argument (since there are only two arguments), found a string"""
1 change: 1 addition & 0 deletions tests/test-js/api-output/parseProperty/err-6.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
error = """expected `(\"CSS selector\" or \"XPath\", \"property name\", \"property value\")` or `(\"CSS selector\" or \"XPath\", [JSON object])`"""
1 change: 1 addition & 0 deletions tests/test-js/api-output/parseProperty/err-7.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
error = """expected `,` or `)`, found `\"` after `\"b\"`"""
1 change: 1 addition & 0 deletions tests/test-js/api-output/parseProperty/err-8.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
error = """expected json as second argument (since there are only two arguments), found a string"""
1 change: 1 addition & 0 deletions tests/test-js/api-output/parseProperty/err-9.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
error = """expected `(\"CSS selector\" or \"XPath\", \"property name\", \"property value\")` or `(\"CSS selector\" or \"XPath\", [JSON object])`"""
1 change: 1 addition & 0 deletions tests/test-js/api-output/parseProperty/multiline-1.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
error = """expected json as second argument (since there are only two arguments), found a string"""
8 changes: 8 additions & 0 deletions tests/test-js/api-output/parseProperty/multiline-2.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
instructions = [
"""let parsePropertyElemJson = await page.$x(\"//a\");
if (parsePropertyElemJson.length === 0) { throw 'XPath \"//a\" not found'; }
parsePropertyElemJson = parsePropertyElemJson[0];
await page.evaluate(e => {
e[\"b\"] = \"c\";
}, parsePropertyElemJson);""",
]
8 changes: 8 additions & 0 deletions tests/test-js/api-output/parseProperty/multiline-3.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
instructions = [
"""let parsePropertyElemJson = await page.$x(\"//a\");
if (parsePropertyElemJson.length === 0) { throw 'XPath \"//a\" not found'; }
parsePropertyElemJson = parsePropertyElemJson[0];
await page.evaluate(e => {
e[\"b\"] = \"c\\n\";
}, parsePropertyElemJson);""",
]
1 change: 1 addition & 0 deletions tests/test-js/api-output/parseProperty/xpath-1.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
error = """XPath must start with `//`"""
8 changes: 8 additions & 0 deletions tests/test-js/api-output/parseProperty/xpath-2.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
instructions = [
"""let parsePropertyElem = await page.$x(\"//a\");
if (parsePropertyElem.length === 0) { throw 'XPath \"//a\" not found'; }
parsePropertyElem = parsePropertyElem[0];
await page.evaluate(e => {
e[\"b\"] = \"c\";
}, parsePropertyElem);""",
]
8 changes: 8 additions & 0 deletions tests/test-js/api-output/parseProperty/xpath-3.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
instructions = [
"""let parsePropertyElemJson = await page.$x(\"//a\");
if (parsePropertyElemJson.length === 0) { throw 'XPath \"//a\" not found'; }
parsePropertyElemJson = parsePropertyElemJson[0];
await page.evaluate(e => {
e[\"b\"] = \"c\";
}, parsePropertyElemJson);""",
]
9 changes: 7 additions & 2 deletions tests/test-js/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ function checkAssertText(x, func) {
func('("//a"\n, \n"b",\n ALL)', 'multiline-2');
}

function checkAttribute(x, func) {
function checkAttributeProperty(x, func) {
func('"', 'err-1');
func('("a", "b"', 'err-2');
func('("a")', 'err-3');
Expand Down Expand Up @@ -1970,7 +1970,7 @@ const TO_CHECK = [
},
{
'name': 'attribute',
'func': checkAttribute,
'func': checkAttributeProperty,
'toCall': (x, e, name, o) => wrapper(parserFuncs.parseAttribute, x, e, name, o),
},
{
Expand Down Expand Up @@ -2178,6 +2178,11 @@ const TO_CHECK = [
'func': checkPressKey,
'toCall': (x, e, name, o) => wrapper(parserFuncs.parsePressKey, x, e, name, o),
},
{
'name': 'property',
'func': checkAttributeProperty,
'toCall': (x, e, name, o) => wrapper(parserFuncs.parseProperty, x, e, name, o),
},
{
'name': 'reload',
'func': checkReload,
Expand Down
4 changes: 4 additions & 0 deletions tests/ui-tests/property.goml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ assert-window-property: {"size": "a"}
assert-document-property-false: {"size": "a"}
document-property: {"size": "a"}
assert-document-property: {"size": "a"}

assert-property-false: ("header", {"yolo": "a"})
property: ("header", {"yolo": "a"})
assert-property: ("header", {"yolo": "a"})

0 comments on commit c8b1339

Please sign in to comment.