This is a migrated thread and some comments may be shown as answers.

NullReferenceException & App.config

16 Answers 1322 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Miroslav
Top achievements
Rank 1
Miroslav asked on 10 Aug 2010, 02:32 PM

Hello
I use VS 2010 with WebUI Test Studio plugin (Windows 7 Enteprise). I can't access App.config from CodeBehind of a WebAii test.
Here is how the CodeStep looks like:

[CodedStep("DbClear")]
public void DbClear()
{
SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
 
string UserId = ConfigurationManager.AppSettings["UserId"];
StringBuilder cmtxt = new StringBuilder();
cmtxt.Append(String.Format("delete from dbo.tblChain where CreatedBy = '{0}'", UserId));
cmtxt.Append(String.Format("delete from dbo.tblCustomerBase where CreatedBy = '{0}'", UserId));
  
SqlCommand cm = new SqlCommand(cmtxt.ToString(), cn);
  
cn.Open();
cm.ExecuteNonQuery();
cn.Close();
}

At the first line I get:
ArtOfTest.Common.Design.Exceptions.ExecutionException: Exception thrown executing coded step: '[DbClear] : DbClear'. ---> System.NullReferenceException: Object reference not set to an instance of an object.

When I use same code within a TestMethod of a standard UnitTest.cs file in the same project it works fine.

Here is how App.config of the project looks like:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="LoginPageUrl" value="http://server:1133/CRMProduction/LoginPage.aspx" />
    <add key="LoginName" value="WebUI" />
    <add key ="Pass" value ="dgtrd444" />
    <add key="UserId" value ="3EA30CD5-DD0E-4004-9F81-9C3FB1CBB3CA" />
  </appSettings>
  <connectionStrings>
    <add name="ConnectionString" connectionString="Data Source=sql_server; Initial Catalog=vitana_db_production; USER ID= VitanaUser; Password= dgtrd333; Min Pool Size=5" providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>

Thanks for the help.

16 Answers, 1 is accepted

Sort by
0
Chris Johnson
Top achievements
Rank 1
answered on 10 Aug 2010, 07:25 PM
Miroslav,

I had the same problem with VS2008, so I wrote my own code to read app.config. I created a class called AppSettings that has methods for all my settings, and dips into the config file. Details like whether you approve of using static classes aside, it works. (Of course I didn't like having to write this code at all, so if anyone has found how to read settings the usual way I'd love to hear it!)


public static class AppSettings
{
    #region Setting accessors 

    public static string SomeSetting
    {
        get { return GetSetting("SomeSetting"); }
    }

    // etc.

    #endregion

    #region Implementation

    // Not this easy with WebAii tests.
    //private static string GetSetting(string name)
    //{
    //    return ConfigurationManager.AppSettings[name];
    //}
 
    private static XmlDocument settingsXml;

    static AppSettings()
    {
        settingsXml = new XmlDocument();
        settingsXml.Load(GetSettingsFilePath());
    }

    private static string GetSettingsFilePath()
    {
        // return Path.Combine(Environment.CurrentDirectory, "FunctionalTests.dll.config");
        string testRunFolder = Path.GetDirectoryName(Assembly.GetAssembly(typeof(AppSettings)).CodeBase);
        Uri uri = new Uri(testRunFolder);
        testRunFolder = uri.LocalPath;
        return Path.Combine(testRunFolder, "FunctionalTests.dll.config");
    }

    private static string GetSetting(string name)
    {
        string xpath = string.Format("configuration/appSettings/add[@key='{0}']/@value", name);
        return settingsXml.SelectSingleNode(xpath).Value;
    }

    #endregion
}

Chris
0
Cody
Telerik team
answered on 10 Aug 2010, 08:53 PM
Hi Chris Johnson,

You are correct. If you run the test from Test View you actually get the configuration from QTAgent32.exe (or QTAgen.exe on 64 bit machines). I can offer a few alternatives:

  1. How about using a data driven test instead? Why do you prefer to use app.config? With a data driven test you can pull data from a CSV file, an Excel file, an XML file, or a SQL database and then bind properties of test steps directly to the data source. No code involved at all.
  2. Use the Settings file that is a part of your Visual Studio project. The settings get compiled directly into your .dll instead of using a separate external config file. Code would then resemble this:
    string connection = TestProject1.Properties.Settings.Default.NorthwindConnectionString;
  3. You can write code to force read your config file. The code goes like this:
    Assembly me = Assembly.GetExecutingAssembly();
    Configuration config = ConfigurationManager.OpenExeConfiguration(System.IO.Path.Combine(this.ExecutionContext.DeploymentDirectory,
        me.ManifestModule.Name));
    string url = config.AppSettings.Settings["LoginPageUrl"].Value;
    string name = config.AppSettings.Settings["LoginName"].Value;
    string pw = config.AppSettings.Settings["Pass"].Value;
    string UserId = config.AppSettings.Settings["UserId"].Value;
    string conn = config.ConnectionStrings.ConnectionStrings["ConnectionString"].ConnectionString;

