Skip to content
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
43 changes: 22 additions & 21 deletions src/commands/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,19 +262,19 @@ if (!String(${objName}[${varKey}]).endsWith(${varValue})) {
checks.push(`\
if (Number.isNaN(${objName}[${varKey}])) {
nonMatchingProps.push('Property \`' + ${varKey} + '\` (\`' + ${objName}[${varKey}] + '\
\`) is NaN');
\`) is NaN (for NEAR check)');
} else if (Math.abs(${objName}[${varKey}] - ${varValue}) <= 1) {
nonMatchingProps.push('Property \`' + ${varKey} + '\` (\`' + ${objName}[${varKey}] + '\
\`) is within 1 of \`' + ${varValue} + '\`');
\`) is within 1 of \`' + ${varValue} + '\` (for NEAR check)');
}`);
} else {
checks.push(`\
if (Number.isNaN(${objName}[${varKey}])) {
nonMatchingProps.push('Property \`' + ${varKey} + '\` (\`' + ${objName}[${varKey}] + '\
\`) is NaN');
\`) is NaN (for NEAR check)');
} else if (Math.abs(${objName}[${varKey}] - ${varValue}) > 1) {
nonMatchingProps.push('Property \`' + ${varKey} + '\` (\`' + ${objName}[${varKey}] + '\
\`) is not within 1 of \`' + ${varValue} + '\`');
\`) is not within 1 of \`' + ${varValue} + '\` (for NEAR check)');
}`);
}
}
Expand Down Expand Up @@ -461,19 +461,19 @@ if (!String(e[${varKey}]).endsWith(${varValue})) {
checks.push(`\
if (Number.isNaN(e[${varKey}])) {
nonMatchingProps.push('Property \`' + ${varKey} + '\` (\`' + e[${varKey}] + '\
\`) is NaN');
\`) is NaN (for NEAR check)');
} else if (Math.abs(e[${varKey}] - ${varValue}) <= 1) {
nonMatchingProps.push('Property \`' + ${varKey} + '\` (\`' + e[${varKey}] + '\
\`) is within 1 of \`' + ${varValue} + '\`');
\`) is within 1 of \`' + ${varValue} + '\` (for NEAR check)');
}`);
} else {
checks.push(`\
if (Number.isNaN(e[${varKey}])) {
nonMatchingProps.push('Property \`' + ${varKey} + '\` (\`' + e[${varKey}] + '\
\`) is NaN');
\`) is NaN (for NEAR check)');
} else if (Math.abs(e[${varKey}] - ${varValue}) > 1) {
nonMatchingProps.push('Property \`' + ${varKey} + '\` (\`' + e[${varKey}] + '\
\`) is not within 1 of \`' + ${varValue} + '\`');
\`) is not within 1 of \`' + ${varValue} + '\` (for NEAR check)');
}`);
}
}
Expand Down Expand Up @@ -679,19 +679,19 @@ if (!attr.endsWith(${varValue})) {
checks.push(`\
if (Number.isNaN(attr)) {
nonMatchingProps.push('attribute \`' + ${varKey} + '\` (\`' + attr + '\
\`) is NaN');
} else if (Math.abs(attr] - ${varValue}) <= 1) {
\`) is NaN (for NEAR check)');
} else if (Math.abs(attr - ${varValue}) <= 1) {
nonMatchingProps.push('attribute \`' + ${varKey} + '\` (\`' + attr + '\
\`) is within 1 of \`' + ${varValue} '\`');
\`) is within 1 of \`' + ${varValue} + '\` (for NEAR check)');
}`);
} else {
checks.push(`\
if (Number.isNaN(attr)) {
nonMatchingProps.push('Property \`' + ${varKey} + '\` (\`' + attr + '\
\`) is NaN');
nonMatchingProps.push('attribute \`' + ${varKey} + '\` (\`' + attr + '\
\`) is NaN (for NEAR check)');
} else if (Math.abs(attr - ${varValue}) > 1) {
nonMatchingProps.push('Property \`' + ${varKey} + '\` (\`' + attr + '\
\`) is not within 1 of \`' + ${varValue} '\`');
nonMatchingProps.push('attribute \`' + ${varKey} + '\` (\`' + attr + '\
\`) is not within 1 of \`' + ${varValue} + '\` (for NEAR check)');
}`);
}
}
Expand Down Expand Up @@ -1378,17 +1378,18 @@ if (!value1.endsWith(value2)) {
if (enabled_checks['NEAR']) {
if (assertFalse) {
checks.push(`\
if (Number.isNaN(value1])) {
nonMatchingProps.push('\`' + value1 + '\` is NaN');
if (Number.isNaN(value1)) {
errors.push('\`' + value1 + '\` is NaN (for NEAR check)');
} else if (Math.abs(value1 - value2) <= 1) {
nonMatchingProps.push('\`' + value1 + '\` is within 1 of \`' + value2 '\`');
errors.push('\`' + value1 + '\` is within 1 of \`' + value2 + '\` (for NEAR check)');
}`);
} else {
checks.push(`\
if (Number.isNaN(value1])) {
nonMatchingProps.push('\`' + value1 + '\` is NaN');
if (Number.isNaN(value1)) {
errors.push('\`' + value1 + '\` is NaN (for NEAR check)');
} else if (Math.abs(value1 - value2) > 1) {
nonMatchingProps.push('\`' + value1 + '\` is not within 1 of \`' + value2 '\`');
errors.push('\`' + value1 + '\` is not within 1 of \`' + value2 + '\` (for NEAR \
check)');
}`);
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/html_files/elements.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
</head>
<body>
<header>Another page!</header>
<div class="content">
<div class="content" nb-value="12">
<div class="right" data-whatever="a">
<p style="margin:2px;">Some text</p>
<div>Some text2</div>
Expand Down
132 changes: 130 additions & 2 deletions tests/test-js/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ function checkAssertFalse(x, func) {
checkAssertInner(x, func, 'try {\n', '\n} catch(e) { return; } throw "assert didn\'t fail";');
}

function checkAssertAttributeInner(x, func, notFound, equal, contains, startsWith) {
function checkAssertAttributeInner(x, func, notFound, equal, contains, startsWith, near) {
x.assert(func('("a", "b", )'), {
'error': 'expected a JSON dictionary as second argument, found `"b"` (a string)',
});
Expand Down Expand Up @@ -585,6 +585,31 @@ ${startsWith(3)}
'wait': false,
'checkResult': true,
});
x.assert(func('("a", {"\\"b": "c"}, [CONTAINS, NEAR])'), {
'instructions': [`\
let parseAssertElemAttr = await page.$("a");
if (parseAssertElemAttr === null) { throw '"a" not found'; }
await page.evaluate(e => {
const nonMatchingAttrs = [];
const parseAssertElemAttrDict = {"\\"b":"c"};
for (const [parseAssertElemAttrAttribute, parseAssertElemAttrValue] of Object.entries(\
parseAssertElemAttrDict)) {
if (!e.hasAttribute(parseAssertElemAttrAttribute)) {${notFound(3)}
continue;
}
const attr = e.getAttribute(parseAssertElemAttrAttribute);
${contains(2)}
${near(2)}
}
if (nonMatchingAttrs.length !== 0) {
const props = nonMatchingAttrs.join(", ");
throw "The following errors happened (for selector \`a\`): [" + props + "]";
}
}, parseAssertElemAttr);`,
],
'wait': false,
'checkResult': true,
});

// XPath
x.assert(func('("//a", {})'), {
Expand Down Expand Up @@ -718,6 +743,14 @@ doesn't contain \`" + parseAssertElemAttrValue + "\` (for CONTAINS check)");
if (!attr.startsWith(parseAssertElemAttrValue)) {
nonMatchingAttrs.push("attribute \`" + parseAssertElemAttrAttribute + "\` (\`" + attr + "\`) \
doesn't start with \`" + parseAssertElemAttrValue + "\` (for STARTS_WITH check)");
}`, indent),
indent => indentString(`\
if (Number.isNaN(attr)) {
nonMatchingProps.push('attribute \`' + parseAssertElemAttrAttribute + '\` (\`' + attr + '\
\`) is NaN (for NEAR check)');
} else if (Math.abs(attr - parseAssertElemAttrValue) > 1) {
nonMatchingProps.push('attribute \`' + parseAssertElemAttrAttribute + '\` (\`' + attr + '\
\`) is not within 1 of \`' + parseAssertElemAttrValue + '\` (for NEAR check)');
}`, indent),
);
}
Expand All @@ -741,6 +774,14 @@ if (attr.indexOf(parseAssertElemAttrValue) !== -1) {
if (attr.startsWith(parseAssertElemAttrValue)) {
nonMatchingAttrs.push("assert didn't fail for attribute \`" + parseAssertElemAttrAttribute + \
"\` (\`" + attr + "\`) (for STARTS_WITH check)");
}`, indent),
indent => indentString(`\
if (Number.isNaN(attr)) {
nonMatchingProps.push('attribute \`' + parseAssertElemAttrAttribute + '\` (\`' + attr + '\
\`) is NaN (for NEAR check)');
} else if (Math.abs(attr - parseAssertElemAttrValue) <= 1) {
nonMatchingProps.push('attribute \`' + parseAssertElemAttrAttribute + '\` (\`' + attr + '\
\`) is within 1 of \`' + parseAssertElemAttrValue + '\` (for NEAR check)');
}`, indent),
);
}
Expand Down Expand Up @@ -864,6 +905,7 @@ function checkAssertObjPropertyInner(
containsCheck,
startsWithCheck,
endsWithCheck,
nearCheck,
) {
x.assert(func('["a"]'), {
'error': 'expected a tuple or a JSON dict, found `["a"]`',
Expand Down Expand Up @@ -1005,6 +1047,27 @@ parseAssertDictPropDict)) {
const props = nonMatchingProps.join(", ");
throw "The following errors happened: [" + props + "]";
}
});`,
],
'wait': false,
'checkResult': true,
});
x.assert(func('({"a": "b"}, [STARTS_WITH, NEAR])'), {
'instructions': [`await page.evaluate(() => {
const nonMatchingProps = [];
const parseAssertDictPropDict = {"a":"b"};
for (const [parseAssertDictPropKey, parseAssertDictPropValue] of Object.entries(\
parseAssertDictPropDict)) {
if (${objName}[parseAssertDictPropKey] === undefined) {${noProp()}
continue;
}
${startsWithCheck()}
${nearCheck()}
}
if (nonMatchingProps.length !== 0) {
const props = nonMatchingProps.join(", ");
throw "The following errors happened: [" + props + "]";
}
});`,
],
'wait': false,
Expand Down Expand Up @@ -1036,6 +1099,14 @@ document[parseAssertDictPropKey] + '\`) does not start with \`' + parseAssertDic
nonMatchingProps.push('Property \`' + parseAssertDictPropKey + '\` (\`' + \
document[parseAssertDictPropKey] + '\`) does not end with \`' + parseAssertDictPropValue + '\`');
}`,
() => `if (Number.isNaN(document[parseAssertDictPropKey])) {
nonMatchingProps.push('Property \`' + parseAssertDictPropKey + '\` (\`' + \
document[parseAssertDictPropKey] + '\`) is NaN (for NEAR check)');
} else if (Math.abs(document[parseAssertDictPropKey] - parseAssertDictPropValue) > 1) {
nonMatchingProps.push('Property \`' + parseAssertDictPropKey + '\` (\`' + \
document[parseAssertDictPropKey] + '\`) is not within 1 of \`' + parseAssertDictPropValue + '\` \
(for NEAR check)');
}`,
);
}

