This is an interesting avenue to explore! I’m not getting the same results, though.
I’ve written a new method so I don’t have to keep editing my particular elements out to share.
For this method:
@Keyword
public void waitForTextToBe(TestObject testObj, String expectedText, int timeout=120) {
boolean textFound = WebUI.waitForElementAttributeValue(testObj, 'innerText', expectedText, timeout)
if (!textFound) {
throw new Exception("Expected text '${expectedText}' not found within ${timeout} seconds.")
}
}
I have written two unit tests. The positive test case doesn’t throw an error (as I expect). The negative test case does throw an error, but I’m not catching it still:
import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import com.kms.katalon.core.testobject.ConditionType
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
boolean negTestPass = false
int waitSec = 5
String xpath = "//div[@class='title solutions-title']"
WebUI.openBrowser('https://katalon.com/')
TestObject solutionsHeader = new TestObject().addProperty('xpath', ConditionType.EQUALS, xpath)
WebUI.waitForElementVisible(solutionsHeader, 10)
String expectedText = WebUI.getText(solutionsHeader)
println("text: ${expectedText}")
String unexpectedText = expectedText + 'FooBar'
// verify method doesn't fail when expected text displays
CustomKeywords.'Global.waitForTextToBe'(solutionsHeader, expectedText, waitSec)
// verify method does fail when expected text doesn't display
try {
CustomKeywords.'Global.waitForTextToBe'(solutionsHeader, unexpectedText, waitSec)
}
catch (Exception) {
// expected exception was thrown, do nothing
negTestPass = true
}
assert negTestPass : "Negative test failed."
WebUI.closeBrowser()
And this is my full console log:
2024-03-11 14:48:25.411 INFO c.k.katalon.core.main.TestCaseExecutor - --------------------
2024-03-11 14:48:25.415 INFO c.k.katalon.core.main.TestCaseExecutor - START Test Cases/UnitTests/KeywordUTs/UTKW008_waitForTextToBe
2024-03-11 14:48:25.938 DEBUG testcase.UTKW008_waitForTextToBe - 1: negTestPass = false
2024-03-11 14:48:25.939 DEBUG testcase.UTKW008_waitForTextToBe - 2: waitSec = 5
2024-03-11 14:48:25.940 DEBUG testcase.UTKW008_waitForTextToBe - 3: xpath = "//div[@class='title solutions-title']"
2024-03-11 14:48:25.942 DEBUG testcase.UTKW008_waitForTextToBe - 4: openBrowser("https://katalon.com/")
2024-03-11 14:48:26.167 INFO c.k.k.core.webui.driver.DriverFactory - Starting 'Chrome' driver
Mar 11, 2024 2:48:26 PM org.openqa.selenium.remote.DesiredCapabilities chrome
INFO: Using `new ChromeOptions()` is preferred to `DesiredCapabilities.chrome()`
2024-03-11 14:48:26.196 INFO c.k.k.core.webui.driver.DriverFactory - Action delay is set to 0 milliseconds
Starting ChromeDriver 121.0.6167.85 (3f98d690ad7e59242ef110144c757b2ac4eef1a2-refs/branch-heads/6167@{#1539}) on port 21751
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
[1710186508.478][WARNING]: This version of ChromeDriver has not been tested with Chrome version 122.
Mar 11, 2024 2:48:28 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
2024-03-11 14:48:28.670 INFO c.k.k.core.webui.driver.DriverFactory - sessionId = 4ee71c2b870f3099713809a68b2c8088
2024-03-11 14:48:28.693 INFO c.k.k.core.webui.driver.DriverFactory - browser = Chrome 122.0.0.0
2024-03-11 14:48:28.710 INFO c.k.k.core.webui.driver.DriverFactory - platform = Mac OS X
2024-03-11 14:48:28.712 INFO c.k.k.core.webui.driver.DriverFactory - seleniumVersion = 3.141.59
2024-03-11 14:48:28.713 INFO c.k.k.core.webui.driver.DriverFactory - proxyInformation = ProxyInformation { proxyOption=NO_PROXY, proxyServerType=HTTP, username=, password=********, proxyServerAddress=127.0.0.1, proxyServerPort=0, executionList="", isApplyToDesiredCapabilities=true }
2024-03-11 14:48:31.648 DEBUG testcase.UTKW008_waitForTextToBe - 5: solutionsHeader = TestObject().addProperty("xpath", EQUALS, xpath)
2024-03-11 14:48:31.697 DEBUG testcase.UTKW008_waitForTextToBe - 6: waitForElementVisible(solutionsHeader, 10)
2024-03-11 14:48:33.376 DEBUG testcase.UTKW008_waitForTextToBe - 7: expectedText = getText(solutionsHeader)
2024-03-11 14:48:34.117 DEBUG testcase.UTKW008_waitForTextToBe - 8: println(text: $expectedText)
text: Solutions
2024-03-11 14:48:34.146 DEBUG testcase.UTKW008_waitForTextToBe - 9: unexpectedText = expectedText + "FooBar"
2024-03-11 14:48:34.147 DEBUG testcase.UTKW008_waitForTextToBe - 10: Global.waitForTextToBe(solutionsHeader, expectedText, waitSec)
2024-03-11 14:48:34.480 INFO k.k.c.m.CustomKeywordDelegatingMetaClass - Global.waitForTextToBe is PASSED
2024-03-11 14:48:34.480 DEBUG testcase.UTKW008_waitForTextToBe - 11: try
2024-03-11 14:48:34.482 DEBUG testcase.UTKW008_waitForTextToBe - 1: Global.waitForTextToBe(solutionsHeader, unexpectedText, waitSec)
2024-03-11 14:48:39.958 WARN .k.b.WaitForElementAttributeValueKeyword - Object '' does not have attribute 'innerText' with value 'SolutionsFooBar'
2024-03-11 14:48:39.982 ERROR k.k.c.m.CustomKeywordDelegatingMetaClass - ❌ java.lang.Exception: Expected text 'SolutionsFooBar' not found within 5 seconds.
2024-03-11 14:48:39.994 DEBUG testcase.UTKW008_waitForTextToBe - 12: catch (Exception Exception)
2024-03-11 14:48:40.033 DEBUG testcase.UTKW008_waitForTextToBe - 1: negTestPass = true
2024-03-11 14:48:40.058 DEBUG testcase.UTKW008_waitForTextToBe - 14: assert negTestPass : "Negative test failed."
2024-03-11 14:48:40.059 DEBUG testcase.UTKW008_waitForTextToBe - 15: closeBrowser()
2024-03-11 14:48:40.347 ERROR c.k.katalon.core.main.TestCaseExecutor - ❌ Test Cases/UnitTests/KeywordUTs/UTKW008_waitForTextToBe FAILED.
Reason:
org.codehaus.groovy.runtime.InvokerInvocationException: java.lang.Exception: Expected text 'SolutionsFooBar' not found within 5 seconds.
at Global.invokeMethod(Global.groovy)
at com.kms.katalon.core.main.CustomKeywordDelegatingMetaClass.invokeStaticMethod(CustomKeywordDelegatingMetaClass.java:55)
at UTKW008_waitForTextToBe.run(UTKW008_waitForTextToBe:26)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:448)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:439)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:418)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:410)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:285)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:144)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:135)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1710186502562.run(TempTestCase1710186502562.groovy:25)
Caused by: java.lang.Exception: Expected text 'SolutionsFooBar' not found within 5 seconds.
at Global.waitForTextToBe(Global.groovy:170)
... 14 more
2024-03-11 14:48:40.419 ERROR c.k.katalon.core.main.TestCaseExecutor - ❌ Test Cases/UnitTests/KeywordUTs/UTKW008_waitForTextToBe FAILED.
Reason:
org.codehaus.groovy.runtime.InvokerInvocationException: java.lang.Exception: Expected text 'SolutionsFooBar' not found within 5 seconds.
at Global.invokeMethod(Global.groovy)
at com.kms.katalon.core.main.CustomKeywordDelegatingMetaClass.invokeStaticMethod(CustomKeywordDelegatingMetaClass.java:55)
at UTKW008_waitForTextToBe.run(UTKW008_waitForTextToBe:26)
at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:448)
at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:439)
at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:418)
at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:410)
at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:285)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:144)
at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:135)
at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
at TempTestCase1710186502562.run(TempTestCase1710186502562.groovy:25)
Caused by: java.lang.Exception: Expected text 'SolutionsFooBar' not found within 5 seconds.
at Global.waitForTextToBe(Global.groovy:170)
... 14 more
2024-03-11 14:48:40.453 INFO c.k.katalon.core.main.TestCaseExecutor - END Test Cases/UnitTests/KeywordUTs/UTKW008_waitForTextToBe