0

I am using Microsoft's WinAppDriver in conjunction with Visual Studio 2015 unit tests to automate testing of Windows applications. These tests are being run from the command line using VSTest.exe because I can pass a .runsettings file as a parameter to specify certain test settings that I may have to change in the future. I want to be able to reference the .runsettings file directly from my test methods without needing to create a bunch of global variables at the beginning of my code in the Setup method. Although I am using multiple classes, I am more or less doing it like so:

protected static string basicFile;

[ClassInitialize]
public static void Setup(TestContext context)
{
var basicFile = context.Properties["basic"].ToString();
}

[TestMethod]
public void BasicTest(){
OpenFile(basicFile);
}

Where context.Properties[] references the key in my .runsettings file.

The reason I cannot simply do

[TestMethod]
public void BasicTest(TestContext context){
var basicFile = context.Properties["basic"].ToString();
OpenFile(basicFile);
}

is because test methods cannot accept any parameters. So, is there any way I can directly reference the .runsettings file within the test method without using context.Properties?

1 Answers1

0

I've just switched to C# project from Java and providing .runsettings in our test automation project also gave me a hard time. I also faced the problem of passing TestContext parameter through several classes. So, my solution may be not so accurate but it worked:

[TestClass]
public class Operations : TestBase
{
    public void CreateRelationship()
    {
        // Add test logic here         
    }
}

Test Base class:

[TestClass]
public abstract class TestBase
{
    [AssemblyInitialize]
    public static void ContextInitialize(TestContext context)
    {
        DriverUtils.Initialize(context);
    }
}

And DriverUtils in turn:

public static class DriverUtils
{   
    private static IWebDriver driver;
    private static TestContext testContext;

    private static string testEnvironment = string.Empty;

    public static void Initialize(TestContext context)
    {
        testContext = context;
        testEnvironment = Convert.ToString(testContext.Properties["TestEnvironmentUrl"]);
    }
}   

The .runsettings file looks exactly like in the examples, but I left "TestEnvironmentUrl" parameter blank. Then I added .runsettings file to TFS artifacts and later in TFS release config added path to the file in 'Run Functional UI Tests' section. Then I was able to override 'TestEnvironmentUrl' with the real Server URL. Still haven't found the implementation of [AssemblyInitialize], but I suppose it makes possible to pass the test context from child test classes to parent TestBase.

Pavlo Osokin
  • 41
  • 1
  • 3