如果语句未在Katalon中满足特定条件

时间:2019-11-05 14:28:36

标签: groovy automation cucumber katalon-studio

我用katalon中的if语句编写了一个满足条件的代码,但是if语句不能满足我的条件。 这是我的代码:

def ElementChecked =
WebUI.verifyElementChecked(findTestObject('Page_/checkbox_BPKB_assignment'),0,FailureHandling.OPTIONAL)
        if (ElementChecked==true){
            println(">>>>>ALREADY CLICKED")
        }
        else{
            println(">>>>>Not clicked yet")
            WebUI.click(findTestObject('Page_/checkbox_BPKB_assignment'))
            WebUI.click(findTestObject('Page_/button_Submit'))
            WebUI.click(findTestObject('Page_/button_OK'))
        }

从上面的代码中,我要检查的元素是单选按钮。因此,如果单选按钮被选中,我会创建一个条件,然后打印(“ >>>>>已经被点击)否则打印(” >>>>>尚未单击“)并进行一些单击。但是,即使条件为真,也总是转到else语句。 谁能帮我这有什么问题吗?

1 个答案:

答案 0 :(得分:1)

如果您基于/未选中某些bsns逻辑,最好使用org.openqa.selenium.WebElement

检查单选按钮状态的示例:

关键字定义:

import org.openqa.selenium.WebDriver
import org.openqa.selenium.WebElement
import com.kms.katalon.core.webui.driver.DriverFactory


public class Q58713480 {

    @Keyword
    def ElementChecked(TestObject testObject) {
        WebDriver driver = DriverFactory.getWebDriver()
        WebElement element = WebUI.findWebElement(testObject, 5)
        if (element.isSelected()) {
            KeywordUtil.logInfo("Element is checked.")
        }
        else {
            KeywordUtil.logInfo("Element is not checked.")
        }
    }
}

测试用例:

WebUI.openBrowser('')
WebUI.maximizeWindow()
WebUI.navigateToUrl("http://www.echoecho.com/htmlforms10.htm")
WebUI.waitForPageLoad(60)

CustomKeywords.'Q58713480.ElementChecked'(findTestObject('radio1'))
WebUI.click(findTestObject('radio1'))
CustomKeywords.'Q58713480.ElementChecked'(findTestObject('radio1'))

WebUI.closeBrowser()

输出:

...
2019-11-14 13:25:14.514 DEBUG testcase.question58713480                - 2: maximizeWindow()
2019-11-14 13:25:15.705 DEBUG testcase.question58713480                - 3: navigateToUrl("http://www.echoecho.com/htmlforms10.htm")
2019-11-14 13:25:18.382 DEBUG testcase.question58713480                - 4: waitForPageLoad(60)
2019-11-14 13:25:18.720 DEBUG testcase.question58713480                - 5: Q58713480.ElementChecked(findTestObject("radio1"))
2019-11-14 13:25:19.298 INFO  com.kms.katalon.core.util.KeywordUtil    - Element is not checked.
2019-11-14 13:25:19.299 INFO  k.k.c.m.CustomKeywordDelegatingMetaClass - Q58713480.ElementChecked is PASSED
2019-11-14 13:25:19.299 DEBUG testcase.question58713480                - 6: click(findTestObject("radio1"))
2019-11-14 13:25:19.568 DEBUG testcase.question58713480                - 7: Q58713480.ElementChecked(findTestObject("radio1"))
2019-11-14 13:25:19.726 INFO  com.kms.katalon.core.util.KeywordUtil    - Element is checked.
2019-11-14 13:25:19.726 INFO  k.k.c.m.CustomKeywordDelegatingMetaClass - Q58713480.ElementChecked is PASSED
2019-11-14 13:25:19.727 DEBUG testcase.question58713480                - 8: closeBrowser()
2019-11-14 13:25:20.702 INFO  c.k.katalon.core.main.TestCaseExecutor   - END Test Cases/question58713480

和“ radio1”测试对象(手动创建): radio1 test object