可以使用try-catch块在测试脚本中捕获RuntimeExceptions。以下是示例代码:
Background:
* configure driver = { type: 'chromedriver', showDriverLog: true }
Scenario: Catch RuntimeExceptions thrown from driver failures
Given driver 'https://github.com'
* def expectedException = 'Exception has occurred!'
* try { driver.input('#junk', 'bad Input') }
* catch (e) {
karate.log('Caught exception: ', e)
assert e.message == expectedException
}
在上面的示例中,我们在try
块中调用driver.input
,该方法将抛出RuntimeExceptions
。在catch
块中,我们捕获和记录异常,并验证其消息是否与expectedException
字符串匹配。
通过这种方式,我们可以捕获并处理由驱动程序故障抛出的异常,从而有助于脚本的调试和维护。
上一篇:捕获开始标签但不捕获闭合标签