Using .runsettings to Configure Selenium Tests from Release Pipeline

I wanted to be able to tell my Selenium tests what browser to test and what resolution to use from my Azure DevOps Release Pipeline. In order to do so, I utilized a .runsettings file.

First I created a .runsettings file and placed it in my Selenium code. I created two parameters, one for testBrowser and one for windowSize, then set them to their default values.

Next I created six stages in my Release Pipeline, two for Chrome, both Desktop and mobile. Then two for Firefox and two for IE.

In order to tell my code to use the appropriate driver for each stage, I specified the .runsettings file to use in the settings file field of each VS Test task.

Then I added the correct parameter and value to their respective stage. For example, the Firefox stage for the desktop resolution has the following values in the override test run parameters field:

-testBrowser Firefox -windowSize Desktop

Now I can use these values in my test methods by creating variables and setting them to the value we passed to .runsettings.

In order to get the .runsettings values in my tests, I needed to use TestContext. The code looks like this:

namespace Tests
{
    [TestClass]
    public class Test
    {
        private static TestContext testContextInstance; // Declare an instance of TestContext
        private string testBrowser; // Declare testBrowser
        private string windowSize; // Declare windowSize

        public Test()
        {
        }

        [TestMethod]
        [TestCategory(nameof(testBrowser))]
        public void Test_Test()
        {
            if (windowSize == "Desktop") // Determine if we are testing on desktop or mobile
            {
                // Run test using IDs, classes, XPaths that are visible in desktop resolution
            }
            else if (windowSize == "Mobile")
            {
                // Run test using IDs, classes, XPaths that are visible in mobile resolution
            }
            else
            {
                CustomException ex = new CustomException("windowSize in Test_Framework() not valid");
                throw ex;
            }
        }

        public TestContext TestContext // Setup TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        [TestInitialize()]
        public void Setup_Test_Test()
        {
            testBrowser = testContextInstance.Properties["testBrowser"].ToString(); // Initialize our testBrowser variable with the value from .runsettings parameter. 
            windowSize = testContextInstance.Properties["windowSize"].ToString(); // Initialize our windowSize variable with the value from .runsettings parameter. 
        }

        [TestCleanup()]
        public void Cleanup_Test_Test()
        {
            Config.driver.Quit(); // Close the driver (it's created in my Config class)
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *