I have then created a Web Test MyTenTests which is made of 10 Test as Step scripts.
I have bound a datasource (an XML file) with Global Values to MyTenTests test
I want to extract the Global values and store them in MyGlobalVariables.variable1, MyGlobalVariables.variable2, .. MyGlobalVariables.variable10 for use in the whole project.
The idea is to set the values in the XML file once per project and make the values available throughout the project.
Ideally this would be done for a Project or TestList but that is not currently an option so I am using Test as Step as a workaround.
Note the class is within the namespace not the individual tests.
Declaration
public static class MyGlobalVariables
{
// Set project specific global variables
public static string MYURL= (string) Data["MYURL"];
public static string MYToolsReminders= (string)Data["MYToolsReminders"];
public static string MYCountVisitorsToURL= (string)Data["MYCountVisitorsToURL"];
}
Usage
The error mesaage is : The name 'Data' does not exist in the current context
8 Answers, 1 is accepted
Add a static class in the name space
public static class HLGlobalVar
{
HLGlobalVar.HLURL=(string)Data["HLURL"];
HLGlobalVar.HLHomeID=(string)Data["HLHomeID"];
HLGlobalVar.HLOwnerId=(string)Data["HLOwnerId"];
HLAdvertiserId=(string)Data["HHLAdvertiserId"];
HLGlobalVar.HLCheckCountURL=(string)Data["HLCheckCountURL"];
HLGlobalVar.HLToolsReminders=(string)Data["HLToolsReminders"];
HLGlobalVar.HLAgentHomesConversion=(string)Data["HLAgentHomesConversion"];
HLGlobalVar.HLRemoteServiceURL=(string)Data["HLRemoteServiceURL"];
HLGlobalVar.HLWebService=(string)Data["HLWebService"];
HLGlobalVar.HLRAUS=(string)Data["HLRAUS"];
HLGlobalVar.HLRTUS=HLWebService + @"/service_rates.asmx";
// Database
HLGlobalVar.HLDBDataSource= (string)Data["HLDBDataSource"];
HLGlobalVar.HLDBUsername= (string)Data["HLDBUsername"];
HLGlobalVar.HLDBPassword= (string)Data["HLDBPassword"];
HLGlobalVar.HLDBDatabase= (string)Data["HLDBDatabase"];
// Check Count login creddentials
HLGlobalVar.HLCheckCountUsername= (string)Data["HLCheckCountUsername"];
HLGlobalVar.HLCheckCountPassword= (string)Data["HLCheckCountPassword"];
}
Step 2
I have created an empty web test and added an XML data file and Bound the data file to the test.
I then added an OnBeforeTestStarted method as below
public override void OnBeforeTestStarted()
{
HLGlobalVar.HLURL=(string)Data["HLURL"];
HLGlobalVar.HLHomeID=(string)Data["HLHomeID"];
HLGlobalVar.HLOwnerId=(string)Data["HLOwnerId"];
HLAdvertiserId=(string)Data["HHLAdvertiserId"];
HLGlobalVar.HLCheckCountURL=(string)Data["HLCheckCountURL"];
HLGlobalVar.HLToolsReminders=(string)Data["HLToolsReminders"];
HLGlobalVar.HLAgentHomesConversion=(string)Data["HLAgentHomesConversion"];
HLGlobalVar.HLRemoteServiceURL=(string)Data["HLRemoteServiceURL"];
HLGlobalVar.HLWebService=(string)Data["HLWebService"];
HLGlobalVar.HLRAUS=(string)Data["HLRAUS"];
HLGlobalVar.HLRTUS=HLWebService + @"/service_rates.asmx";
// Database
HLGlobalVar.HLDBDataSource= (string)Data["HLDBDataSource"];
HLGlobalVar.HLDBUsername= (string)Data["HLDBUsername"];
HLGlobalVar.HLDBPassword= (string)Data["HLDBPassword"];
HLGlobalVar.HLDBDatabase= (string)Data["HLDBDatabase"];
// Check Count login creddentials
HLGlobalVar.HLCheckCountUsername= (string)Data["HLCheckCountUsername"];
HLGlobalVar.HLCheckCountPassword= (string)Data["HLCheckCountPassword"];
}
Step 3
I should be able to reference my global variables from the tests. Unfortunately, every reference to the Global Var
Yippee It is working!!
It seems you have found the method OnBeforeTestStarted(). Is your issue solved now, do you have a running test? I am making that conclusion by your statement at the end of your second post:
"Yippee It is working!!"
Please let me know if you need any additional assistance.
Ivaylo
the Telerik team
Vote now
Hi Ivaylo,
My work-a-round is working but it is a bit painful especially it is to be maintained by non-developers.
I think it would be a very useful feature request.
FEATURE REQUEST
1. The ability to attach a data source to a project and thus making it available to every test within the project.
2. It would remove the need for creating global variables in code.
3. Ensure running multiple test lists would not be an issue (would not cause conflict about which test or coded step holds the Global variables).
Regards,
Asare
Yes you can attach a data source to a project and make it available for every test within the project but you will need to data bind each test to the data source. Please check this article for more information.
All the best,Ivaylo
the Telerik team
Vote now
I want to set values at the beginning of a project and then use them throughout all the tests.
Please excuse me for the misunderstanding.
Actually I missed to share that instead of overriding you can build your own custom DLL containing the method OnBeforeTestStarted() that can be shared by multiple projects/tests. Here is an article I found on how to create your custom DLL. You can include all the methods you would like to share in this custom DLL and just add it as assembly to the project in which you would like it to be used.
Here is our article on how to add an assembly reference.
Ivaylo
the Telerik team
Vote now
It was a helpful post and I was able to load global varibales, using the public static class.
Now, my question is, When I update any of these variables in test1, it is not used as updated value when running test2. ( Running Quick Execution, Every time the runtime loads the class with default values).
Is there any work around for this purpose?
This is actually an expected behavior. Once the Quick Execution or test list execution is over, the next time you run the test (or the test list), it will use again the default settings that are stored in the dll file.
However, there is an easy workaround. You just need to use test list and make sure that test1 will be executed before test2.
Looking forward to hearing from you.
Velin Koychev
Telerik