I have gotten all three methods to work. I'd be interested to hear from you which appeals to you most and why.

Greetings,
Cody
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Chris Johnson
Top achievements
Rank 1
answered on 11 Aug 2010, 03:24 PM
Hello Cody -

Thanks for the information, that is very helpful. Here are my thoughts on your three alternatives:

1. Data driven tests: sounds like a great way to go for standard .aii tests that don't use a lot of code behind. But we convert most of our tests to code, and use the .aii files mostly as element referencers, to maintain the element repository. I know that's probably not the conventional use of the product, but I find .aii steps too limiting in terms of test reuse. I want everything in code for the highest level of reuse and flexibility.

2. Using settings embedded in the DLL: I don't like this one so much, because it makes it hard to change settings and re-run the tests without rebuilding the project.

3. Use the Configuration class: I like this one. If I knew about it beforehand, I would have done this instead of rolling my own.

Well that's four ways to do it, if you count mine. (Which I don't recommend, given #3.) That should be enough for anyone! Thanks again Cody.

Chris
0
Cody
Telerik team
answered on 11 Aug 2010, 04:00 PM
Hi Chris Johnson,

I see. Thanks for the feedback. Here's another thought for you... since you are converting your test to code you might want to take advantage of Microsoft's data driven unit test feature. http://msdn.microsoft.com/en-us/library/ms182527.aspx

Regards,
Cody
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Miroslav
Top achievements
Rank 1
answered on 11 Aug 2010, 04:53 PM

Hello,
thanks for all replies.
I think I will use data driven tests, but they seem strange. I have created a login test that is used inside a few other data driven tests that are bound to single xml file. The login test uses some values of the inherited data and it works ok.
When I click on customize in code of one of the login steps, the genereted code looks like this:

Pages.VitanaCRM.TbLoginNameText.Text = ((string)(System.Convert.ChangeType(Data["LoginName"], typeof(string))));

but it doesnt work. It gives error:

System.NullReferenceException: Object reference not set to an instance of an object.
   at ArtOfTest.WebAii.Design.BaseWebAiiTest.TestData.get_Item(String column)

I actually dont need to customize the step now but it is strange when a tool generates code that doesnt work.

0
Cody
Telerik team
answered on 11 Aug 2010, 05:39 PM
Hello Miroslav,

I am sorry you have run into this problem. Are you using our build 2010.2.713? We recently fixed a bug in this area. The fix was included in our latest internal build, 2010.2.806. You may want to give this new version a try.

Best wishes,
Cody
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Balaram
Top achievements
Rank 1
answered on 18 Jun 2012, 12:59 PM
Hi Cody,
I am stuck in almost simliar problem. Am trying to read app.config through code.

Am using similar App.config in the VS2010 project.

<?xml version="1.0" encoding="utf-8" ?> 
<configuration
  <appSettings
    <add key="LoginPageUrl" value="http://server:1133/CRMProduction/LoginPage.aspx" /> 
    <add key="LoginName" value="WebUI" /> 
    <add key ="Pass" value ="dgtrd444" /> 
    <add key="UserId" value ="3EA30CD5-DD0E-4004-9F81-9C3FB1CBB3CA" /> 
  </appSettings
 
</configuration>
As suggested by you am trying to force read the config file as below.
Assembly me = Assembly.GetExecutingAssembly(); 
Configuration config = ConfigurationManager.OpenExeConfiguration(System.IO.Path.Combine(this.ExecutionContext.DeploymentDirectory, 
    me.ManifestModule.Name)); 
string url = config.AppSettings.Settings["LoginPageUrl"].Value; 
string name = config.AppSettings.Settings["LoginName"].Value; 
string pw = config.AppSettings.Settings["Pass"].Value; 
string UserId = config.AppSettings.Settings["UserId"].Value; 

I am unable to get the path of the config file and it gives "Object reference not set to an instance of an object." exception.

Could you please help.

Regards,
Balaram
0
Plamen
Telerik team
answered on 21 Jun 2012, 01:52 PM
Hi Balaram,

In order to make this to work you need to enable Deployment as seen in this article and add all items from the Bin folder. I recorded a short video demonstrating how to overcome the issue.

Regards,
Plamen
the Telerik team
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
0
Monica
Top achievements
Rank 1
answered on 17 Jul 2013, 03:19 PM
Hi,

I'm having the same Object Null error.
I added the app.config file on the VS2012, but I'm running the test on Test Studio, a coded step to read the configuration as you describe on the 3rd way.
My project doesn't have a Test Settings file so I wasn't able to follow the Enable Deployment tip.
Can you please explain which is the best way to add a app.config file to tests just running in TestStudio, I have read the link http://www.telerik.com/automated-testing-tools/support/documentation/user-guide/code-samples/general/override-app.config.aspx but I always have the null error.

Thank you.
0
Cody
Telerik team
answered on 17 Jul 2013, 08:41 PM
Hello Monica,

First I'd like to understand why are you trying to use an app.config file? What are you planning on putting in it? There very well could be a better approach. Once we understand what automation challenge you're trying to solve with an app.config file we can advise you better on the best course of action.

