Jan 3, 2017

C# Selenium Webdriver - Code to take browser parameter from config file

App.Config::

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 
    <configSections>
 
    </configSections>
 
  <appSettings>
    <add key="browser" value="ff" />
    <add key ="url" value="YOUR APPLICATION URL"/>
    <add key ="userName" value="YOUR APPLICATION USERNAME"/>
    <add key ="password" value ="YOUR APPLICATION PASSWORD"/>
  </appSettings>

</configuration>
       

Class File code::


IWebDriver driver;
        /// <summary>
        /// This method is used to start the browser instance depending on the browser parameter from configuration file.
        /// </summary>
        [TestInitialize]
        public void Start()
        {
            switch (ConfigurationManager.AppSettings["browser"])
            {
                case "ff":
                    {
                        driver = new FirefoxDriver();
                        break;
                    }
                case "ch":
                    {
                        driver = new ChromeDriver();
                        break;
                    }
                case "ie":
                    {
                        driver = new InternetExplorerDriver();
                        break;
                    }
                default:
                    {
                        Console.WriteLine("Something is wrong.. Please check.");
                        break;
                    }
            }
        }

No comments: