Skip to content

Commit 0da4f41

Browse files
committed
webauto: decided to rename waitForText() and waitForEnabled()
1 parent 1302d89 commit 0da4f41

File tree

6 files changed

+65
-66
lines changed

6 files changed

+65
-66
lines changed

karate-core/README.md

+35-35
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,10 @@
117117
<a href="#retry"><code>retry()</code></a>
118118
| <a href="#waitfor"><code>waitFor()</code></a>
119119
| <a href="#waitforany"><code>waitForAny()</code></a>
120-
| <a href="#waitforurl"><code>waitForUrl()</code></a>
121-
| <a href="#waituntil"><code>waitUntil()</code></a>
122-
| <a href="#waituntiltext"><code>waitUntilText()</code></a>
123-
| <a href="#waituntilenabled"><code>waitUntilEnabled()</code></a>
120+
| <a href="#waitforurl"><code>waitForUrl()</code></a>
121+
| <a href="#waitfortext"><code>waitForText()</code></a>
122+
| <a href="#waitforenabled"><code>waitForEnabled()</code></a>
123+
| <a href="#waituntil"><code>waitUntil()</code></a>
124124
| <a href="#delay"><code>delay()</code></a>
125125
| <a href="#script"><code>script()</code></a>
126126
| <a href="#scripts"><code>scripts()</code></a>
@@ -814,6 +814,30 @@ Very handy for waiting for an expected URL change *and* asserting if it happened
814814

