'Why does ConfigurationManager.GetSection return null?

I am trying to store the environments and capabilities sections that are in my App.config file into 2 different name-value collections, and for some reason, it is returning null.

The App.config file:

<?xml version="1.0" encoding="utf-8" ?>
  <configuration>
  <configSections>
    <sectionGroup name="capabilities">
        <section name="parallel" type="System.Configuration.AppSettingsSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    </sectionGroup>

    <sectionGroup name="environments">
        <section name="chrome" type="System.Configuration.AppSettingsSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <section name="firefox" type="System.Configuration.AppSettingsSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        <section name="edge" type="System.Configuration.AppSettingsSection, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
    </sectionGroup>
</configSections>

<appSettings>
    <add key="user" value="" />
    <add key="key" value="" />
    <add key="server" value="hub-cloud.browserstack.com" />
</appSettings>

<capabilities>
    <parallel>
        <add key="build" value="Build 1.0" />
        <add key="name" value="parallel_test" />
        <add key="browserstack.debug" value="true" />
    </parallel>
</capabilities>

<environments>
    <chrome>
        <add key="browser" value="Chrome" />
        <add key="os_version" value="10" />
        <add key="os" value="Windows" />
        <add key="browser_version" value="latest" />
        <add key="browserstack.local" value="true" />
        <add key="browserstack.selenium_version" value="3.14.0" />
        <add key="browserstack.user" value="" />
        <add key="browserstack.key" value="" />
    </chrome>
    <firefox>
        <add key="browser" value="Firefox" />
        <add key="os_version" value="10" />
        <add key="os" value="Windows" />
        <add key="browser_version" value="latest" />
        <add key="browserstack.local" value="true" />
        <add key="browserstack.selenium_version" value="3.10.0" />
        <add key="browserstack.user" value="" />
        <add key="browserstack.key" value="" />
        </firefox>
        <edge>
        <add key="browser" value="Edge" />
        <add key="os_version" value="10" />
        <add key="os" value="Windows" />
        <add key="browser_version" value="latest" />
        <add key="browserstack.local" value="true" />
        <add key="browserstack.selenium_version" value="3.5.2" />
        <add key="browserstack.user" value="" />
        <add key="browserstack.key" value="" />
    </edge>
  </environments>
</configuration>

TestInit class:

[OneTimeSetUp]
public void Init()
{
    NameValueCollection caps = ConfigurationManager.GetSection("capabilities/" + profile) as NameValueCollection; //Returns Null
    NameValueCollection settings = ConfigurationManager.GetSection("environments/" + environment) as NameValueCollection; //Returns Null

    DesiredCapabilities capability = new DesiredCapabilities();

    foreach (string key in caps.AllKeys) //<--System.NullReferenceException: 'Object reference not set to an instance of an object. caps was null.'

    ...
}

I don't know if the issue could be the structure of the folder where my App.config file is located, if that even matters. So, here is my folder's structure:

enter image description here



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source