我尝试按照https://docs.katalon.com/katalon-studio/docs/create-global-variables-on-the-fly.html中的帖子动态创建GlobalVariable。
关键字定义为
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
我遇到以下错误
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)
请让我知道如何解决此问题。谢谢。
答案 0 :(得分:1)
您可以在同一执行中创建GlobalVariables。换句话说,如果您创建这样的GlobalVariable,则可以在创建它的测试用例中使用它,也可以在执行它的测试套件的下一个测试用例中使用它(显然是执行整个测试套件,而不是只是创建GlobalVariable的测试用例。
实际上,不能永久创建或编辑GlobalVariable。
您可以做的是创建一个文件并将其保存在所需的位置,然后在任何需要的位置恢复信息。
这里有一个例子: TestScript1。
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
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
尽管我回答太晚了,但我希望它能有所帮助。