diff --git a/goml-script.md b/goml-script.md index 775fe8a82..847218c26 100644 --- a/goml-script.md +++ b/goml-script.md @@ -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) @@ -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: diff --git a/src/commands.js b/src/commands.js index b9d827e76..035a247d6 100644 --- a/src/commands.js +++ b/src/commands.js @@ -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, diff --git a/src/commands/all.js b/src/commands/all.js index e89bd52a2..73977fe4e 100644 --- a/src/commands/all.js +++ b/src/commands/all.js @@ -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, diff --git a/src/commands/dom_modifiers.js b/src/commands/dom_modifiers.js index e96da9f84..cc3d038ba 100644 --- a/src/commands/dom_modifiers.js +++ b/src/commands/dom_modifiers.js @@ -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") @@ -153,5 +164,6 @@ function parseText(parser) { module.exports = { 'parseAttribute': parseAttribute, 'parseCss': parseCss, + 'parseProperty': parseProperty, 'parseText': parseText, }; diff --git a/tests/test-js/api-output/parseProperty/basic-1.toml b/tests/test-js/api-output/parseProperty/basic-1.toml new file mode 100644 index 000000000..8806ab3f9 --- /dev/null +++ b/tests/test-js/api-output/parseProperty/basic-1.toml @@ -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);""", +] diff --git a/tests/test-js/api-output/parseProperty/basic-2.toml b/tests/test-js/api-output/parseProperty/basic-2.toml new file mode 100644 index 000000000..218e1e795 --- /dev/null +++ b/tests/test-js/api-output/parseProperty/basic-2.toml @@ -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);""", +] diff --git a/tests/test-js/api-output/parseProperty/basic-3.toml b/tests/test-js/api-output/parseProperty/basic-3.toml new file mode 100644 index 000000000..f84d08e35 --- /dev/null +++ b/tests/test-js/api-output/parseProperty/basic-3.toml @@ -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);""", +] diff --git a/tests/test-js/api-output/parseProperty/basic-4.toml b/tests/test-js/api-output/parseProperty/basic-4.toml new file mode 100644 index 000000000..9c5b83fb2 --- /dev/null +++ b/tests/test-js/api-output/parseProperty/basic-4.toml @@ -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);""", +] diff --git a/tests/test-js/api-output/parseProperty/err-1.toml b/tests/test-js/api-output/parseProperty/err-1.toml new file mode 100644 index 000000000..7fa81047f --- /dev/null +++ b/tests/test-js/api-output/parseProperty/err-1.toml @@ -0,0 +1 @@ +error = """expected `\"` at the end of the string""" diff --git a/tests/test-js/api-output/parseProperty/err-10.toml b/tests/test-js/api-output/parseProperty/err-10.toml new file mode 100644 index 000000000..f47633488 --- /dev/null +++ b/tests/test-js/api-output/parseProperty/err-10.toml @@ -0,0 +1,6 @@ +instructions = [ +] +wait = false +warnings = [ + """Ignoring recursive entry with key `\"a\"`""", +] diff --git a/tests/test-js/api-output/parseProperty/err-2.toml b/tests/test-js/api-output/parseProperty/err-2.toml new file mode 100644 index 000000000..f4d16f830 --- /dev/null +++ b/tests/test-js/api-output/parseProperty/err-2.toml @@ -0,0 +1 @@ +error = """expected `)` or `,` after `\"b\"`""" diff --git a/tests/test-js/api-output/parseProperty/err-3.toml b/tests/test-js/api-output/parseProperty/err-3.toml new file mode 100644 index 000000000..79e453215 --- /dev/null +++ b/tests/test-js/api-output/parseProperty/err-3.toml @@ -0,0 +1 @@ +error = """expected `(\"CSS selector\" or \"XPath\", \"property name\", \"property value\")` or `(\"CSS selector\" or \"XPath\", [JSON object])`""" diff --git a/tests/test-js/api-output/parseProperty/err-4.toml b/tests/test-js/api-output/parseProperty/err-4.toml new file mode 100644 index 000000000..79e453215 --- /dev/null +++ b/tests/test-js/api-output/parseProperty/err-4.toml @@ -0,0 +1 @@ +error = """expected `(\"CSS selector\" or \"XPath\", \"property name\", \"property value\")` or `(\"CSS selector\" or \"XPath\", [JSON object])`""" diff --git a/tests/test-js/api-output/parseProperty/err-5.toml b/tests/test-js/api-output/parseProperty/err-5.toml new file mode 100644 index 000000000..4f9133646 --- /dev/null +++ b/tests/test-js/api-output/parseProperty/err-5.toml @@ -0,0 +1 @@ +error = """expected json as second argument (since there are only two arguments), found a string""" diff --git a/tests/test-js/api-output/parseProperty/err-6.toml b/tests/test-js/api-output/parseProperty/err-6.toml new file mode 100644 index 000000000..79e453215 --- /dev/null +++ b/tests/test-js/api-output/parseProperty/err-6.toml @@ -0,0 +1 @@ +error = """expected `(\"CSS selector\" or \"XPath\", \"property name\", \"property value\")` or `(\"CSS selector\" or \"XPath\", [JSON object])`""" diff --git a/tests/test-js/api-output/parseProperty/err-7.toml b/tests/test-js/api-output/parseProperty/err-7.toml new file mode 100644 index 000000000..daeb5fb77 --- /dev/null +++ b/tests/test-js/api-output/parseProperty/err-7.toml @@ -0,0 +1 @@ +error = """expected `,` or `)`, found `\"` after `\"b\"`""" diff --git a/tests/test-js/api-output/parseProperty/err-8.toml b/tests/test-js/api-output/parseProperty/err-8.toml new file mode 100644 index 000000000..4f9133646 --- /dev/null +++ b/tests/test-js/api-output/parseProperty/err-8.toml @@ -0,0 +1 @@ +error = """expected json as second argument (since there are only two arguments), found a string""" diff --git a/tests/test-js/api-output/parseProperty/err-9.toml b/tests/test-js/api-output/parseProperty/err-9.toml new file mode 100644 index 000000000..79e453215 --- /dev/null +++ b/tests/test-js/api-output/parseProperty/err-9.toml @@ -0,0 +1 @@ +error = """expected `(\"CSS selector\" or \"XPath\", \"property name\", \"property value\")` or `(\"CSS selector\" or \"XPath\", [JSON object])`""" diff --git a/tests/test-js/api-output/parseProperty/multiline-1.toml b/tests/test-js/api-output/parseProperty/multiline-1.toml new file mode 100644 index 000000000..4f9133646 --- /dev/null +++ b/tests/test-js/api-output/parseProperty/multiline-1.toml @@ -0,0 +1 @@ +error = """expected json as second argument (since there are only two arguments), found a string""" diff --git a/tests/test-js/api-output/parseProperty/multiline-2.toml b/tests/test-js/api-output/parseProperty/multiline-2.toml new file mode 100644 index 000000000..fd280d260 --- /dev/null +++ b/tests/test-js/api-output/parseProperty/multiline-2.toml @@ -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);""", +] diff --git a/tests/test-js/api-output/parseProperty/multiline-3.toml b/tests/test-js/api-output/parseProperty/multiline-3.toml new file mode 100644 index 000000000..a7843e0fd --- /dev/null +++ b/tests/test-js/api-output/parseProperty/multiline-3.toml @@ -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);""", +] diff --git a/tests/test-js/api-output/parseProperty/xpath-1.toml b/tests/test-js/api-output/parseProperty/xpath-1.toml new file mode 100644 index 000000000..e4a50b83c --- /dev/null +++ b/tests/test-js/api-output/parseProperty/xpath-1.toml @@ -0,0 +1 @@ +error = """XPath must start with `//`""" diff --git a/tests/test-js/api-output/parseProperty/xpath-2.toml b/tests/test-js/api-output/parseProperty/xpath-2.toml new file mode 100644 index 000000000..cd9330dbc --- /dev/null +++ b/tests/test-js/api-output/parseProperty/xpath-2.toml @@ -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);""", +] diff --git a/tests/test-js/api-output/parseProperty/xpath-3.toml b/tests/test-js/api-output/parseProperty/xpath-3.toml new file mode 100644 index 000000000..fd280d260 --- /dev/null +++ b/tests/test-js/api-output/parseProperty/xpath-3.toml @@ -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);""", +] diff --git a/tests/test-js/api.js b/tests/test-js/api.js index 888fbb7ce..2e463d3a9 100644 --- a/tests/test-js/api.js +++ b/tests/test-js/api.js @@ -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'); @@ -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), }, { @@ -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, diff --git a/tests/ui-tests/property.goml b/tests/ui-tests/property.goml index c9cb7a855..a6d3cd6aa 100644 --- a/tests/ui-tests/property.goml +++ b/tests/ui-tests/property.goml @@ -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"})