简体   繁体   中英

How can I update a parameter within a C# Class when running the application via a BAT file?

I currently have a suite of Selenium automation tests using SpecFlow and C# (IDE is Visual Studio 2017).

I created a batch file to run the applicable feature files.

Currently I set my test environment (ie QA, UAT, Prod) within Environments.cs by using the following property

public static string CurrentEnvironment { get; set; } = uat;'

What I want to achieve is to some how pass the test environment via the batch file so there is no need to open the solution and modify before running the BAT file.

There will likely be other parameters I will want to update via this method in the future such as Specflow parameters whereby I might want to override a parameter value.

I've tried Googling a solution but I've found structuring my question doesn't yield the results I want.

Batch file:

ECHO ON

set Date=%date:~0,2%%date:~3,2%%date:~6,4%
set Time=%time:~0,2%%time:~3,2%

cd C:\Users\%username%\source\repos\AutomationTests\TestProject\packages\SpecRun.Runner.1.8.0\tools

SpecRun.exe run default.srprofile /basefolder:C:\Users\%username%\source\repos\AutomationTests\TestProject\TestProject\bin\Debug /filter:testpath:"Feature:TestFeature"

In essence if the CurrentEnvironment property in my solution is set to 'UAT' I want to be able to override that to say 'QA' via the BAT file.

What modifications do I need to make to the BAT file and what to my solution (if any)?

You can set the test environment with environment variables. Environment.GetEnvironmentVariable() is the method to call to read environment variables.

Here is an example:

Program.cs (in a console app):

using System;

namespace TestEnvironmentVariable
{
    class Program
    {
        static void Main(string[] args)
        {
            string testEnvironment = Environment.GetEnvironmentVariable("test_env");
            Console.WriteLine($"Test environment: {testEnvironment}");
        }
    }
}

run.bat:

set test_env=uat

TestEnvironmentVariable.exe

When running run.bat:

>run.bat

>set test_env=uat

>TestEnvironmentVariable.exe
Test environment: uat

You can also put all your settings in a json file that you use as a configuration file. It also makes it possible to change the settings without having to compile. Here is a small example:

Create a json file, eg settings.json:

{
  "TestEnvironment": "UAT"
}

It can be created in the root folder of the solution. In the file's properties, set Copy to Output Directory to Copy always or Copy if newer . This makes sure it's moved to the binary output directory.

Then create a Settings.cs file that is representing the class we deserialize the json file to:

namespace TestEnvironmentVariable
{
    public sealed class Settings
    {
        public Settings() { }

        public string TestEnvironment { get; set; }
    }
}

You can add more variables here when they are needed. The json file should have the same variables. And then the code that does the deserialization:

using System.IO;
using Newtonsoft.Json;

namespace TestEnvironmentVariable
{
    public static class SettingsUtil
    {
        public static T GetObjectFromJsonFile<T>(string filename)
        {
            string json = File.ReadAllText(filename);
            var deserializedObject = JsonConvert.DeserializeObject<T>(json);

            return deserializedObject;
        }
    }
}

You have to add Newtonsoft.Json with NuGet. We can then read the json file in our code:

using System;

namespace TestEnvironmentVariable
{
    class Program
    {
        static void Main(string[] args)
        {
            Settings settings = SettingsUtil.GetObjectFromJsonFile<Settings>("settings.json");
            Console.WriteLine($"Test environment: {settings.TestEnvironment}");
        }
    }
}

Output:

>TestEnvironmentVariable.exe
Test environment: UAT

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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