You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -814,6 +814,30 @@ Very handy for waiting for an expected URL change *and* asserting if it happened
814
814
815
815
Also see [waits](#wait-api).
816
816
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
+
AndwaitForEnabled('#someId').click()
837
+
```
838
+
839
+
Also see [waits](#wait-api).
840
+
817
841
## `waitFor()`
818
842
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)
819
843
@@ -876,7 +900,7 @@ Wait for the JS expression to evaluate to `true`. Will poll using the [retry()](
876
900
* waitUntil("document.readyState == 'complete'")
877
901
```
878
902
879
-
## `waitUntil(locator,js)`
903
+
### `waitUntil(locator,js)`
880
904
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 HTMLDOM. 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 "`_`".
881
905
882
906
Here is a real-life example:
@@ -887,12 +911,12 @@ Here is a real-life example:
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.
892
916
893
917
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.
894
918
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).
896
920
897
921
For an example of how JavaScript looks like on the "Karate side" see [Function Composition](#function-composition).
898
922
@@ -906,31 +930,7 @@ And waitUntil('#eg01WaitId', "_.innerHTML == 'APPEARED!'")
906
930
And waitUntil('#eg01WaitId', '!_.disabled')
907
931
```
908
932
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
-
AndwaitUntilEnabled('#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).
934
934
935
935
### `waitUntil(function)`
936
936
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
958
958
959
959
Also see [waits](#wait-api).
960
960
961
-
### Function Composition
961
+
## Function Composition
962
962
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):
963
963
964
964
```cucumber
@@ -1017,11 +1017,11 @@ Script | Description
1017
1017
[`waitFor('#myId')`](#waitfor) | waits for an element as described above
1018
1018
`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
1019
1019
[`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')`
1020
1022
[`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
1021
1023
[`waitUntil(expression)`](#waituntil) | wait until *any* user defined JavaScript statement to evaluate to `true` in the browser
1022
1024
[`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')`
0 commit comments