I have a Test Studio Standalone project with a database Data Source. I've written a coded step that inserts test data directly into the database, since this particular data cannot be created directly from the UI with a recorded step. Currently, the DB ConnectionString is hard-coded in this step's code, but I would like it to be dynamic by using the same ConnectionString as the existing Data Source. Is this possible?
I've tried binding the Data Source to the coded step then using Reflection to find the connection strings in the current configuration, but the ConnectionString from the Data Source was not in there. Probably because it only works from an explicit app.config file (in Visual Studio project), which I do not have.
Is there a way to programmatically retrieve the connection string, or do I need to take another approach?
Perhaps my best option is to have a utility class to store connection strings globally accessible to all coded steps? If I do this, is there a way to programmatically retrieve the BaseURL setting for it to determine which connection string is appropriate based on which machine/environment the tests are running in?
Thanks for any input and suggestions.
6 Answers, 1 is accepted
Hi,
There is probably a better way of doing it, but the way i do it is, i put the connection string into its own coded step then extract it and use the extracted variable in any other of the projects steps. something like the below in its own step
string DBConn = @"data source = etc etc";
SetExtractedValue("Conn", DBConn);
then in whichever step you wish to use it, you can call it by:
object myVar = GetExtractedValue("Conn").ToString();
string myVar2 = myVar.ToString();
it is key that the initial coded step is run on whichever test you are doign, if you have a seperate navigate to url step, can put it into there, and know it is run always.
Hope that helps!
Ian
Hello,
@Ian thank you for sharing your knowledge with the community. We appreciate it.
@Clark, does that work for you or you want to check the connection string directly in the code?
Hope to hear from you soon.
Regards,
Telerik
@Ian Thanks for the suggestion. That's similar to what I was thinking I might do with a utility class. At this point, I mostly just need to reuse the values in other coded steps.
@Boyan I would prefer to reuse the connection string from the project's Data Source, if possible. If I cannot do that and must use a utility class instead, I would like to add code to determine the correct connection string based on the project's Base URL (since the test suite could be run against one of several servers).
Is it possible to access the Data Source connection string and/or the project's Base URL from a coded step?
Thanks.
Here is how to obtain the base url in a coded step:
Log.WriteLine(Manager.Settings.Web.BaseUrl.ToString());
and for the connection string:
Log.WriteLine(ExecutionContext.DataContexts.FirstOrDefault().Value.DataInfo.ConnectionString.ToString());
However the connection string is empty on my side. Could you please give that a try on your side if it works. If not I will log a bug report.
Thank you!
Regards,
Boyan Boev
Telerik
Thank you, Boyan.
I am experiencing the same thing as you. The connection string does not exist in the ExecutionContext.DataContexts collection.
Fortunately, I am able to get the BaseUrl from Manager.Settings.Web.BaseUrl, and that helps a great deal. I am now storing my connection strings in a utility class as noted in my original post, and I can use the current BaseUrl to determine the correct connection string I need at runtime. This accomplishes what I need.
Thanks again for your help!
I am happy to hear that.
If you need further assistance please let us know.
Regards,
Boyan Boev
Telerik