Skip to content

Commit a40539a

Browse files
fs: wait for file content change (#1563)
1 parent f124f6c commit a40539a

8 files changed

Lines changed: 76 additions & 9 deletions

File tree

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public PageElement(WebDriver driver,
107107
this.offsetWidth = new PageElementValue<>(this, "offsetWidth", fetchIntElementPropertyFunc("offsetWidth"));
108108
this.clientHeight = new PageElementValue<>(this, "clientHeight", fetchIntElementPropertyFunc("clientHeight"));
109109
this.clientWidth = new PageElementValue<>(this, "clientWidth", fetchIntElementPropertyFunc("clientWidth"));
110-
this.snapshotValue = SnapshotValue.EMPTY;
110+
this.snapshotValue = SnapshotValue.empty();
111111
}
112112

113113
public PageElementValue<String> attribute(String name) {
@@ -366,9 +366,9 @@ public void prettyPrint(PrettyPrinter printer) {
366366

367367
@Override
368368
public void takeSnapshot() {
369-
execute(tokenizedMessage().action("taking value snapshot").add(pathDescription),
369+
snapshotValue.take(tokenizedMessage().action("taking value snapshot").add(pathDescription),
370370
() -> tokenizedMessage().action("value snapshot is taken").forP().add(pathDescription),
371-
() -> snapshotValue.take(extractActualValue()));
371+
this::extractActualValue);
372372
}
373373

374374
@Override

webtau-core/src/main/java/org/testingisdocumenting/webtau/data/snapshot/SnapshotValue.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,27 @@
1616

1717
package org.testingisdocumenting.webtau.data.snapshot;
1818

19+
import org.testingisdocumenting.webtau.reporter.TokenizedMessage;
20+
import org.testingisdocumenting.webtau.reporter.WebTauStep;
21+
22+
import java.util.function.Supplier;
23+
1924
/**
2025
* Snapshot value that maintains a state whether it was taken or not
2126
*/
2227
public class SnapshotValue {
2328
private Object value;
2429
private boolean isTaken;
2530

26-
public static final SnapshotValue EMPTY = new SnapshotValue(null, false);
31+
public static SnapshotValue empty() {
32+
return new SnapshotValue(null, false);
33+
}
2734

28-
public void take(Object value) {
29-
this.isTaken = true;
30-
this.value = value;
35+
public void take(TokenizedMessage inProgressMessage, Supplier<TokenizedMessage> completionMessage, Supplier<Object> value) {
36+
WebTauStep.createAndExecuteStep(inProgressMessage, completionMessage, () -> {
37+
this.isTaken = true;
38+
this.value = value.get();
39+
});
3140
}
3241

3342
private SnapshotValue(Object value, boolean isTaken) {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* Add: File Content [wait to change](utilities/file-system#wait-for-content-change)

webtau-docs/znai/utilities/file-system.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,14 @@ left: :include-file: doc-artifacts/fs-content-to-replace.txt {title: "file to re
4545
right: :include-file: doc-artifacts/fs-content-after-replace-text.txt {title: "result of replace"}
4646
```
4747

48+
# Wait For Content Change
49+
50+
:include-file: doc-artifacts/snippets/fsFileContent/waitForChange.groovy {
51+
title: "wait for file content change",
52+
surroundedBy: ["file-take-snapshot", "file-wait-to-change"],
53+
surroundedBySeparator: "..."
54+
}
55+
4856
# Copy
4957

5058
:include-file: doc-artifacts/snippets/fsCopy/copyFileToDir.groovy {

webtau-feature-testing/examples/scenarios/fs/files.groovy

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,19 @@ scenario('replace file content using regexp') {
6767
doc.capture('fs-content-after-replace-text', contentAfter)
6868

6969
fs.delete(path)
70+
}
71+
72+
scenario('snapshot and change') {
73+
def path = fs.tempFile('snapshot', 'change')
74+
fs.writeText(path, 'hello')
75+
76+
// file-take-snapshot
77+
def textContent = fs.textContent(path)
78+
textContent.takeSnapshot()
79+
// file-take-snapshot
80+
81+
fs.writeText(path, 'hello world')
82+
// file-wait-to-change
83+
textContent.waitTo change
84+
// file-wait-to-change
7085
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ class WebTauFileSystemFeaturesTest {
6767
'fsFileContent', 'examples/scenarios/fs/files.groovy', [
6868
'createFile.groovy': 'create file using string path',
6969
'readFile.groovy': 'read file using string path',
70-
'replaceFile.groovy': 'replace file content using regexp'
70+
'replaceFile.groovy': 'replace file content using regexp',
71+
'waitForChange.groovy': 'snapshot and change'
7172
])
7273
}
7374

webtau-feature-testing/test-expectations/scenarios/fs/files/run-details.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
{
22
"scenarioDetails" : [ {
3+
"scenario" : "after all tests",
4+
"shortContainerId" : "Teardown",
5+
"stepsSummary" : {
6+
"numberOfSuccessful" : 2
7+
}
8+
}, {
39
"scenario" : "create and delete file",
410
"shortContainerId" : "files.groovy",
511
"stepsSummary" : {
@@ -23,6 +29,12 @@
2329
"stepsSummary" : {
2430
"numberOfSuccessful" : 8
2531
}
32+
}, {
33+
"scenario" : "snapshot and change",
34+
"shortContainerId" : "files.groovy",
35+
"stepsSummary" : {
36+
"numberOfSuccessful" : 5
37+
}
2638
} ],
2739
"exitCode" : 0
2840
}

webtau-filesystem/src/main/java/org/testingisdocumenting/webtau/fs/FileTextContent.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package org.testingisdocumenting.webtau.fs;
1818

1919
import org.testingisdocumenting.webtau.data.ValuePath;
20+
import org.testingisdocumenting.webtau.data.snapshot.SnapshotValue;
21+
import org.testingisdocumenting.webtau.data.snapshot.SnapshotValueAware;
2022
import org.testingisdocumenting.webtau.expectation.ActualPathAndDescriptionAware;
2123
import org.testingisdocumenting.webtau.expectation.ActualValueExpectations;
2224
import org.testingisdocumenting.webtau.reporter.StepReportOptions;
@@ -30,12 +32,14 @@
3032

3133
import static org.testingisdocumenting.webtau.WebTauCore.tokenizedMessage;
3234

33-
public class FileTextContent implements ActualValueExpectations, ActualPathAndDescriptionAware {
35+
public class FileTextContent implements ActualValueExpectations, ActualPathAndDescriptionAware, SnapshotValueAware {
3436
private final ValuePath actualPath;
3537
private final Path path;
38+
private final SnapshotValue snapshotValue;
3639

3740
public FileTextContent(Path path) {
3841
this.actualPath = new ValuePath("file <" + path.getFileName().toString() + ">");
42+
this.snapshotValue = SnapshotValue.empty();
3943
this.path = path;
4044
}
4145

@@ -104,4 +108,21 @@ private String extractByRegexpStepImpl(Pattern regexp) {
104108

105109
return extracted;
106110
}
111+
112+
@Override
113+
public void takeSnapshot() {
114+
snapshotValue.take(tokenizedMessage().action("taking snapshot").from().url(path),
115+
() -> tokenizedMessage().action("snapshot is taken").forP().url(path),
116+
this::getData);
117+
}
118+
119+
@Override
120+
public Object snapshotValue() {
121+
return snapshotValue.required();
122+
}
123+
124+
@Override
125+
public Object currentValue() {
126+
return getData();
127+
}
107128
}

0 commit comments

Comments
 (0)