We have a Vaadin based web service and HTML pages are non static.
E.g. changing some selection will cause a replacement of DOM instead of a âpage reloadâ.
There are HTML tags like this where the element attributes stay the same, only the text part will be changed:
Before clicking on web page:
<div class="v-label v-widget register-contentbox-heading v-label-register-contentbox-heading v-has-width" id="lbl_contentbox_heading" style="width: 361px;">This is the FIRST text</div>
After changing some selection on page the text will be relaced by server:
<div class="v-label v-widget register-contentbox-heading v-label-register-contentbox-heading v-has-width" id="lbl_contentbox_heading" style="width: 361px;">This is the SECOND text I want to wait for</div>
Those available Keywords do not work:
- WebUI.waitForPageLoad(5) â is not usable, as the browser does not initially load the page
- WebUI.waitForElementVisible(findTestObject(âlbl_element_om_pageâ), 5) â this one waits until the element is visible. But the element was already visible before, too
- WebUI.verifyElementText(findTestObject(âlbl_element_om_pageâ), âThis is the SECOND text I want to wait forâ) â The element is already present, but still with old text value âThis is the FIRST textâ
- WebUI.waitForElementAttributeValue() â This is not usable, because the Element does not have a âtextâ attribute
Thatâs why I would like to request a new build-in âWebUI.waitForElementText()â with timeout and selection of failure handling.
Currently I have created my own CustomKeyword to search in complete HTML body for a given text fragment with a timeout condition:
CustomKeywords.'guiResources.guiSupportFunctions.waitForTextDisplayed'('This is the SECOND text I want to wait for' , 5, FailureHandling.CONTINUE_ON_FAILURE)
The corresponding CustomKeyword code looks like this in my file guiSupportFunctions.groovy
package guiResources
import org.openqa.selenium.WebDriver
import com.kms.katalon.core.webui.driver.DriverFactory
import org.openqa.selenium.By
class guiSupportFunctions {
private static final KeywordLogger LOG = new KeywordLogger();
/**
\* Wait until the given text is visible on page
*
\* @param input
* Der String, auf dessen Anzeige gewartet wird
\* @param timeout
* timeout value, wie lange gewartet wird
\* @param failureHandling
* failureHandling to control the test step result
\* @return true, if "input" is found within the given timeout value,
* else return false
*/
@Keyword
public static boolean waitForTextDisplayed(String input) {
return waitForTextDisplayed(input, 5, FailureHandling.STOP\_ON\_FAILURE)
}
@Keyword
public static boolean waitForTextDisplayed(String input, Integer timeout) {
return waitForTextDisplayed(input, timeout, FailureHandling.STOP\_ON\_FAILURE)
}
@Keyword
public static boolean waitForTextDisplayed(String input, Integer timeout, FailureHandling failureHandling) {
WebDriver driver = DriverFactory.getWebDriver()
long startWaitTime = java.util.Calendar.getInstance().getTimeInMillis();
long elapsedTime = 0;
long timeoutms = timeout * 1000;
LOG.logInfo("waitForTextDisplayed('" + input + "') - wait for max " + (timeoutms / 1000) + " secs");
while (!isTextPresentOnPage(driver, input) && (elapsedTime < timeoutms)) {
try {
Thread.sleep(100);
elapsedTime = java.util.Calendar.getInstance().getTimeInMillis() - startWaitTime;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
(!isTextPresentOnPage(driver, input) && (elapsedTime < timeoutms))
}
elapsedTime = java.util.Calendar.getInstance().getTimeInMillis() - startWaitTime;
LOG.logInfo("elapsed time: " + (elapsedTime / 1000) + "s");
boolean found = isTextPresentOnPage(driver, input);
// For negativ tests it possible \*not\* to find the given text
if (!found) {
LOG.logInfo("Did wait for " + (timeoutms / 1000) + " seconds for text '" + input
\+ "' to be displayed - without success. Giving up.");
if ( failureHandling == FailureHandling.STOP\_ON\_FAILURE) {
throw new StepFailedException("Did wait for " + (timeoutms / 1000) + " seconds for text '" + input
\+ "' to be displayed - without success. Giving up.");
} else {
KeywordUtil.markFailed("Did wait for " + (timeoutms / 1000) + " seconds for text '" + input
\+ "' to be displayed - without success. Giving up.")
}
return false;
}
LOG.logInfo("Text is displayed. Continue with test.");
return true;
}
/**
\* Check if given text is present directly in html-body
*
\* @param text
* The text to be find
\* @return true, if "text" is visible/found,
* else false
*/
private static boolean isTextPresentOnPage(WebDriver driver, String text) {
String bodyText = "";
try {
bodyText = driver.findElement(By.tagName("body")).getText();
} catch (Exception e) {
LOG.logInfo("isTextPresentOnPage(): Giving up. Could not read body text because of Exception: " + e.getLocalizedMessage());
KeywordUtil.markFailed("isTextPresentOnPage(): Giving up. Could not read body text because of Exception: " + e.getLocalizedMessage());
return false;
}
return bodyText.contains(text.trim());
}
}