I have a test case that is being called from 2 different test suites. I would like to be able to create a data file, with a query that looks like this…
Select * FROM my_database_table WHERE test_suite = ${GlobalVariable.Test_Suite}
where the test_suite value would be replaced with whatever the name of the current test Suite is. It appears that datafiles are read as strings, so the globalVariable is not replaced with the appropriate value from my profile.
has anyone found a work-around for this? Is it possible to use a global variable in a data file query?
@dpackwood I may not understand your question completely, but the query is just a string so you can concatenate any part of your query to your heart’s content.
def query = "Select * FROM my_database_table WHERE " + GV.gTestSuite + " = " + ${GlobalVariable.Test_Suite}
println query
Are you setting your GV in your TestSuite’s @SetupTestCase method?
@grylion54 thanks for the reply. I’m talking specifically about creating a new ‘Test Data’ File in Katalon Studio. We are creating a database type file, and it asks for an SQL query. Trying to add any “code” values results in an incorrect sql syntax error
We are trying to determine if we can use a global variable in the query text, to create a dynamic query that would pull data based on the test suite name.
If you use GV, you may have to add “toString()” at the end as the GV may be treated as an Object but you want it as a String.
Example:
GlobalVariable.gHostName.toString()
And if you have an Integer code value, you would need to put quotes around it within the SQL clause (put a single quote just before the double quote and again after your code put double quote and then a single quote to enclose the code value).
@dpackwood please advise if the solution given by @grylion54 has worked, since I’m facing the same issue, but I have created data files for each table in the db, then the global variable is reading from these data files table, but in the test case when I want to calculate for example globalVariable A (0.5)+ globalVariable B (11.0), the numbers are being added to each other as if they are strings ex the results that I am getting is 0.511.0, and the numbers are not being calculated properly
You can change a String that contains a number into a number that you can do math with by conversion. In your case, you want a “double”. A Double is a number with a decimal point.
def dnum = Double.parseDouble(GlobalVariable.A) + Double.parseDouble(GlobalVariable.B);
println("the result is " + dnum.toString());
Note: An Integer is a number that does not have a decimal point. You can convert a String into an Integer as well, however, you need another method–Integer.parseInt(str).