NullReferenceException & App.config

15 posts, 0 answers
  1. Miroslav
    Miroslav avatar
    2 posts
    Member since:
    Aug 2010

    Posted 10 Aug 2010 Link to this post

    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.
  2. Chris Johnson
    Chris Johnson avatar
    20 posts
    Member since:
    Jan 2010

    Posted 10 Aug 2010 Link to this post

    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
  3. Cody
    Admin
    Cody avatar
    3360 posts

    Posted 10 Aug 2010 Link to this post

    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
  4. Chris Johnson
    Chris Johnson avatar
    20 posts
    Member since:
    Jan 2010

    Posted 11 Aug 2010 Link to this post

    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
  5. Cody
    Admin
    Cody avatar
    3360 posts

    Posted 11 Aug 2010 Link to this post

    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
  6. Miroslav
    Miroslav avatar
    2 posts
    Member since:
    Aug 2010

    Posted 11 Aug 2010 Link to this post

    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.

  7. Cody
    Admin
    Cody avatar
    3360 posts

    Posted 11 Aug 2010 Link to this post

    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
  8. Balaram
    Balaram avatar
    2 posts
    Member since:
    May 2012

    Posted 18 Jun 2012 Link to this post

    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
  9. Plamen
    Admin
    Plamen avatar
    8 posts

    Posted 21 Jun 2012 Link to this post

    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
  10. Monica
    Monica avatar
    5 posts
    Member since:
    Jun 2013

    Posted 17 Jul 2013 Link to this post

    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.
  11. Cody
    Admin
    Cody avatar
    3360 posts

    Posted 17 Jul 2013 Link to this post

    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
  12. Monica
    Monica avatar
    5 posts
    Member since:
    Jun 2013

    Posted 18 Jul 2013 Link to this post

    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

  13. Cody
    Admin
    Cody avatar
    3360 posts

    Posted 18 Jul 2013 Link to this post

    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
  14. Monica
    Monica avatar
    5 posts
    Member since:
    Jun 2013

    Posted 24 Jul 2013 Link to this post

    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
  15. Cody
    Admin
    Cody avatar
    3360 posts

    Posted 24 Jul 2013 Link to this post

    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
Back to Top
  Cast Your Vote at DevPro Connections! Cast Your Vote at  Win IT Pro Connections!