Skip to content

Commit

Permalink
Merge pull request #621 from GuillaumeGomez/wait-for-text
Browse files Browse the repository at this point in the history
Add `wait-for-text-false` command
  • Loading branch information
GuillaumeGomez authored Sep 27, 2024
2 parents 69797d0 + d1737c0 commit 0f7967f
Show file tree
Hide file tree
Showing 38 changed files with 1,096 additions and 4 deletions.
26 changes: 24 additions & 2 deletions goml-script.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ Here's the command list:
* [`wait-for-text`](#wait-for-text)
* [`wait-for-text-false`](#wait-for-text-false)
* [`wait-for-size`](#wait-for-size)
* [`wait-for-size-false`](#wait-for-size-false)
* [`wait-for-window-property`](#wait-for-window-property)
* [`wait-for-window-property-false`](#wait-for-window-property-false)
* [`write`](#write)
Expand Down Expand Up @@ -2253,7 +2254,7 @@ If you want to wait for all properties to have the expected values, take a look

#### wait-for-size

**wait-for-size** command wait for the given element(s) that either the "width" or the "height" (or both) have the expected value. Examples:
**wait-for-size** command waits for the given element(s) that either the "width" or the "height" (or both) have the expected value. Examples:

```
wait-for-size: ("button", {"width": 200, "height": 20})
Expand All @@ -2269,7 +2270,28 @@ wait-for-size: ("button", {"width": 200, "height": 20}, ALL)
wait-for-size: ("//button", {"width": 200, "height": 20}, ALL)
```

To be more exact, this command compares the "offsetWidth" and "offsetHeight", which include the content
To be more exact, this command compares the "offsetWidth" and "offsetHeight", which includes the content
size, the padding and the border.

#### wait-for-size-false

**wait-for-size-false** command waits for any of the given element(s) that either the "width" or the "height" (or both) not have the expected value. Examples:

```
wait-for-size-false: ("button", {"width": 200, "height": 20})
// Same with XPath
wait-for-size-false: ("//button", {"width": 200, "height": 20})
```

If you want to check that any of the elements matching the given selector do not have the expected value(s), use `ALL`:

```
wait-for-size-false: ("button", {"width": 200, "height": 20}, ALL)
// Same with XPath
wait-for-size-false: ("//button", {"width": 200, "height": 20}, ALL)
```

To be more exact, this command compares the "offsetWidth" and "offsetHeight", which includes the content
size, the padding and the border.

#### wait-for-text
Expand Down
1 change: 1 addition & 0 deletions src/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ const ORDERS = {
'wait-for-property': commands.parseWaitForProperty,
'wait-for-property-false': commands.parseWaitForPropertyFalse,
'wait-for-size': commands.parseWaitForSize,
'wait-for-size-false': commands.parseWaitForSizeFalse,
'wait-for-text': commands.parseWaitForText,
'wait-for-text-false': commands.parseWaitForTextFalse,
'wait-for-window-property': commands.parseWaitForWindowProperty,
Expand Down
1 change: 1 addition & 0 deletions src/commands/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ module.exports = {
'parseWaitForProperty': wait.parseWaitForProperty,
'parseWaitForPropertyFalse': wait.parseWaitForPropertyFalse,
'parseWaitForSize': wait.parseWaitForSize,
'parseWaitForSizeFalse': wait.parseWaitForSizeFalse,
'parseWaitForText': wait.parseWaitForText,
'parseWaitForTextFalse': wait.parseWaitForTextFalse,
'parseWaitForWindowProperty': wait.parseWaitForWindowProperty,
Expand Down
25 changes: 23 additions & 2 deletions src/commands/wait.js
Original file line number Diff line number Diff line change
Expand Up @@ -1213,11 +1213,24 @@ ${indentString(incr, 1)}
'checkResult': true,
};
}

// Possible inputs:
//
// * ("CSS selector", JSON dict)
// * ("XPath", JSON dict)
function parseWaitForSize(parser) {
return parseWaitForSizeInner(parser, false);
}

// Possible inputs:
//
// * ("CSS selector", JSON dict)
// * ("XPath", JSON dict)
function parseWaitForSizeFalse(parser) {
return parseWaitForSizeInner(parser, true);
}

function parseWaitForSizeInner(parser, waitFalse) {
const identifiers = ['ALL'];
const ret = validator(parser, {
kind: 'tuple',
Expand Down Expand Up @@ -1278,17 +1291,24 @@ function parseWaitForSize(parser) {
const whole = commonSizeCheckCode(
selector, enabledChecks.has('ALL'), false, json, varName, errorsVarName);

let comp = '===';
let errorMessage = '"The following checks still fail: [" + err + "]"';
if (waitFalse) {
comp = '!==';
errorMessage = '"All checks still pass"';
}

const [init, looper] = waitForElement(selector, varName, {checkAll: enabledChecks.has('ALL')});
const incr = incrWait(`\
const err = ${errorsVarName}.join(", ");
throw new Error("The following checks still fail: [" + err + "]");`);
throw new Error(${errorMessage});`);

const instructions = `\
${init}
while (true) {
${indentString(looper, 1)}
${indentString(whole, 1)}
if (errors.length === 0) {
if (errors.length ${comp} 0) {
break;
}
Expand Down Expand Up @@ -1325,4 +1345,5 @@ module.exports = {
'parseWaitForWindowProperty': parseWaitForWindowProperty,
'parseWaitForWindowPropertyFalse': parseWaitForWindowPropertyFalse,
'parseWaitForSize': parseWaitForSize,
'parseWaitForSizeFalse': parseWaitForSizeFalse,
};
51 changes: 51 additions & 0 deletions tests/api-output/parseWaitForSizeFalse/basic-1.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
instructions = [
"""const timeLimit = page.getDefaultTimeout();
const timeAdd = 50;
let allTime = 0;
let assertSize = null;
while (true) {
while (true) {
assertSize = await page.$$(\"a\");
if (assertSize.length !== 0) {
assertSize = assertSize[0];
break;
}
await new Promise(r => setTimeout(r, timeAdd));
if (timeLimit === 0) {
continue;
}
allTime += timeAdd;
if (allTime >= timeLimit) {
throw new Error(\"The CSS selector \\\"a\\\" was not found\");
}
}
async function checkElemSize(elem) {
return await elem.evaluate(e => {
const innerErrors = [];
const height = e.offsetHeight;
const width = e.offsetWidth;
return innerErrors;
});
}
const errors = [];
errors.push(...await checkElemSize(assertSize));
if (errors.length !== 0) {
break;
}
await new Promise(r => setTimeout(r, timeAdd));
if (timeLimit === 0) {
continue;
}
allTime += timeAdd;
if (allTime >= timeLimit) {
const err = errors.join(\", \");
throw new Error(\"All checks still pass\");
}
}""",
]
wait = false
warnings = [
]
checkResult = true
53 changes: 53 additions & 0 deletions tests/api-output/parseWaitForSizeFalse/basic-2.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
instructions = [
"""const timeLimit = page.getDefaultTimeout();
const timeAdd = 50;
let allTime = 0;
let assertSize = null;
while (true) {
while (true) {
assertSize = await page.$$(\"a\");
if (assertSize.length !== 0) {
assertSize = assertSize[0];
break;
}
await new Promise(r => setTimeout(r, timeAdd));
if (timeLimit === 0) {
continue;
}
allTime += timeAdd;
if (allTime >= timeLimit) {
throw new Error(\"The CSS selector \\\"a\\\" was not found\");
}
}
async function checkElemSize(elem) {
return await elem.evaluate(e => {
const innerErrors = [];
const height = e.offsetHeight;
const width = e.offsetWidth;
if (width !== 1) {
innerErrors.push(\"expected a width of `1`, found `\" + width + \"`\");
}
return innerErrors;
});
}
const errors = [];
errors.push(...await checkElemSize(assertSize));
if (errors.length !== 0) {
break;
}
await new Promise(r => setTimeout(r, timeAdd));
if (timeLimit === 0) {
continue;
}
allTime += timeAdd;
if (allTime >= timeLimit) {
const err = errors.join(\", \");
throw new Error(\"All checks still pass\");
}
}""",
]
wait = false
warnings = [
]
checkResult = true
57 changes: 57 additions & 0 deletions tests/api-output/parseWaitForSizeFalse/basic-3.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
instructions = [
"""const timeLimit = page.getDefaultTimeout();
const timeAdd = 50;
let allTime = 0;
let assertSize = null;
while (true) {
while (true) {
assertSize = await page.$$(\"a\");
if (assertSize.length !== 0) {
break;
}
await new Promise(r => setTimeout(r, timeAdd));
if (timeLimit === 0) {
continue;
}
allTime += timeAdd;
if (allTime >= timeLimit) {
throw new Error(\"The CSS selector \\\"a\\\" was not found\");
}
}
async function checkElemSize(elem) {
return await elem.evaluate(e => {
const innerErrors = [];
const height = e.offsetHeight;
const width = e.offsetWidth;
if (width !== 1) {
innerErrors.push(\"expected a width of `1`, found `\" + width + \"`\");
}
return innerErrors;
});
}
const errors = [];
for (const elem of assertSize) {
errors.push(...await checkElemSize(elem));
if (errors.length !== 0) {
break;
}
}
if (errors.length !== 0) {
break;
}
await new Promise(r => setTimeout(r, timeAdd));
if (timeLimit === 0) {
continue;
}
allTime += timeAdd;
if (allTime >= timeLimit) {
const err = errors.join(\", \");
throw new Error(\"All checks still pass\");
}
}""",
]
wait = false
warnings = [
]
checkResult = true
53 changes: 53 additions & 0 deletions tests/api-output/parseWaitForSizeFalse/basic-4.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
instructions = [
"""const timeLimit = page.getDefaultTimeout();
const timeAdd = 50;
let allTime = 0;
let assertSize = null;
while (true) {
while (true) {
assertSize = await page.$$(\"a\");
if (assertSize.length !== 0) {
assertSize = assertSize[0];
break;
}
await new Promise(r => setTimeout(r, timeAdd));
if (timeLimit === 0) {
continue;
}
allTime += timeAdd;
if (allTime >= timeLimit) {
throw new Error(\"The CSS selector \\\"a\\\" was not found\");
}
}
async function checkElemSize(elem) {
return await elem.evaluate(e => {
const innerErrors = [];
const height = e.offsetHeight;
const width = e.offsetWidth;
if (height !== 1) {
innerErrors.push(\"expected a height of `1`, found `\" + height + \"`\");
}
return innerErrors;
});
}
const errors = [];
errors.push(...await checkElemSize(assertSize));
if (errors.length !== 0) {
break;
}
await new Promise(r => setTimeout(r, timeAdd));
if (timeLimit === 0) {
continue;
}
allTime += timeAdd;
if (allTime >= timeLimit) {
const err = errors.join(\", \");
throw new Error(\"All checks still pass\");
}
}""",
]
wait = false
warnings = [
]
checkResult = true
57 changes: 57 additions & 0 deletions tests/api-output/parseWaitForSizeFalse/basic-5.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
instructions = [
"""const timeLimit = page.getDefaultTimeout();
const timeAdd = 50;
let allTime = 0;
let assertSize = null;
while (true) {
while (true) {
assertSize = await page.$$(\"a\");
if (assertSize.length !== 0) {
break;
}
await new Promise(r => setTimeout(r, timeAdd));
if (timeLimit === 0) {
continue;
}
allTime += timeAdd;
if (allTime >= timeLimit) {
throw new Error(\"The CSS selector \\\"a\\\" was not found\");
}
}
async function checkElemSize(elem) {
return await elem.evaluate(e => {
const innerErrors = [];
const height = e.offsetHeight;
const width = e.offsetWidth;
if (height !== 1) {
innerErrors.push(\"expected a height of `1`, found `\" + height + \"`\");
}
return innerErrors;
});
}
const errors = [];
for (const elem of assertSize) {
errors.push(...await checkElemSize(elem));
if (errors.length !== 0) {
break;
}
}
if (errors.length !== 0) {
break;
}
await new Promise(r => setTimeout(r, timeAdd));
if (timeLimit === 0) {
continue;
}
allTime += timeAdd;
if (allTime >= timeLimit) {
const err = errors.join(\", \");
throw new Error(\"All checks still pass\");
}
}""",
]
wait = false
warnings = [
]
checkResult = true
1 change: 1 addition & 0 deletions tests/api-output/parseWaitForSizeFalse/err-1.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
error = """expected a JSON dict, found `\"b\"` (a string) (second element of the tuple)"""
Loading

0 comments on commit 0f7967f

Please sign in to comment.