我尝试在https://docs.katalon.com/katalon-studio/docs/create-global-variables-on-the-the-the-the-phariables-the-fly中动态创建一个全局变量。.html。
关键字定义是
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)
请让我知道如何解决此问题。谢谢。
您可以在同一执行中创建GlobalVariables。换句话说,如果您创建这样的全球变量,则可以在创建它的测试用例中或在执行它的下一个测试套件的测试用例中使用它(显然是执行整个测试套件,而不是执行只是创建全局变量的测试案例)。
实际上,这不是永久创建或编辑全局变量的选项。
您能做的就是创建一个文件并保存在此处,然后随后恢复您想要的信息。
在这里您有一个示例: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-value-permanance/18115/15
我希望它能有所帮助,尽管我回答为时已晚。