首页 > 解决方案 > 如何使用关键字验证页面上的文本?

问题描述

我正在尝试验证微调器完成加载后出现的页面上的一些文本。是否可以在 Katalon 中执行此类测试?

我尝试创建一个关键字然后使用它,但不知道下一步该做什么。

public class ModuleKeywords {


    @Keyword
    def myText(String){
    myText = WebUI.getText(findTestObject('Object Repository/WMS/Page_/li_System Activity'))
    return myText
    }

}
def sysText = CustomKeywords.'com.wms.modules.general.ModuleKeywords.myText'()

    if (sysText == 'System Activity') {

    println("The text displayed is = "  + sysText)

}
    else
    println('Activity page did not load')

我希望代码能够在单击链接并完成加载微调器后在页面上找到并验证文本“系统活动”。

运行脚本后出现以下错误:2019-09-10 13:52:42.921 错误 kkcmCustomKeywordDelegatingMetaClass - ❌ 没有此类属性:myText for class: com.wms.modules.general.ModuleKeywords 2019-09-10 13:52: 42.925 错误 ckkatalon.core.main.TestCaseExecutor - ❌ 测试用例/回归/WMS/C16320 - 活动模块失败。原因:groovy.lang.MissingPropertyException:没有这样的属性:类的 myText:com.wms.modules.general.ModuleKeywords.myText(ModuleKeywords.groovy:26) 在 com.wms.modules 的 com.wms.modules.general.ModuleKeywords .general.ModuleKeywords.invokeMethod(ModuleKeywords.groovy) 在 com.kms.katalon.core.main.CustomKeywordDelegatingMetaClass.invokeStaticMethod(CustomKeywordDelegatingMetaClass.java:50) 在 C16320 - Activity Module.run(C16320 - Activity Module:23) 在 com。 kms.katalon.core.main.ScriptEngine。

标签: katalon-studio

解决方案


There is some confusion on what myText means. It is unclear if it is a method name or a property name.

So, I suggest you change the name of the text variable (I'll name it just text) inside of your method. Be sure to add the def keyword, as well.

Finally, when writing a method with this signature def myText(String) you are saying the method accepts a string parameter, but you are not providing the parameter in your example so it is not needed.

Your method will then look like this:

@Keyword
def myText(){
    def text = WebUI.getText(findTestObject('Object Repository/WMS/Page_/li_System Activity'))
    return text
}

推荐阅读