简体   繁体   English

在C# selenium自动化项目中使用appsettings.json

[英]Using appsettings.json in C# selenium automation project

I'm working on automation tests which I'm still new to writing the scripts.我正在从事自动化测试,我对编写脚本还是很陌生。 I'll try to be as short as possible.我会尽量简短。 I'm building everything in .NET CORE.我正在 .NET CORE 中构建所有内容。 Selenium C# in Visual Studio Visual Studio 中的 Selenium C#

Basically what I would like to know is how to pull string from appsettings.json file to my test class file?基本上我想知道的是如何从 appsettings.json 文件中提取字符串到我的测试 class 文件中?

Basically what I have inside my appsettings.json file ->基本上我的appsettings.json 文件里面有什么 ->

  "BaseURL": "https://google.com/",

What I have in my test clase is:我在我的测试中拥有的是:

    
       [TestCase]
        public void LoginToGmail()
        
            *currently Im using hardcoded version of*
            driver.url = "https://google.com/"*
            IWebElement GmailLogo = driver.FindElement(By.Xpath.......);
            Assert.IsTrue(GmailLogo.Displayed);
        
But I would like to use something like driver.url = "BaseURL" <- (string from the .json file)

How do you pull it from the.json file?你如何从 .json 文件中提取它?

Any kind of help would be appreciated!任何形式的帮助将不胜感激!

You can use the ConfigurationBuilder class to read the config file.您可以使用ConfigurationBuilder class 来读取配置文件。 Assuming this structure for appsettings.json:假设 appsettings.json 的结构如下:

{
    "BaseURL": "https://..."
}

Reading and using the configuration becomes:读取和使用配置变为:

var settings = new ConfigurationBuilder()
    .AddJsonFile("appsettings.json")
    .Build();

driver.Url = settings["BaseURL"];

Important: Be sure the "Copy to Output Directory" property on appsettings.json is set to "Always" or "If Newer" (right-click on appsettings.json in Solution Explorer and choose "Properties"):重要提示:确保 appsettings.json 上的“复制到 Output 目录”属性设置为“始终”或“如果更新”(右键单击 appsettings.Z466DEEC76ECDF5FCA6D38571F6324 中的 4)

appsettings.json 的属性窗格的屏幕截图

Any JSON can be deserialised into a class using C#, this is not limited to an appsettings.json file and you may find it useful to store data sets in json format for testing purposes. Any JSON can be deserialised into a class using C#, this is not limited to an appsettings.json file and you may find it useful to store data sets in json format for testing purposes.

https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0 https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0

Thank you @SteveBeck for the link, helped me.谢谢@SteveBeck 的链接,帮助了我。 Also thanks to @Greg Burghardt.还要感谢@Greg Burghardt。 I managed to set it up like this: created new class for the read Json file and browser startup/close.我设法这样设置:为读取的 Json 文件和浏览器启动/关闭创建了新的 class。

using Newtonsoft.Json;
using NUnit.Framework;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using System.IO;

public class NewSetup
{

    public class GetSetUpFile
    {
        public string BaseURL { get; set; }
        public string SetupJson { get; set; }
        private readonly string _strSetupFile;

        public GetSetUpFile(string jsonfile)
        {
            _strSetupFile = jsonfile;
        }

        public void ReadSetUpFile()
        {

            SetupJson = File.ReadAllText(_strSetupFile);
        }

        public class StartBrowserDriver
        {

             protected IWebDriver driver;

            [SetUp]
            public void StartBrowser()
            {
                var setup = new GetSetUpFile(@"C:\\PATH\\\TO\\\appsettings.json");
                setup.ReadSetUpFile();
                //Deserialize json objects
                setup = JsonConvert.DeserializeObject<GetSetUpFile>(setup.SetupJson);
                var url = setup.BaseURL;
                var DesiredURL = url;
                driver = new ChromeDriver("C:\\Program Files\\Google\\Driver");
                driver.Navigate().GoToUrl(DesiredURL);
                driver.Manage().Window.Maximize();
                driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
            }
            [TearDown]
            public void CloseBrowser()
            {
                driver.Close();
                driver.Quit();
            }
        }
    }
}

Navigate back to your main test class:导航回您的主要测试 class:

  
class User_is_able_to_login_to_Gmail : StartAndCloseBrowserDriver
{
[TestCase]
public void public void LoginToGmail();
IWebElement GmailLogo = driver.FindElement(By.Xpath.......);
Assert.IsTrue(GmailLogo.Displayed);
GmailLogo.Click();
\\\\continue\\\\

The test runs & works.测试运行和工作。 If someone has some pointers about the build, please let me know:) Thanks to everyone!如果有人对构建有一些指示,请告诉我:) 谢谢大家!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM