Skip to content

Commit f124f6c

Browse files
browser: page element change matcher (#1562)
1 parent 63bd481 commit f124f6c

9 files changed

Lines changed: 78 additions & 28 deletions

File tree

webtau-browser/src/main/java/org/testingisdocumenting/webtau/browser/page/PageElement.java

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,7 @@
4242
import org.testingisdocumenting.webtau.expectation.ActualPathAndDescriptionAware;
4343
import org.testingisdocumenting.webtau.expectation.ActualValueExpectations;
4444
import org.testingisdocumenting.webtau.expectation.state.VisibleStateAware;
45-
import org.testingisdocumenting.webtau.reporter.StepReportOptions;
46-
import org.testingisdocumenting.webtau.reporter.TokenizedMessage;
47-
import org.testingisdocumenting.webtau.reporter.WebTauStepInput;
48-
import org.testingisdocumenting.webtau.reporter.WebTauStepInputKeyValue;
45+
import org.testingisdocumenting.webtau.reporter.*;
4946

5047
import java.util.*;
5148
import java.util.function.Consumer;
@@ -367,6 +364,23 @@ public void prettyPrint(PrettyPrinter printer) {
367364
new PageElementPrettyPrinter(this, 10).prettyPrint(printer);
368365
}
369366

367+
@Override
368+
public void takeSnapshot() {
369+
execute(tokenizedMessage().action("taking value snapshot").add(pathDescription),
370+
() -> tokenizedMessage().action("value snapshot is taken").forP().add(pathDescription),
371+
() -> snapshotValue.take(extractActualValue()));
372+
}
373+
374+
@Override
375+
public Object snapshotValue() {
376+
return snapshotValue.required();
377+
}
378+
379+
@Override
380+
public Object currentValue() {
381+
return extractActualValue();
382+
}
383+
370384
List<HtmlNode> extractHtmlNodes() {
371385
return extractHtmlNodes(findElements());
372386
}
@@ -574,21 +588,6 @@ private void checkNotNullAndExecuteScriptOnElement(String actionLabel, String sc
574588
((JavascriptExecutor) driver).executeScript(script, argsList.toArray(new Object[0]));
575589
}
576590

577-
@Override
578-
public void takeSnapshot() {
579-
snapshotValue.take(extractActualValue());
580-
}
581-
582-
@Override
583-
public Object snapshotValue() {
584-
return snapshotValue.required();
585-
}
586-
587-
@Override
588-
public Object currentValue() {
589-
return extractActualValue();
590-
}
591-
592591
private Object extractActualValue() {
593592
return isMarkedAsAll ?
594593
valuesList.get() :

webtau-docs/znai/browser/asynchronous-page.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ If presence/absence of an element is not important, you can directly to wait for
2828

2929
Note: any matcher that you can use with `should` and `shouldNot` can be used with `waitTo` and `waitToNot`
3030

31+
# Wait to change
32+
33+
Use `change` matcher when you want to wait for any change of a value without knowing the specifics.
34+
35+
:include-file: doc-artifacts/snippets/matchers/snapshotAndChange.groovy {title: "wait for change", noGap: true, noGapSeparator: true}
36+
:include-cli-output: doc-artifacts/matchers.groovy-console-output.txt {startLine: "change matcher", endLine: "change matcher", excludeStartEnd: true}
37+
3138
# Wait on url
3239

3340
Another cue to use could be a url change after an action.

webtau-docs/znai/browser/matchers.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
---
2-
type: two-sides
3-
---
4-
51
# Text
62

3+
:include-xml: doc-artifacts/snippets/matchers/texts.html
4+
75
:include-file: doc-artifacts/snippets/matchers/equalText.groovy {title: "exact text"}
86

97
:include-file: doc-artifacts/snippets/matchers/equalTextRegexp.groovy {title: "regexp text"}
@@ -19,10 +17,10 @@ It is not required when you do an explicit comparison with the list on the right
1917

2018
:include-file: doc-artifacts/snippets/matchers/equalListOfTextAndRegexp.groovy {title: "list of text and regexp"}
2119

22-
:include-xml: doc-artifacts/snippets/matchers/texts.html {rightSide: true}
23-
2420
# Numbers
2521

22+
:include-xml: doc-artifacts/snippets/matchers/numbers.html
23+
2624
:include-file: doc-artifacts/snippets/matchers/equalNumber.groovy {title: "exact number"}
2725

2826
:include-file: doc-artifacts/snippets/matchers/greaterNumber.groovy {title: "greater than"}
@@ -33,13 +31,17 @@ It is not required when you do an explicit comparison with the list on the right
3331

3432
:include-file: doc-artifacts/snippets/matchers/lessEqualListMixOfNumbers.groovy {title: "list of exact and greater/less"}
3533

36-
:include-xml: doc-artifacts/snippets/matchers/numbers.html {rightSide: true}
37-
3834
# State
3935

36+
:include-file: doc-artifacts/snippets/matchers/state.html
37+
4038
:include-file: doc-artifacts/snippets/matchers/enabledDisabled.groovy {title: "enabled/disabled"}
4139

4240
:include-file: doc-artifacts/snippets/matchers/visibleHidden.groovy {title: "visible/hidden"}
4341

44-
:include-file: doc-artifacts/snippets/matchers/state.html {rightSide: true}
42+
# Snapshot And Change
43+
44+
:include-file: doc-artifacts/snippets/matchers/snapshotAndChange.groovy {title: "wait for change", noGap: true, noGapSeparator: true}
45+
:include-cli-output: doc-artifacts/matchers.groovy-console-output.txt {startLine: "change matcher", endLine: "change matcher", excludeStartEnd: true}
46+
4547

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* Add: Browser page element [wait to change](browser/asynchronous-page#wait-to-change)

webtau-feature-testing/examples/scenarios/ui/matchers.groovy

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,14 @@ scenario("visible state") {
7171
def feedback = $("#feedback")
7272
feedback.shouldBe hidden
7373
feedback.shouldNotBe visible
74+
}
75+
76+
scenario("change matcher") {
77+
def number = $("#number-to-change")
78+
def trigger = $("#change-number")
79+
80+
number.takeSnapshot()
81+
trigger.click()
82+
83+
number.waitTo change
7484
}

webtau-feature-testing/src/test/groovy/org/testingisdocumenting/webtau/featuretesting/WebTauBrowserFeaturesTestBase.groovy

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ class WebTauBrowserFeaturesTestBase {
220220
"lessEqualListMixOfNumbers.groovy": "less equal list mix of numbers",
221221
"enabledDisabled.groovy": "enable state",
222222
"visibleHidden.groovy": "visible state",
223+
"snapshotAndChange.groovy": "change matcher",
223224
])
224225

225226
extractHtmlSnippets(root, "matchers.html", [

webtau-feature-testing/src/test/resources/matchers.html

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<!--
2+
~ Copyright 2024 webtau maintainers
23
~ Copyright 2019 TWO SIGMA OPEN SOURCE, LLC
34
~
45
~ Licensed under the Apache License, Version 2.0 (the "License");
@@ -15,6 +16,17 @@
1516
-->
1617

1718
<html>
19+
<head>
20+
<script>
21+
function changeNumber() {
22+
var htmlElement = document.getElementById('number-to-change');
23+
var number = Number(htmlElement.innerText);
24+
number++;
25+
htmlElement.innerText = "" + number;
26+
}
27+
</script>
28+
<title>webtau matchers test</title>
29+
</head>
1830
<body>
1931
<div id="numbers">
2032
<div>
@@ -46,5 +58,11 @@
4658
<div id="feedback" style="display: none;"></div>
4759
</div>
4860
</div>
61+
<div id="change">
62+
<div>
63+
<button id="change-number" onclick='changeNumber()'>Click to change number</button>
64+
<div id="number-to-change">300</div>
65+
</div>
66+
</div>
4967
</body>
5068
</html>

webtau-feature-testing/test-expectations/scenarios/ui/matchers/chrome-run-details.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@
8989
"stepsSummary" : {
9090
"numberOfSuccessful" : 2
9191
}
92+
}, {
93+
"scenario" : "change matcher",
94+
"shortContainerId" : "matchers.groovy",
95+
"stepsSummary" : {
96+
"numberOfSuccessful" : 3
97+
}
9298
} ],
9399
"exitCode" : 0
94100
}

webtau-feature-testing/test-expectations/scenarios/ui/matchers/firefox-run-details.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,12 @@
8989
"stepsSummary" : {
9090
"numberOfSuccessful" : 2
9191
}
92+
}, {
93+
"scenario" : "change matcher",
94+
"shortContainerId" : "matchers.groovy",
95+
"stepsSummary" : {
96+
"numberOfSuccessful" : 3
97+
}
9298
} ],
9399
"exitCode" : 0
94100
}

0 commit comments

Comments
 (0)