Quantcast
Channel: Reading settings from app.config or web.config in .NET - Stack Overflow
Viewing all articles
Browse latest Browse all 29

Answer by user3534040 for Reading settings from app.config or web.config in .NET

$
0
0

I was able to get the below approach working for .NET Core projects:

Steps:

  1. Create an appsettings.json (format given below) in your project.
  2. Next create a configuration class. The format is provided below.
  3. I have created a Login() method to show the usage of the Configuration Class.

    Create appsettings.json in your project with content:

    {"Environments": {"QA": {"Url": "somevalue","Username": "someuser","Password": "somepwd"  },"BrowserConfig": {"Browser": "Chrome","Headless": "true"  },"EnvironmentSelected": {"Environment": "QA"  }}public static class Configuration{    private static IConfiguration _configuration;    static Configuration()    {        var builder = new ConfigurationBuilder()            .AddJsonFile($"appsettings.json");        _configuration = builder.Build();    }    public static Browser GetBrowser()    {        if (_configuration.GetSection("BrowserConfig:Browser").Value == "Firefox")        {            return Browser.Firefox;        }        if (_configuration.GetSection("BrowserConfig:Browser").Value == "Edge")        {            return Browser.Edge;        }        if (_configuration.GetSection("BrowserConfig:Browser").Value == "IE")        {            return Browser.InternetExplorer;        }        return Browser.Chrome;    }    public static bool IsHeadless()    {        return _configuration.GetSection("BrowserConfig:Headless").Value == "true";    }    public static string GetEnvironment()    {        return _configuration.GetSection("EnvironmentSelected")["Environment"];    }    public static IConfigurationSection EnvironmentInfo()    {        var env = GetEnvironment();        return _configuration.GetSection($@"Environments:{env}");    }}public void Login(){    var environment = Configuration.EnvironmentInfo();    Email.SendKeys(environment["username"]);    Password.SendKeys(environment["password"]);    WaitForElementToBeClickableAndClick(_driver, SignIn);}

Viewing all articles
Browse latest Browse all 29

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>