【问题标题】:my code doesn't read Value from app.config我的代码没有从 app.config 读取值
【发布时间】:2021-07-08 21:17:18
【问题描述】:

我是升 C 的绝对初学者。 我的代码没有从app.config. 读取值我无法弄清楚缺少什么。 该代码会打开 Chrome,但不会转到 Google 网站。 如果有人能提供帮助,非常感谢。

using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System.Configuration;
using System.IO;
using System.Reflection;

namespace TestProject1
{
    [TestClass]
    public class UnitTest1
    {
        public static IWebDriver driver { get; set; }
        [TestMethod]
        public void TestMethod1()
        {
            var chromeOptions = new ChromeOptions();
            chromeOptions.AddArgument("-no-sandbox");
            driver = new ChromeDriver(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), chromeOptions);
            driver.Navigate().GoToUrl(ConfigurationManager.AppSettings["URL"]);
        }
    }
}


<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <appSettings>
        <add key="URL" value="https://www.google.com/" />
    </appSettings>
</configuration>

【问题讨论】:

  • The code opens the Chrome but doesn't go to Google 什么代码?请更新您的帖子以包含不起作用的代码,而不仅仅是代码图像。
  • 请将代码和数据添加为文本 (using code formatting),而不是图像。图片:A)不允许我们复制粘贴代码/错误/数据进行测试; B) 不允许根据代码/错误/数据内容进行搜索;和many more reasons。除了代码格式的文本之外,只有在图像添加了一些重要的东西,而不仅仅是文本代码/错误/数据传达的内容时,才应该使用图像。
  • 在配置文件中注意你的上限;它应该是 &lt;appSettings&gt; 而不是 &lt;AppSettings&gt;
  • 尝试将AppSettingsxml节点重命名为appSettings

标签: c# selenium-webdriver automated-tests app-config


【解决方案1】:

Chrome 打开显示驱动程序正确启动。

Appsettings 拼写错误。

   <appSettings>
      <add key="applicationurl"     value="www.google.com" />
 </appSettings>

否则将其分配给字符串/调试,您可以从 app.config 正确读取值,其余代码看起来不错。

【讨论】:

  • Appsettings 无法拼写。还是不行。
【解决方案2】:

试试这个

private object getASR(string key, Type type, object defaultValue)
{
 AppSettingsReader asr = new AppSettingsReader();
 try {
  return asr.GetValue(key, type);
  } catch (Exception) {
   return defaultValue;
  }
}

string urlValue = (string)getASR("URL", typeof(string), "defaultValue");

【讨论】:

    【解决方案3】:

    appSettings 区分大小写,在您的配置文件中将AppSettings 更改为appSettings

    【讨论】:

    • 区分大小写在这种情况下没有帮助。
    【解决方案4】:

    我的猜测是您的代码首先没有导航到谷歌的原因是从未设置过 url 键。通常,您需要按照@james 建议的方式阅读 dotnet 测试项目所需的任何配置。

    我的建议是使用配置生成器,它可以让您从 json 文件中提取配置,如果您愿意,也可以从环境变量中提取配置。你可以像这样设置一个配置助手:

    using System;
    using System.IO;
    using System.Reflection;
    using Microsoft.Extensions.Configuration;
    
    
    namespace TestProject1
    {
        
        public class Config
        {
            public string? Url { get; set; }
            public string? Username  { get; set; }
            public string? Password { get; set; }
            // any other config you need
    
        }
        
        public static class ConfigProvider
        {
            
            public static Config Config;
            
            static ConfigProvider()
            {
                // Build config from various sources
                var configRoot = new ConfigurationBuilder()
                    .AddJsonFile("testSettings.json")
                    .AddEnvironmentVariables()
                    .Build();
                
                // Bind config to new config object
                Config = new Config();
                
                // Bind params from testParams.json
                configRoot.Bind(Config);
            }
    
        }
    
        
    }
    

    将 testSettings.json 文件添加到您的测试项目根目录,如下所示:

    {
        "Url": "www.google.co.uk",
        "Username": "myuser",
        "Password": "mypassword"
    }
    

    更新您的 csproj 文件以包含配置构建器依赖项,并将其设置为将您的配置文件复制到构建文件夹:

    <Project Sdk="Microsoft.NET.Sdk">
    
       <PropertyGroup> 
       <!-- property group items -->
       </PropertyGroup>
    
      <ItemGroup>
        <PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="5.0.0" />
        <PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="5.0.0" />
        <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="5.0.0" />
        <!-- other dependencies -->
      </ItemGroup>
    
      <ItemGroup>
        <Content Include="testSettings.json" CopyToOutputDirectory="PreserveNewest" />
      </ItemGroup>
    
    
    </Project>
    

    然后您可以像这样在测试中引用您需要的任何配置:

    [TestMethod]
    public void TestMethod1()
    {
      // driver setup here ...
      driver.Navigate().GoToUrl(ConfigProvider.Config.Url);
      driver.FindElement(By.CssSelector("#username")).SendKeys(ConfigProvider.Config.Username);
      driver.FindElement(By.CssSelector("#password")).SendKeys(ConfigProvider.Config.Password);
    }
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      • 2014-04-03
      • 2013-09-19
      • 2021-07-19
      • 2012-10-14
      • 2012-07-12
      相关资源
      最近更新 更多