URL 配置文件中的设置 returns 空

URL setting in configuration file returns null

我正在学习本教程:

https://www.toolsqa.com/selenium-webdriver/c-sharp/manage-read-configurations-using-configurationmanager-csharp/

出于某种原因,当我尝试这个时,它总是 returns null

Environment.config 看起来像:

<appSettings>
    <add key="URL" value="http://www.test.com"/>
</appSettings>

我的测试 class 看起来像:

using System;
using System.Configuration;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using qa.WrapperFactory;

namespace UnitTestProject2
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            // Sign in through google first, so we don't have to follow new tabs
            BrowserFactory.InitBrowser("Chrome");
            var y = ConfigurationManager.AppSettings["URL"];
            BrowserFactory.LoadApplication(ConfigurationManager.AppSettings["URL"]);
            BrowserFactory.CloseAllDrivers();
        }
    }
}

出于某种原因,我不断收到错误消息:

Message: Test method UnitTestProject2.UnitTest1.TestMethod1 threw exception: System.ArgumentNullException: Argument 'url' cannot be null.

浏览器正确加载

您没有正确按照教程进行操作,如果您仔细阅读 "Steps to read AppSettings from External Config File using ConfigurationManager" 部分,您将看到:

使用 ConfigurationManager 从外部配置文件读取 AppSettings 的步骤:

App.config 文件

<configuration>
        <appSettings configSource="Configurations\Environment.config" />
</configuration>

在同一项目下创建另一个配置文件并将其命名为Environment.config。

Environment.config 文件

<appSettings>
        <add key="URL" value="http://www.example.com"/>
</appSettings>

要从上面的配置文件中读取连接字符串,请使用以下代码:

var url = ConfigurationManager.AppSettings["URL"]; 

在使用 ConfigurationManager.AppSetting["key"].

之前,您需要将外部 SomeConfigFile.config 文件 path/reference 添加到您的 App.config 文件