Forum Discussion

Redders's avatar
Redders
New Contributor
6 years ago
Solved

Groovy - setPropertyValue for a value in a file and using it in the same script

Hi folks,   I've been trying to read values from a CSV via a script and use them within the same script to login to a db.   I've got the db login and SQL query running when hard coded the DB logi...
  • JHunt's avatar
    6 years ago

    At this line, DBServer isn't among your Groovy variables.

     

    String db_server = "{$DBServer}"

    Instead, DBServer was the name of the property set on the Project level. You can just get it in the same way that you set it, as in...

     

    String db_server = testRunner.testCase.testSuite.project.getPropertyValue("DBServer")

    or use this alternative:

     

    String db_server = context.expand('${#Project#DBServer}')

    However, I'm not sure that your users would appreciate their credentials being set into the SoapUI project file that they might save and share with others later. Instead, just keep it in your scripts. If it's all in the one script, just create the variables directly:

     

    db_server = propData[0]
    db_database = propData[1]
    db_port = propData[2]
    db_user = propData[3]
    db_password = propData[4] String connectionUrl = "jdbc:sqlserver://" + db_server + ":" + db_port + ";database=" + db_database + ";user=" + db_user + ";password=" + db_password;

    Or, if its in a different script (but still in the same test case), you can do essentially the same thing using the test run context, which persists during the test run:

     

    context.db_server = propData[0]
    context.db_database = propData[1]
    context.db_port = propData[2]
    context.db_user = propData[3]
    context.db_password = propData[4] String connectionUrl = "jdbc:sqlserver://" + context.db_server + ":" + context.db_port + ";database=" + context.db_database + ";user=" + context.db_user + ";password=" + context.db_password;