Need help in adding new section is app.config in specflow project

462 views
Skip to first unread message

Bharat C

unread,
Jan 25, 2016, 10:11:18 AM1/25/16
to SpecFlow
Hi

I am adding a new section "users" to app.config. I have added config handler to read values from app.config. The problem is I am always getting null when I try to read users. Below are files, please help me if I miss any thing.

Command used to read:
var users = ConfigurationManager.GetSection("Users");

App.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="specFlow" type="TechTalk.SpecFlow.Configuration.ConfigurationSectionHandler, TechTalk.SpecFlow" />
    <section name="users" type="Browser_Feature_Tests.UserConfigHandler, Browser_Feature_Tests" />
  </configSections>
  <specFlow>
    <!-- For additional details on SpecFlow configuration options see http://go.specflow.org/doc-config -->
  <!-- For additional details on SpecFlow configuration options see http://go.specflow.org/doc-config --><unitTestProvider name="NUnit" /></specFlow>
  <users>
    <user usertype="csr" username="abc" password="xxx" />
    <user usertype="admin" username="xyz" password="xxx" />
  </users>
</configuration>


UserConfigHandler.cs

namespace Browser_Feature_Tests
{
    public class UserConfigHandler : ConfigurationSection
    {
        [ConfigurationProperty("users")]
        [ConfigurationCollection(typeof(UserCollection), AddItemName = "user")]
        public UserCollection Users 
        { 
            get { return (UserCollection)base["users"]; }
            set { this["users"] = value; }
        }
    }

    public class UserCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new UserElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((UserElement)element).UserType;
        }
    }

    public class UserElement : ConfigurationElement
    {
        [ConfigurationProperty("usertype", IsRequired=true)]
        public string UserType
        {
            get { return (string)this["usertype"]; }
            set { this["usertype"] = value; }
        }

        [ConfigurationProperty("username", IsRequired = true)]
        public string UserName
        {
            get { return (string)this["username"]; }
            set { this["username"] = value; }
        }

        [ConfigurationProperty("password", IsRequired = true)]
        public string Password
        {
            get { return (string)this["password"]; }
            set { this["password"] = value; }
        }

    }
}


Sam Holder

unread,
Jan 25, 2016, 10:48:27 AM1/25/16
to specflow
How is this related to specflow exactly?

--
You received this message because you are subscribed to the Google Groups "SpecFlow" group.
To unsubscribe from this group and stop receiving emails from it, send an email to specflow+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Message has been deleted

Todd J

unread,
Jan 25, 2016, 11:13:00 AM1/25/16
to SpecFlow
Based on your other posts, if you are trying to execute your code in multiple environments you might want to try this. 

create a line in your app.config:

<add key="Environment" value="test" />

Create a data.cs file

     
 public class EnvData
       
{
           
public EnvData()
           
{
               
if (ConfigurationManager.AppSettings["Environment"] == "test")
               
{
                    defaultEmailLogin
= "te...@no.com";
                    defaultPassword
= "Tesing@987";
               
}


               
else if (ConfigurationManager.AppSettings["Environment"] == "dev")


               
{
                    defaultEmailLogin
= "te...@no.com";
                    defaultPassword
= "Tesing@123";
               
}
           
}
 
 
public static string defaultEmailLogin;
 
public static string defaultPassword;
 
}



Add the below to your steps file:

 private readonly EnvData _envData;
       
public LoginSteps(EnvData envData )
       
{
            _envData
= envData;
       
}

Your steps will look like this:
 [Given(@"I login as my default user")]
       
public void GivenILoginAsMyDefaultUser()
       
{
           
Pages.LoginPage.VerifyPageReady();
           
Pages.LoginPage.TypeEmail(EnvData.defaultEmailLogin);
           
Pages.LoginPage.TypePassword(EnvData.defaultPassword);
           
Pages.LoginPage.ClickSignInButton();
       
}



Reply all
Reply to author
Forward
0 new messages