在 Katalon Studio 中配置 If-else 语句

Configuring If-else Statement in Katalon Studio

我正在尝试配置 if-else 语句,但我的代码卡在 if 语句上,无法继续执行 else 语句。

我尝试将 break; 添加到 if 语句,但它也不起作用。

WebUI.openBrowser('')

WebUI.navigateToUrl('123/account/login?ReturnUrl=%2F')

WebUI.maximizeWindow()

WebUI.setText(findTestObject('123/Page_Log in/input_ _Username'), 'admin')

WebUI.setEncryptedText(findTestObject('123/Page_Log in/input_ _Password'), 'admin')

not_run: WebUI.verifyElementPresent(findTestObject('123/Page_Log in/span_Log in'), 1)

WebUI.click(findTestObject('123/Page_Log in/span_Log in'))

not_run: WebUI.verifyElementPresent(findTestObject('123/Page_Operator/button_To activate session sta'), 1)

WebUI.click(findTestObject('123/Page_Operator/button_To activate session sta'))

if (WebUI.verifyTextPresent("Operator already has active session", true)) {
    WebUI.click(findTestObject('if-else/Page_Operator/button_Clear'))

    WebUI.click(findTestObject('if-else/Page_Operator/button_To activate session sta'))

    WebUI.waitForPageLoad(5)

    WebUI.click(findTestObject('123/Page_Operator/click_phone'))

    WebDriver driver = DriverFactory.getWebDriver()

    WebElement Table = driver.findElement(By.xpath('//div[@id=\'missedCallsContainer\']'))

    List<WebElement> rows_table = Table.findElements(By.xpath('//tr[@class=\'dl-menu\']'))

    int rows_count = rows_table.size()

    println('No. of rows: ' + rows_count)
} 

else {
    WebUI.click(findTestObject('123/Page_Operator/click_phone'))
}

如果代码进入 if 语句它会工作,但如果我在 if 语句中提供的文本不存在,它会停止工作并且不会转到 else。

documentation see driverfactory.

Katalon developers were aware that WebDriver methods may be useful for users, so they introduced the DriverFactory library. This library is responsible for manipulation with the WebDriver instance and offers a few useful methods for using WebDriver in Katalon.

One of the biggest benefits which are brought by this library is the changeWebDriver() method. As I mentioned earlier, it is not possible to use Katalon keywords with custom WebDriver instances by default. But as soon as you call this method, you’ll be able to use them. Katalon will set your driver as a default one, and the full Katalon’s functionality is provided.

只需添加DriverFactory.changeWebDriver(driver):

WebUI.openBrowser('')

WebUI.navigateToUrl('123/account/login?ReturnUrl=%2F')

WebUI.maximizeWindow()

WebUI.setText(findTestObject('123/Page_Log in/input_ _Username'), 'admin')

WebUI.setEncryptedText(findTestObject('123/Page_Log in/input_ _Password'), 'admin')

not_run: WebUI.verifyElementPresent(findTestObject('123/Page_Log in/span_Log in'), 1)

WebUI.click(findTestObject('123/Page_Log in/span_Log in'))

not_run: WebUI.verifyElementPresent(findTestObject('123/Page_Operator/button_To activate session sta'), 1)

WebUI.click(findTestObject('123/Page_Operator/button_To activate session sta'))

if (WebUI.verifyTextPresent("Operator already has active session", true)) {
    WebUI.click(findTestObject('if-else/Page_Operator/button_Clear'))

    WebUI.click(findTestObject('if-else/Page_Operator/button_To activate session sta'))

    WebUI.waitForPageLoad(5)

    WebUI.click(findTestObject('123/Page_Operator/click_phone'))

    WebDriver driver = DriverFactory.getWebDriver()
    
    DriverFactory.changeWebDriver(driver)

    WebElement Table = driver.findElement(By.xpath('//div[@id=\'missedCallsContainer\']'))

    List<WebElement> rows_table = Table.findElements(By.xpath('//tr[@class=\'dl-menu\']'))

    int rows_count = rows_table.size()

    println('No. of rows: ' + rows_count)
} 

else {
    WebUI.click(findTestObject('123/Page_Operator/click_phone'))
}

你的脚本很好,我认为你只需要在 if 语句处稍微改变一下。 对于 if 语句,我认为您需要使用另一个版本的 verifyTextPresent,如下所示,还有一个参数 FailureHandling.OPTIONAL

可以在 https://docs.katalon.com/katalon-studio/tutorials/how_to_check_element_status_in_conditional_statement.html

找到更多信息