Search code examples
c#seleniummstest

How to properly make use of TestContext.Properties


I need to access TestContext.Properties before a TestMethod so the test can receive the correct environment to test.


The contents of my test.runsettings:

<?xml version="1.0" encoding="utf-8"?>
  <RunSettings>
    <TestRunParameters>
      <Parameter name="colegio" value="7" />
    </TestRunParameters>

As you can see, the file only contains one parameter, called colegio (school)


This are the contents of TestBase.cs:

using Microsoft.VisualStudio.TestTools.UnitTesting;
using InfraestructureSelenium.Helper;
using System.Collections.Generic;
using InfraestructureSelenium.Configuration.Enumerados;

namespace TestSelenium
{
    [DeploymentItem("chromedriver.exe")]
    [DeploymentItem("IEDriverServer.exe")]
    [DeploymentItem("phantomjs.exe")]
    [DeploymentItem("geckodriver.exe")]
    [TestClass]
    public class TestBase
    {
        protected TestBase()
        { }

        public TestBase(int id = (int)ColegioEnum.ColegioDemoMovilidad2_Supervisor)
        {
            DiccionarioCompartido = new Dictionary<string, string>();
            SeleniumHelper = new HelperSelenium(id, WebDriverSelector.ObtenerWebDriver());
        }

        public TestBase(HelperSelenium seleniumHelper, Dictionary<string, string> diccionarioCompartido = null)
        {
            SeleniumHelper = seleniumHelper;
        }

        public HelperSelenium SeleniumHelper { get; set; }

        public static Dictionary<string, string> DiccionarioCompartido { get; set; }

        public void CloseBrowser()
        {
            SeleniumHelper.Quit();
        }

        #region Configuración Test

        [TestInitialize]
        public void InitializeTest()
        {

        }

        [TestCleanup]
        public void TestCleanupTest()
        {
            SeleniumHelper.Quit();
        }

        #endregion
        }
    }

As you can see, public TestBase(int id = (int)ColegioEnum.ColegioDemoMovilidad2_Supervisor) receives an argument, which corresponds to the colegio environment I want to test. If no colegio is passed as an argument, the default one will be ColegioEnum.ColegioDemoMovilidad2_Supervisor

However, when I try to instantiate TestContextin a TestClass, like this:

[TestClass]
    public class GenerarBoletinDeClase : TestBase
    {
        public TestContext TestContext { get; set; }

        private static TestContext _testContext;

        [TestInitialize]
        public static void SetupTests(TestContext testContext)
        {
            _testContext = testContext;
        }

        public GenerarBoletinDeClase() : base((int)TestContext.Properties["colegio"]) { }

The following error appears: An object reference is required for the non-static field, method, or property

Any help is appreciated as I've given this problem a lot of time and I couldn't progress any further.


Solution

  • First of all, thanks to @Peska for providing the code in this answer https://stackoverflow.com/a/51367231/5364231


    So, finally what I did was add the following code to the class TestBase:

    public class TestBase
    {
        public TestContext TestContext { get; set; }
    
        public static int Colegio { get; set; }
    
        [AssemblyInitialize]
        public static void ClassInitialize(TestContext TestContext)
        {
            Colegio = int.Parse(TestContext.Properties["colegio"].ToString()); // colegio is equal 7 in here
        }
    
        public TestBase()
        {
            SeleniumHelper = new HelperSelenium(Colegio, WebDriverSelector.ObtenerWebDriver());
            DiccionarioCompartido = new Dictionary<string, string>();
        }
    

    The decorator [AssemblyInitialize] is necessary, [ClassInitialize] and [TestInitialize] will not work, I believe because the TestContext has not been instantiated yet.

    After that, make sure that you have configured a Test Settings File by going to Test > Test Settings > Select Test Settings File, the file must be named *.runsettings

    With that, everything should be set up for TestContext.Properties to read from your test settings file