【问题标题】:Configuring If-else Statement in Katalon Studio在 Katalon Studio 中配置 If-else 语句
【发布时间】:2019-05-23 16:32:44
【问题描述】:

我正在尝试配置 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。

【问题讨论】:

    标签: selenium testing automated-tests katalon-studio


    【解决方案1】:

    您的脚本很好,我认为您只需要在 if 语句处稍作改动即可。 对于 if 语句,我认为您需要使用另一个版本的 verifyTextPresent,如下所示,再加一个参数 FailureHandling.OPTIONAL

    更多信息请访问https://docs.katalon.com/katalon-studio/tutorials/how_to_check_element_status_in_conditional_statement.html

    【讨论】:

    • 您好,我添加了 FailureHandling.OPTIONAL,它实际上帮助我将广告转到 else 语句,但程序说有一个警告使该测试用例出现,是否有可能避免这种情况?跨度>
    • 没有FailureHandling有什么办法可以做到吗?
    • 警告是可以的,因为它有助于知道文本不存在。如果您不想使用它,您可以将 verifyTextPresent 与 if 语句分开,并使用 Try-Catch 手动捕获异常。在 Katalon Studio 中使用 FailureHandling 是一种很好的做法。
    【解决方案2】:

    documentation see driverfactory

    Katalon 开发人员意识到 WebDriver 方法可能对用户有用,因此他们引入了 DriverFactory 库。这个库负责操作 WebDriver 实例,并提供了一些在 Katalon 中使用 WebDriver 的有用方法。

    这个库带来的最大好处之一是 changeWebDriver() 方法。正如我之前提到的,默认情况下无法将 Katalon 关键字与自定义 WebDriver 实例一起使用。但是只要你调用了这个方法,你就可以使用它们了。 Katalon 会将您的驱动程序设置为默认驱动程序,并提供 Katalon 的全部功能。

    只需添加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'))
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多