Rajamanthiram
Rajamanthiram

Reputation: 11

Generating GlobalVariable dynamically

I have tried to create a GlobalVariable dynamically following the post in https://docs.katalon.com/katalon-studio/docs/create-global-variables-on-the-fly.html.

Keyword definition is,

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
}

The script code is,

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

I am getting the following error

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)

Kindly let me know how to fix this. Thanks.

Upvotes: 1

Views: 1492

Answers (1)

You just can create GlobalVariables in the same execution. In other words, if you create a GlobalVariable like this, you just can use it in the test case where you created it, or in the next test cases of the test suite where you are executing it (obviously executing the whole test suite, not just the test case where the GlobalVariable is created).

Actually, it is not an option to create or edit a GlobalVariable permanently.

What you can do, is to create a File and save there whatever you want and later recover the info wherever you want.

Here you have an example: 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()

I took this info from here (I pasted it here in case the URL will be broken in a future):

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

I hope it could help, although I answered too late.

Upvotes: 1

Related Questions