Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add store-text command #361

Merged
merged 3 commits into from
Sep 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion goml-script.md
Original file line number Diff line number Diff line change
Expand Up @@ -1162,12 +1162,24 @@ For more information about variables, read the [variables section](#variables).

```
store-property: (variable_name, "#button", "clientHeight")
assert-variable: (varible_name, 152)
assert-variable: (variable_name, 152)
store-property: (variable_name, "#button", "scrollHeight")
```

For more information about variables, read the [variables section](#variables).

#### store-text

**store-text** command stores an element's text into a variable. Examples:

```
store-property: (variable_name, "#button")
assert-variable: (variable_name, "click me")
```

For more information about variables, read the [variables section](#variables).


#### store-value

**store-value** command stores a value into a variable. The value can be a number of a string. Examples:
Expand Down
1 change: 1 addition & 0 deletions src/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ const ORDERS = {
'store-css': commands.parseStoreCss,
'store-local-storage': commands.parseStoreLocalStorage,
'store-property': commands.parseStoreProperty,
'store-text': commands.parseStoreText,
'store-value': commands.parseStoreValue,
'text': commands.parseText,
'timeout': commands.parseTimeout,
Expand Down
1 change: 1 addition & 0 deletions src/commands/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ module.exports = {
'parseStoreCss': store.parseStoreCss,
'parseStoreLocalStorage': store.parseStoreLocalStorage,
'parseStoreProperty': store.parseStoreProperty,
'parseStoreText': store.parseStoreText,
'parseStoreValue': store.parseStoreValue,
'parseText': dom_modifiers.parseText,
'parseTimeout': context_setters.parseTimeout,
Expand Down
60 changes: 60 additions & 0 deletions src/commands/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,70 @@ arg.variables["${tuple[0].getText()}"] = await jsHandle.jsonValue();`,
};
}


// Possible inputs:
//
// * (ident, "CSS selector" | "XPath"
function parseStoreText(parser) {
const elems = parser.elems;

if (elems.length === 0) {
return {'error': 'expected a tuple, found nothing'};
} else if (elems.length !== 1 || elems[0].kind !== 'tuple') {
return {
'error': `expected a tuple, found \`${parser.getRawArgs()}\``,
};
}
const tuple = elems[0].getRaw();
if (tuple.length !== 2) {
let err = `expected 2 elements in the tuple, found ${tuple.length} element`;
if (tuple.length > 1) {
err += 's';
}
return {'error': err};
} else if (tuple[0].kind !== 'ident') {
return {
'error': `expected first argument to be an ident, found ${tuple[0].getArticleKind()} \
(\`${tuple[0].getText()}\`)`,
};
} else if (tuple[1].kind !== 'string') {
return {
'error': `expected second argument to be a CSS selector or an XPath, found \
${tuple[1].getArticleKind()} (\`${tuple[1].getText()}\`)`,
};
}

const selector = tuple[1].getSelector();
if (selector.error !== undefined) {
return selector;
}
const warnings = [];
const isPseudo = !selector.isXPath && selector.pseudo !== null;
if (isPseudo) {
warnings.push(`Pseudo-elements (\`${selector.pseudo}\`) don't have attributes so \
the check will be performed on the element itself`);
}

const varName = 'parseStoreText';
const code = `\
${getAndSetElements(selector, varName, false)}
const jsHandle = await ${varName}.evaluateHandle(e => {
return browserUiTestHelpers.getElemText(e, "");
});
arg.variables["${tuple[0].getText()}"] = await jsHandle.jsonValue();`;

return {
'instructions': [code],
'wait': false,
'warnings': warnings.length !== 0 ? warnings : undefined,
};
}

module.exports = {
'parseStoreAttribute': parseStoreAttribute,
'parseStoreCss': parseStoreCss,
'parseStoreLocalStorage': parseStoreLocalStorage,
'parseStoreProperty': parseStoreProperty,
'parseStoreText': parseStoreText,
'parseStoreValue': parseStoreValue,
};
61 changes: 61 additions & 0 deletions tests/test-js/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -6755,6 +6755,62 @@ arg.variables["VAR"] = await jsHandle.jsonValue();`,
});
}

function checkStoreText(x, func) {
x.assert(func(''), {'error': 'expected a tuple, found nothing'});
x.assert(func('hello'), {'error': 'expected a tuple, found `hello`'});
x.assert(func('('), {'error': 'expected `)` at the end'});
x.assert(func('(1)'), {'error': 'expected 2 elements in the tuple, found 1 element'});
x.assert(func('(1, 1)'), {
'error': 'expected first argument to be an ident, found a number (`1`)',
});
x.assert(func('(a, 1)'), {
'error': 'expected second argument to be a CSS selector or an XPath, found a number (`1`)',
});
x.assert(func('(VAR, \'\')'), {
'error': 'CSS selector cannot be empty',
'isXPath': false,
});

x.assert(func('(VAR, "a")'), {
'instructions': [`\
let parseStoreText = await page.$("a");
if (parseStoreText === null) { throw '"a" not found'; }
const jsHandle = await parseStoreText.evaluateHandle(e => {
return browserUiTestHelpers.getElemText(e, "");
});
arg.variables["VAR"] = await jsHandle.jsonValue();`,
],
'wait': false,
});
x.assert(func('(VAR, "//a")'), {
'instructions': [`\
let parseStoreText = await page.$x("//a");
if (parseStoreText.length === 0) { throw 'XPath "//a" not found'; }
parseStoreText = parseStoreText[0];
const jsHandle = await parseStoreText.evaluateHandle(e => {
return browserUiTestHelpers.getElemText(e, "");
});
arg.variables["VAR"] = await jsHandle.jsonValue();`,
],
'wait': false,
});

x.assert(func('(VAR, "a::after")'), {
'instructions': [`\
let parseStoreText = await page.$("a");
if (parseStoreText === null) { throw '"a" not found'; }
const jsHandle = await parseStoreText.evaluateHandle(e => {
return browserUiTestHelpers.getElemText(e, "");
});
arg.variables["VAR"] = await jsHandle.jsonValue();`,
],
'wait': false,
'warnings': [
'Pseudo-elements (`::after`) don\'t have attributes so the check will be performed ' +
'on the element itself'],
});
}

function checkStoreValue(x, func) {
x.assert(func(''), {'error': 'expected a tuple, found nothing'});
x.assert(func('hello'), {'error': 'expected a tuple, found `hello`'});
Expand Down Expand Up @@ -8457,6 +8513,11 @@ const TO_CHECK = [
'func': checkStoreProperty,
'toCall': (e, o) => wrapper(parserFuncs.parseStoreProperty, e, o),
},
{
'name': 'store-text',
'func': checkStoreText,
'toCall': (e, o) => wrapper(parserFuncs.parseStoreText, e, o),
},
{
'name': 'store-value',
'func': checkStoreValue,
Expand Down
7 changes: 7 additions & 0 deletions tests/ui-tests/store.goml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,10 @@ local-storage: {"a key": "a value"}
store-local-storage: (local, "a key")
// Should fail.
assert-variable: (local, "a key")
// Should succeed.
assert-variable: (local, "a value")
store-text: (text, "#another-one")
// Should fail.
assert-variable: (text, "hoy")
// Should succeed.
assert-variable: (text, "hey")
1 change: 1 addition & 0 deletions tests/ui-tests/store.output
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ store... FAILED
[ERROR] (line 13) The following errors happened: [`24` contains `4` (for CONTAINS check), `24` ends with `4` (for ENDS_WITH check)]: for command `assert-variable-false: (height, "4", [CONTAINS, ENDS_WITH])`
[ERROR] (line 25) Error: Evaluation failed: No attribute name `data-whatever`: for command `store-attribute: (attr, "header", "data-whatever")`
[ERROR] (line 31) The following errors happened: [`a value` isn't equal to `a key`]: for command `assert-variable: (local, "a key")`
[ERROR] (line 36) The following errors happened: [`hey` isn't equal to `hoy`]: for command `assert-variable: (text, "hoy")`


<= doc-ui tests done: 0 succeeded, 1 failed