815815
Also see [waits](#wait-api).
816816

817+
## `waitForText()`
818+
This is just a convenience short-cut for `waitUntil(locator, "_.textContent.includes('" + expected + "')")` since it is so frequently needed. Note the use of the JavaScript [`String.includes()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) function to do a *text contains* match for convenience. The need to "wait until some text appears" is so common, and with this - you don't need to worry about dealing with white-space such as line-feeds and invisible tab characters.
819+
820+
Of course, try not to use single-quotes within the string to be matched, or escape them using a back-slash (`\`) character.
821+
822+
```cucumber
823+
* waitForText('#eg01WaitId', 'APPEARED')
824+
```
825+
826+
And if you really need to scan the whole page for some text, you can use this, but it is better to be more specific for better performance:
827+
828+
```cucumber
829+
* waitForText('body', 'APPEARED')
830+
```
831+
832+
## `waitForEnabled()`
833+
This is just a convenience short-cut for `waitUntil(locator, '!_.disabled')` since it is so frequently needed:
834+
835+
```cucumber
836+
And waitForEnabled('#someId').click()
837+
```
838+
839+
Also see [waits](#wait-api).
840+
817841
## `waitFor()`
818842
This is typically used for the *first* element you need to interact with on a freshly loaded page. Use this in case a [`submit()`](#submit) for the previous action is un-reliable, see the section on [`waitFor()` instead of `submit()`](#waitfor-instead-of-submit)
819843

@@ -876,7 +900,7 @@ Wait for the JS expression to evaluate to `true`. Will poll using the [retry()](
876900
* waitUntil("document.readyState == 'complete'")
877901
```
878902

879-
## `waitUntil(locator,js)`
903+
### `waitUntil(locator,js)`
880904
A very useful variant that takes a [locator](#locators) parameter is where you supply a JavaScript "predicate" function that will be evaluated *on* the element returned by the locator in the HTML DOM. Most of the time you will prefer the short-cut boolean-expression form that begins with an underscore (or "`!`"), and Karate will inject the JavaScript DOM element reference into a variable named "`_`".
881905

882906
Here is a real-life example:
@@ -887,12 +911,12 @@ Here is a real-life example:
887911
And waitUntil('.alert-message', "_.innerHTML.includes('Some Text')")
888912
```
889913

890-
## Karate vs the Browser
914+
### Karate vs the Browser
891915
One thing you need to get used to is the "separation" between the code that is evaluated by Karate and the JavaScript that is sent to the *browser* (as a raw string) and evaluated. Pay attention to the fact that the [`includes()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) function you see in the above example - is pure JavaScript.
892916

893917
The use of `includes()` is needed in this real-life example, because [`innerHTML()`](https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML) can return leading and trailing white-space (such as line-feeds and tabs) - which would cause an exact "`==`" comparison in JavaScript to fail.
894918

895-
But guess what - this example is baked into a Karate API, see [`waitUntilText()`](#waituntiltext).
919+
But guess what - this example is baked into a Karate API, see [`waitForText()`](#waitfortext).
896920

897921
For an example of how JavaScript looks like on the "Karate side" see [Function Composition](#function-composition).
898922

@@ -906,31 +930,7 @@ And waitUntil('#eg01WaitId', "_.innerHTML == 'APPEARED!'")
906930
And waitUntil('#eg01WaitId', '!_.disabled')
907931
```
908932
909-
Also see [`waitUtntilEnabled`](#waituntilenabled) which is the preferred short-cut for the last example above, also look at the examples for [chaining](#chaining) and then the section on [waits](#wait-api).
910-
911-
## `waitUntilText()`
912-
This is just a convenience short-cut for `waitUntil(locator, "_.textContent.includes('" + expected + "')")` since it is so frequently needed. Note the use of [`includes()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) for a "string contains" match for convenience. Because the need to "wait until some text appears" is so common, and you don't need to worry about dealing with white-space such as line-feeds and invisible tab characters.
913-
914-
Of course, try not to use single-quotes within the string to be matched, or escape them using a back-slash (`\`) character.
915-
916-
```cucumber
917-
* waitUntilText('#eg01WaitId', 'APPEARED')
918-
```
919-
920-
And if you really need to scan the whole page for some text, you can use this:
921-
922-
```cucumber
923-
* waitUntilText('body', 'APPEARED')
924-
```
925-
926-
## `waitUntilEnabled()`
927-
This is just a convenience short-cut for `waitUntil(locator, '!_.disabled')` since it is so frequently needed:
928-
929-
```cucumber
930-
And waitUntilEnabled('#someId').click()
931-
```
932-
933-
Also see [waits](#wait-api).
933+
Also see [`waitForEnabled()`](#waitforenabled) which is the preferred short-cut for the last example above, also look at the examples for [chaining](#chaining) and then the section on [waits](#wait-api).
934934
935935
### `waitUntil(function)`
936936
A *very* powerful variation of `waitUntil()` takes a full-fledged JavaScript function as the argument. This can loop until *any* user-defined condition and can use any variable (or Karate or [Driver JS API](#syntax)) in scope. The signal to stop the loop is to return any not-null object. And as a convenience, whatever object is returned, can be re-used in future steps.
@@ -958,7 +958,7 @@ Then match searchResults contains 'karate-core/src/main/resources/karate-logo.pn
958958
959959
Also see [waits](#wait-api).
960960
961-
### Function Composition
961+
## Function Composition
962962
The above example can be re-factored in a very elegant way as follows, using Karate's [native support for JavaScript](https://github.com/intuit/karate#javascript-functions):
963963
964964
```cucumber
@@ -1017,11 +1017,11 @@ Script | Description
10171017
[`waitFor('#myId')`](#waitfor) | waits for an element as described above
10181018
`retry(10).waitFor('#myId')` | like the above, but temporarily over-rides the settings to wait for a [longer time](#retry-actions), and this can be done for *all* the below examples as well
10191019
[`waitForUrl('google.com')`](#waitforurl) | for convenience, this uses a string *contains* match - so for example you can omit the `http` or `https` prefix
1020+
[`waitForText('#myId', 'appeared')`](#waitfortext) | frequently needed short-cut for waiting until a string appears - and this uses a "string contains" match for convenience
1021+
[`waitForEnabled('#mySubmit')`](#waitforenabled) | frequently needed short-cut for `waitUntil(locator, '!_disabled')`
10201022
[`waitForAny('#myId', '#maybe')`](#waitforany) | handle if an element may or *may not* appear, and if it does, handle it - for e.g. to get rid of an ad popup or dialog
10211023
[`waitUntil(expression)`](#waituntil) | wait until *any* user defined JavaScript statement to evaluate to `true` in the browser
10221024
[`waitUntil(function)`](#waituntilfunction) | use custom logic to handle *any* kind of situation where you need to wait, *and* use other API calls if needed
1023-
[`waitUntilText()`](#waituntiltext) | frequently needed short-cut for waiting until a string appears - and this uses a "string contains" match for convenience
1024-
[`waitUntilEnabled()`](#waituntilenabled) | frequently needed short-cut for `waitUntil(locator, '!_disabled')`
10251025

10261026
Also see the examples for [chaining](#chaining).
10271027

karate-core/src/main/java/com/intuit/karate/driver/Driver.java

+8-12
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,14 @@ default String waitForUrl(String expected) {
149149
return getOptions().waitForUrl(this, expected);
150150
}
151151

152+
default Element waitForText(String locator, String expected) {
153+
return waitUntil(locator, "_.textContent.includes('" + expected + "')");
154+
}
155+
156+
default Element waitForEnabled(String locator) {
157+
return waitUntil(locator, "!_.disabled");
158+
}
159+
152160
default Element waitForAny(String locator1, String locator2) {
153161
return getOptions().waitForAny(this, new String[]{locator1, locator2});
154162
}
@@ -161,18 +169,6 @@ default Element waitUntil(String locator, String expression) {
161169
return getOptions().waitUntil(this, locator, expression);
162170
}
163171

164-
default Element waitUntilEnabled(String locator) {
165-
return waitUntil(locator, "!_.disabled");
166-
}
167-
168-
default Element waitUntilText(String locator, String expected) {
169-
return waitUntil(locator, "_.textContent.includes('" + expected + "')");
170-
}
171-
172-
default Element waitUntilText(String expected) {
173-
return waitUntil("document", "_.textContent.includes('" + expected + "')");
174-
}
175-
176172
default Object waitUntil(Supplier<Object> condition) {
177173
return getOptions().retry(() -> condition.get(), o -> o != null, "waitUntil (function)");
178174
}

karate-core/src/main/java/com/intuit/karate/driver/DriverElement.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public Element input(String value) {
104104
@Override
105105
public Element input(String[] values) {
106106
return driver.input(locator, values);
107-
}
107+
}
108108

109109
@Override
110110
public Element select(String text) {
@@ -135,14 +135,14 @@ public Element waitFor() {
135135
}
136136

137137
@Override
138-
public Element waitUntil(String expression) {
139-
return driver.waitUntil(locator, expression); // will throw exception if not found
138+
public Element waitForText(String text) {
139+
return driver.waitForText(locator, text);
140140
}
141141

142142
@Override
143-
public Element waitUntilText(String text) {
144-
return driver.waitUntilText(locator, text);
145-
}
143+
public Element waitUntil(String expression) {
144+
return driver.waitUntil(locator, expression); // will throw exception if not found
145+
}
146146

147147
@Override
148148
public Object script(String expression) {

karate-core/src/main/java/com/intuit/karate/driver/Element.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public interface Element {
6161

6262
Element waitUntil(String expression);
6363

64-
Element waitUntilText(String text);
64+
Element waitForText(String text);
6565

6666
Object script(String expression);
6767

karate-core/src/main/java/com/intuit/karate/driver/MissingElement.java

+9-9
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public boolean isExists() {
5050
@Override
5151
public boolean isEnabled() {
5252
return true; // hmm
53-
}
53+
}
5454

5555
@Override
5656
public Element focus() {
@@ -70,12 +70,12 @@ public Element click() {
7070
@Override
7171
public Element submit() {
7272
return this;
73-
}
73+
}
7474

7575
@Override
7676
public Mouse mouse() {
7777
return null;
78-
}
78+
}
7979

8080
@Override
8181
public Element input(String text) {
@@ -86,7 +86,7 @@ public Element input(String text) {
8686
public Element input(String[] values) {
8787
return this;
8888
}
89-
89+
9090
@Override
9191
public Element select(String text) {
9292
return this;
@@ -100,28 +100,28 @@ public Element select(int index) {
100100
@Override
101101
public Element switchFrame() {
102102
return this;
103-
}
103+
}
104104

105105
@Override
106106
public Element delay(int millis) {
107107
driver.delay(millis);
108108
return this;
109-
}
109+
}
110110

111111
@Override
112112
public Element waitFor() {
113113
return this;
114114
}
115115

116116
@Override
117-
public Element waitUntil(String expression) {
117+
public Element waitForText(String text) {
118118
return this;
119119
}
120120

121121
@Override
122-
public Element waitUntilText(String text) {
122+
public Element waitUntil(String expression) {
123123
return this;
124-
}
124+
}
125125

126126
@Override
127127
public Object script(String expression) {

karate-demo/src/test/java/driver/core/test-01.feature

+6-3
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@ Scenario Outline: using <config>
1313
# wait for very slow loading element
1414
And waitFor('#eg01WaitId')
1515

16+
# wait for text (is a string "contains" match for convenience)
17+
And waitForText('#eg01WaitId', 'APPEARED')
18+
And waitForText('body', 'APPEARED')
19+
And waitForEnabled('#eg01WaitId')
20+
1621
# powerful variants of the above, call any js on the element
1722
And waitUntil('#eg01WaitId', "function(e){ return e.innerHTML == 'APPEARED!' }")
1823
And waitUntil('#eg01WaitId', "_.innerHTML == 'APPEARED!'")
1924
And waitUntil('#eg01WaitId', '!_.disabled')
20-
And waitUntilText('#eg01WaitId', 'APPEARED')
21-
And waitUntilText('body', 'APPEARED')
22-
And waitUntilEnabled('#eg01WaitId')
25+
2326
And match script('#eg01WaitId', "function(e){ return e.innerHTML }") == 'APPEARED!'
2427
And match script('#eg01WaitId', '_.innerHTML') == 'APPEARED!'
2528
And match script('#eg01WaitId', '!_.disabled') == true

0 commit comments

Comments
 (0)