Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

492
Views
How to access TestRunParameters within RunSettings file

Reading through https://msdn.microsoft.com/en-us/library/jj635153.aspx I have created a .RunSettings files with a few parameters similar to the example:

  <TestRunParameters>
    <Parameter name="webAppUrl" value="http://localhost" />
    <Parameter name="webAppUserName" value="Admin" />
    <Parameter name="webAppPassword" value="Password" />
  </TestRunParameters>

I plan on having a .RunSettings file for each of our environments with appropriate URLs and credentials for running a CodedUI test on the specified RunSettings file's environment.

I can see that from command line to reference the Settings file I can run:

vstest.console myTestDll.dll /Settings:Local.RunSettings /Logger:trx
vstest.console myTestDll.dll /Settings:QA.RunSettings /Logger:trx

etc...

But I don't see any way that calls out how to actually utilize the TestRunParameters from within the codedUI test.

What I would like to do is set up test initializers that use the TestRunParameters to determine where to log in, and what credentials to use. Something like this:

[TestInitialize()]
public void MyTestInitialize()
{

    // I'm unsure how to grab the RunSettings.TestRunParameters below
    string entryUrl = ""; // TestRunParameters.webAppUrl
    string userName = ""; // TestRunParameters.webAppUserName
    string password = ""; // TestRunParameters.webAppPassword

    LoginToPage(entryUrl, userName, password);
}

public void LoginToPage(string entryUrl, string userName, string password)
{
    // Implementation
}

Information on how to reference the TestRunParameters is greatly appreciated!

EDIT

/// <summary>
/// Summary description for CodedUITest1
/// </summary>
[CodedUITest]
public class CodedUITest1
{

    public static string UserName = string.Empty;

    [ClassInitialize]
    public static void TestClassInitialize(TestContext context)
    {
        UserName = context.Properties["webAppUserName"].ToString();
        Console.WriteLine(UserName);
    }

    [TestMethod]
    public void CodedUITestMethod1()
    {
        this.UIMap.RecordedMethod1();
        // To generate code for this test, select "Generate Code for Coded UI Test" from the shortcut menu and select one of the menu items.
    }

    // Rest of the default class - TestContext instantiation, UI map instantiation, etc
}

The exception I'm getting when running:

NullReference Exception

enter image description here

@williamfalconeruk I have updated my test class as above, but I am still getting the same error, any idea what I'm doing wrong?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

For those that use Resharper with this issue, I discovered the fix (no need to disable Resharper):

  1. Go to Visual Studio top menu -> Resharper -> Options

  2. Find the Tools section, expand "Unit Testing"

  3. Click on "MsTest". The checkbox should be on enabled, but the Test Settings file path below that may be blank. If it is, click on browse and select the runsettings file you want to use.

  4. Click save, rebuild and try to run the tests, the parameters should now work.

Not sure why but simply selecting the Test Settings file from the Tests menu -> Test Settings does not actually work when using Resharper, so this file needs to be explicitly pointed to directly in the Resharper options.

over 4 years ago · Santiago Trujillo Report

0

I also came across this recently, as we wanted to move away from legacy environment variable usage. The solution provided below was suitable for our needs, but there may be a better one...

It turns out you can access these in the TestContext of a ClassInitialize method of your Test fixture. There is a Properties dictionary which has these parameters, and you could access the values here:

[ClassInitialize]
public static void TestClassinitialize(TestContext context)
{
    var webAppUrl = context.Properties["webAppUrl"].ToString();

   //other settings etc..then use your test settings parameters here...
}

Note: these are static so if you need access to this you may need to set up static properties to access within your Test code.

An alternative suggested is using a Data Driven Test approach. Here's some basic info on data driven tests here which may also help: https://msdn.microsoft.com/en-us/library/ms182527.aspx

As I said, the above solution suited our needs at a basic level.

UPDATE: see image below in response to test settings returning null...

Test Settings in Visual Studio

over 4 years ago · Santiago Trujillo Report

0

This works for me (VS2017-pro):

namespace TestApp.Test
{
    [TestClass]
    public class UnitTest1
    {
        // This enables the runner to set the TestContext. It gets updated for each test.
        public TestContext TestContext { get; set; }
        [TestMethod]
        public void TestMethod1()
        {
            // Arrange
            String expectedName = "TestMethod1";
            String expectedUrl = "http://localhost";

            // Act
            String actualName = TestContext.TestName;
            // The properties are read from the .runsettings file
            String actualUrl = TestContext.Properties["webAppUrl"].ToString();

            // Assert
            Assert.AreEqual(expectedName, actualName);
            Assert.AreEqual(expectedUrl, actualUrl);
        }

        [TestMethod]
        public void TestMethod2()
        {
            // Arrange
            String expectedName = "TestMethod2";

            // Act
            String actualName = TestContext.TestName;

            // Assert
            Assert.AreEqual(expectedName, actualName);
        }
    }
}

Make sure to select the runsettings file you wish to use, here: Test -> Test Settings.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!