Now having said that, Enable Deployment and the Test Settings file only applies when running the tests using either MSTest or from Visual Studio Test View. When running a test in the Test Studio standalone IDE our own test runner is used which does not use the VS Test Settings file or look at any sort of Enable Deployment setting.

Now, let's address the specific problem you are running into, the null reference exception. If you still really want/need to use an app.config file when running tests using our Test Studio standalone IDE, start by

  1. Placing your app.config file in the "bin" folder of the test project
  2. Rename it to match the name of the dll that the test project will generate. For example, if you get a dll named myproject.dll, name your config file myproject.dll.config.
  3. Now use code like this to open and read the values from your config file.
Assembly me = Assembly.GetExecutingAssembly();
Configuration config = ConfigurationManager.OpenExeConfiguration(me.Location);
 
ServiceModelSectionGroup serviceModel = (ServiceModelSectionGroup)config.SectionGroups["system.serviceModel"];
ServiceEndpointElement endpoint= serviceModel.Services.Services[0].Endpoints[0];
 
string url = config.AppSettings.Settings["LoginPageUrl"].Value;
string name = config.AppSettings.Settings["LoginName"].Value;
string pw = config.AppSettings.Settings["Pass"].Value;
string UserId = config.AppSettings.Settings["UserId"].Value;
 
string conn = config.ConnectionStrings.ConnectionStrings["ConnectionString"].ConnectionString;

Attached you will find a complete working test project as a demonstration.

Regards,
Cody
Telerik
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
0
Monica
Top achievements
Rank 1
answered on 18 Jul 2013, 10:53 AM
Hi Cody,

Thanks for your quick response. I did  the changes you suggested, the only thing I had to change on my code was the name and location of the app.config file and now everything is working.

We want to add a configuration file to our test project, to set the connectionstring and the list of testlist we want to validate on OnAfterTestListCompleted, is the app.config file the best way?

Thank you.
Mónica

0
Cody
Telerik team
answered on 18 Jul 2013, 08:44 PM
Hello Mónica,

Thank you for the additional information. OnAfterTestListCompleted only exists in our test extension API, which means you're creating your own DLL as an extension. An app.config file is the best approach in this scenario.

Where will the connection string be used? In the code of a test, or in the code of the extension? For setting the connection string to be used by one specific test you could put it in one of the custom properties. In code you get access to this value this way:

this.ExecutionContext.Test.CustomProperty1;

Or make the test a data driven test and put the connection string into one column of the data source. Othereise an app.config is a good solution for project wide configuration.

Regards,
Cody
Telerik
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
0
Monica
Top achievements
Rank 1
answered on 24 Jul 2013, 02:20 PM
Hello Cody,

I'm using the config file as described on our last messages, settings are now available for all our test and in test extensions.
Thanks for the tip to set it on a CustomProperty, in the future it will be useful for other projects.

Mónica
0
Cody
Telerik team
answered on 24 Jul 2013, 02:30 PM
Hello Mónica,

You are entirely welcome and thank you for the update.

Regards,
Cody
Telerik
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
0
aplicaciones
Top achievements
Rank 1
answered on 08 Feb 2018, 08:50 AM

Hello,

I know that this post is rather old but I am struggling with this.
The alternative 3 provided by Cody works fine as long as I run the test on my "local environment" but whenever i run this test "remotely" my config file has no settings at all.
I can physically see (through the FileSystem) the file being deployed and I see that it has the proper configuration.
Whenever I ask for a test to run Remotely  I got a directory created and deployed in a path like this:

C:\Users\<someUserName>\AppData\Local\Temp\Projects\2653928a-3ffc-4787-bdb4-2dd64dd4212b\<ProjectName>
If I go there the app.config exists but the code is not able to fetch the configuration.
I am probably missing something here or doing something wrong.

I hope you can help me.
Regards,
Sebastian

0
Elena
Telerik team
answered on 15 Feb 2018, 11:36 AM
Hi Sebastian,

Thank you for reaching us out. 

As you are referring to an older post which contains plenty of information and advice I would like to ask for some further details on your end. Please create a sample project which a sample config file to demonstrate the exact issue you are experiencing. It will be helpful to know what code do you use to call that file and fetch the required settings. 

Also I would recommend to submit a support ticket on that topic to get the advantage of a licensed user and get a faster response. 

Thank you for your understanding and cooperation! 

Regards,
Elena Tsvetkova
Progress Telerik
 
Quickly become an expert in Test Studio, check out our new training sessions!
Test Studio Trainings
 
Tags
General Discussions
Asked by
Miroslav
Top achievements
Rank 1
Answers by
Chris Johnson
Top achievements
Rank 1
Cody
Telerik team
Miroslav
Top achievements
Rank 1
Balaram
Top achievements
Rank 1
Plamen
Telerik team
Monica
Top achievements
Rank 1
aplicaciones
Top achievements
Rank 1
Elena
Telerik team
Share this question
or