Expand All @@ -1062,10 +1133,18 @@ if (String(document[parseAssertDictPropKey]).indexOf(parseAssertDictPropValue) !
nonMatchingProps.push("assert didn't fail for property \`" + parseAssertDictPropKey + \
'\` (for ENDS_WITH check)');
}`,
() => `if (Number.isNaN(document[parseAssertDictPropKey])) {
nonMatchingProps.push('Property \`' + parseAssertDictPropKey + '\` (\`' + \
document[parseAssertDictPropKey] + '\`) is NaN (for NEAR check)');
} else if (Math.abs(document[parseAssertDictPropKey] - parseAssertDictPropValue) <= 1) {
nonMatchingProps.push('Property \`' + parseAssertDictPropKey + '\` (\`' + \
document[parseAssertDictPropKey] + '\`) is within 1 of \`' + parseAssertDictPropValue + '\` \
(for NEAR check)');
}`,
);
}

function checkAssertVariableInner(x, func, equal, contains, starts_with, ends_with) {
function checkAssertVariableInner(x, func, equal, contains, starts_with, ends_with, near) {
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'});
Expand Down Expand Up @@ -1246,6 +1325,26 @@ const errors = [];
${contains}
${starts_with}
${ends_with}
if (errors.length !== 0) {
const errs = errors.join(", ");
throw "The following errors happened: [" + errs + "]";
}`,
],
'wait': false,
});
x.assert(func('(VAR, "a", [STARTS_WITH, NEAR])'), {
'instructions': [`\
function stringifyValue(value) {
if (['number', 'string', 'boolean'].indexOf(typeof value) !== -1) {
return String(value);
}
return JSON.stringify(value);
}
const value1 = stringifyValue(arg.variables["VAR"]);
const value2 = stringifyValue("a");
const errors = [];
${starts_with}
${near}
if (errors.length !== 0) {
const errs = errors.join(", ");
throw "The following errors happened: [" + errs + "]";
Expand All @@ -1272,6 +1371,13 @@ if (!value1.startsWith(value2)) {
`\
if (!value1.endsWith(value2)) {
errors.push("\`" + value1 + "\` doesn't end with \`" + value2 + "\` (for ENDS_WITH check)");
}`,
`\
if (Number.isNaN(value1)) {
errors.push('\`' + value1 + '\` is NaN (for NEAR check)');
} else if (Math.abs(value1 - value2) > 1) {
errors.push('\`' + value1 + '\` is not within 1 of \`' + value2 + '\` (for \
NEAR check)');
}`,
);
}
Expand All @@ -1293,6 +1399,12 @@ if (value1.startsWith(value2)) {
`\
if (value1.endsWith(value2)) {
errors.push("\`" + value1 + "\` ends with \`" + value2 + "\` (for ENDS_WITH check)");
}`,
`\
if (Number.isNaN(value1)) {
errors.push('\`' + value1 + '\` is NaN (for NEAR check)');
} else if (Math.abs(value1 - value2) <= 1) {
errors.push('\`' + value1 + '\` is within 1 of \`' + value2 + '\` (for NEAR check)');
}`,
);
}
Expand Down Expand Up @@ -1321,6 +1433,14 @@ window[parseAssertDictPropKey] + '\`) does not start with \`' + parseAssertDictP
nonMatchingProps.push('Property \`' + parseAssertDictPropKey + '\` (\`' + \
window[parseAssertDictPropKey] + '\`) does not end with \`' + parseAssertDictPropValue + '\`');
}`,
() => `if (Number.isNaN(window[parseAssertDictPropKey])) {
nonMatchingProps.push('Property \`' + parseAssertDictPropKey + '\` (\`' + \
window[parseAssertDictPropKey] + '\`) is NaN (for NEAR check)');
} else if (Math.abs(window[parseAssertDictPropKey] - parseAssertDictPropValue) > 1) {
nonMatchingProps.push('Property \`' + parseAssertDictPropKey + '\` (\`' + \
window[parseAssertDictPropKey] + '\`) is not within 1 of \`' + parseAssertDictPropValue + '\` \
(for NEAR check)');
}`,
);
}

Expand All @@ -1347,6 +1467,14 @@ if (String(window[parseAssertDictPropKey]).indexOf(parseAssertDictPropValue) !==
nonMatchingProps.push("assert didn't fail for property \`" + parseAssertDictPropKey + \
'\` (for ENDS_WITH check)');
}`,
() => `if (Number.isNaN(window[parseAssertDictPropKey])) {
nonMatchingProps.push('Property \`' + parseAssertDictPropKey + '\` (\`' + \
window[parseAssertDictPropKey] + '\`) is NaN (for NEAR check)');
} else if (Math.abs(window[parseAssertDictPropKey] - parseAssertDictPropValue) <= 1) {
nonMatchingProps.push('Property \`' + parseAssertDictPropKey + '\` (\`' + \
window[parseAssertDictPropKey] + '\`) is within 1 of \`' + parseAssertDictPropValue + '\` \
(for NEAR check)');
}`,
);
}

Expand Down
13 changes: 13 additions & 0 deletions tests/ui-tests/assert-variable.goml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Checking variables values
store-value: (variable_name, "hello")
assert-variable: (variable_name, "he", STARTS_WITH)
assert-variable: (variable_name, "o", ENDS_WITH)
assert-variable: (variable_name, 10, NEAR) // should fail
store-value: (variable_name, 10)
assert-variable: (variable_name, 10, NEAR)
assert-variable: (variable_name, 9, NEAR)
assert-variable-false: (variable_name, 9, NEAR) // should fail
assert-variable: (variable_name, 11, NEAR)
assert-variable-false: (variable_name, 8, NEAR)
assert-variable: (variable_name, 8, NEAR) // Should fail
assert-variable-false: (variable_name, 12, NEAR)
8 changes: 8 additions & 0 deletions tests/ui-tests/assert-variable.output
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
=> Starting doc-ui tests...

assert-variable... FAILED
[ERROR] (line 9) The following errors happened: [`10` is within 1 of `9` (for NEAR check)]: for command `assert-variable-false: (variable_name, 9, NEAR) // should fail`
[ERROR] (line 12) The following errors happened: [`10` is not within 1 of `8` (for NEAR check)]: for command `assert-variable: (variable_name, 8, NEAR) // Should fail`


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