【问题标题】:How can I use App.config to store urls and retrieve the urls in testing Selenium tests with C#?如何使用 App.config 存储 url 并在使用 C# 测试 Selenium 测试时检索 url?
【发布时间】:2021-05-29 17:33:55
【问题描述】:

我创建了一个 App.config 文件来存储 url,而不是在我的单元测试中硬编码它们,所以如果 url 发生更改,我只有一个地方需要更改它们。这是我的 App.config(url 已更改为发布目的):

<configuration>
  <appSettings>
    <add key="Home" value="https://home.com"/>
    <add key="AnotherPage" value="https://anotherpage.com"/>
  </appSettings>
</configuration>

在我的测试中,我尝试使用这样的网址:

using Xunit;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using System;
using System.IO;
using System.Configuration;

namespace Homepage.Tests.UI.Tests
{
    public class HomePageShould
    {
        [Fact]
        public static void Open()
        {
            var url = ConfigurationManager.AppSettings["Home"];
            using IWebDriver driver = new ChromeDriver();
            driver.Navigate().GoToUrl(url);

        }
    }
}

我收到此错误:

结果消息:System.ArgumentNullException:参数“url”不能为空。 (参数‘值’)

我也试过这个:var _homeUrl = ConfigurationManager.AppSettings.Get("Home");

得到同样的错误。

我做错了什么,如何让它发挥作用?

【问题讨论】:

    标签: c# selenium app-config


    【解决方案1】:

    这不起作用,因为创建新项目时会自动生成 App.config 文件。很可能,您创建了错误类型的项目。

    您可以找到有关配置和 Selenium 的更多信息here

    附:我会将其发布为评论,但我的声誉

    【讨论】:

    • 感谢您的回答,我看不出我所做的与文章中的有何不同。
    【解决方案2】:

    App.config 文件应该在创建项目时自动创建。因此,如果您这样做,也许您不需要手动创建它。可能是手动创建此文件可能是找不到键下的值的问题。除此之外,您的代码看起来还可以获取价值。

    【讨论】:

    • asp.net 核心应用程序会自动创建 app.config 吗?我通过选择 xunit 测试项目模板 .net core 在 Visual Studio 中创建了我的测试。
    • 没有。点网核心不一定需要 app.config 文件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-06
    • 1970-01-01
    • 1970-01-01
    • 2019-12-02
    • 1970-01-01
    • 2018-12-01
    • 1970-01-01
    相关资源
    最近更新 更多