首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >动态生成GlobalVariable

动态生成GlobalVariable
EN

Stack Overflow用户
提问于 2019-03-21 17:58:56
回答 1查看 930关注 0票数 1

我曾尝试在https://docs.katalon.com/katalon-studio/docs/create-global-variables-on-the-fly.html中动态创建一个GlobalVariable。

关键字定义是,

代码语言:javascript
复制
public class Helper {

@Keyword
void addGlobalVariable(String name, def value) {
  GroovyShell shell1 = new GroovyShell()
  MetaClass mc = shell1.evaluate("internal.GlobalVariable").metaClass
  String getterName = "get" + name.capitalize()
  mc.'static'."$getterName" = { -> return value }
  mc.'static'."$name" = value
}

脚本代码是,

CustomKeywords.'Helper.addGlobalVariable'('localURL','katalon.com') println GlobalVariable.localURL

我收到以下错误

代码语言:javascript
复制
2019-03-21 14:56:24.433 [39mDEBUG[0;39m [36mify if an contract can be deleted -[0;39m [39m1: Helper.addGlobalVariable("localURL", "katalon.com")[0;39m
internal.GlobalVariable
2019-03-21 14:56:24.659 [34mINFO [0;39m [36mk.k.c.m.CustomKeywordDelegatingMetaClass -[0;39m [39mHelper.addGlobalVariable is PASSED[0;39m
2019-03-21 14:56:24.659 [39mDEBUG[0;39m [36mify if an author contract can be deleted -[0;39m [39m2: println(BaseURL)[0;39m
http://dev.dewdropsbff.zycus.net/api
2019-03-21 14:56:24.662 [39mDEBUG[0;39m [36mify if an contract can be deleted -[0;39m [39m3: println(localURL)[0;39m
2019-03-21 14:56:24.671 [1;31mERROR[0;39m [36mc.k.katalon.core.main.TestCaseExecutor   -[0;39m [31m❌ println(localURL) FAILED.[0;39m
[31mReason:[0;39m
[31mgroovy.lang.MissingPropertyException: No such property: localURL for class: internal.GlobalVariable[0;39m
[31m    at Verify if an author contract can be deleted.run(Verify if an author contract can be deleted:23)[0;39m
[31m    at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)[0;39m
[31m    at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)[0;39m
[31m    at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:331)[0;39m
[31m    at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:322)[0;39m
[31m    at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:301)[0;39m
[31m    at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:293)[0;39m
[31m    at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:227)[0;39m
[31m    at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)[0;39m
[31m    at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:105)[0;39m
[31m    at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)[0;39m
[31m    at TempTestCase1553160381933.run(TempTestCase1553160381933.groovy:21)

请让我知道如何解决这个问题。谢谢。

EN

回答 1

Stack Overflow用户

发布于 2020-02-11 23:43:04

您只需在同一执行中创建GlobalVariables即可。换句话说,如果您像这样创建一个GlobalVariable,那么您可以在创建它的测试用例中使用它,或者在执行它的测试套件的下一个测试用例中使用它(显然是执行整个测试套件,而不仅仅是创建GlobalVariable的测试用例)。

实际上,这不是一个永久创建或编辑GlobalVariable的选项。

你能做的,就是创建一个文件并保存在你想要的任何地方,然后在你想要的任何地方恢复信息。

这里有一个示例: TestScript1。

代码语言:javascript
复制
    import java.nio.file.Files
    import java.nio.file.Path
    import java.nio.file.Paths

    import com.kms.katalon.core.configuration.RunConfiguration
    import com.kms.katalon.core.testobject.ConditionType
    import com.kms.katalon.core.testobject.TestObject as 
    import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

    // parameters
    def URL = "https://www.google.com/search?q=katalon"
    def testObject = new TestObject().addProperty("xpath", "//div[@id='resultStats']")

    // create directory to locate a temporary file
    Path projectDir = Paths.get(RunConfiguration.getProjectDir())
    Path tmpDir = projectDir.resolve('tmp')
    if (!Files.exists(tmpDir)) {
        Files.createDirectory(tmpDir)
    }

    // Prepare File object 
    File dataFile = tmpDir.resolve('data.txt').toFile()

    // open a web page
    WebUI.openBrowser('')
    WebUI.navigateToUrl(URL)
    WebUI.verifyElementPresent(testObject, 10)

    // retrieve a text labeled "result stats" from the Google Search Result page 
    String value = WebUI.getText(testObject).trim()

    // save the text into a file under <project dir>/tmp directory
    dataFile.text = value

    WebUI.closeBrowser()

TestScript2

代码语言:javascript
复制
    import java.nio.file.Files
    import java.nio.file.Path
    import java.nio.file.Paths

    import com.kms.katalon.core.configuration.RunConfiguration
    import com.kms.katalon.core.testobject.ConditionType
    import com.kms.katalon.core.testobject.TestObject as TestObject
    import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

    // parameters
    def URL = "https://www.google.com/search?q=katalon"
    def testObject = new TestObject().addProperty("xpath", ConditionType.EQUALS,         "//div[@id='resultStats']")

    // create directory to locate a temporary file
    Path projectDir = Paths.get(RunConfiguration.getProjectDir())
    Path tmpDir = projectDir.resolve('
    if (!Files.exists(tmpDir)) {
        Files.createDirectory(tmpDir)
    }

    // read the file to retrieve the data prepared by TestScript1
    File dataFile = tmpDir.resolve('data.txt').toFile()
    String previousValue = dataFile.text.trim()

    // open a web page
    WebUI.openBrowser('')
    WebUI.navigateToUrl(URL)
    WebUI.verifyElementPresent(testObject, 10)

    // verify if the same value as the data is displayed in the web page

    //WebUI.verifyTextPresent(value, false)        // this line often fails with no         meaningful diagnostics 

    def currentValue = WebUI.getText(testObject).trim()
    assert currentValue == previousValue

    WebUI.closeBrowser()

我从这里获取了这些信息(我把它粘贴在这里,以防将来URL会被破坏):

https://forum.katalon.com/t/change-a-global-variable-value-permanently/18115/15

我希望它能有所帮助,尽管我回答得太晚了。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55277804

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档