Search code examples
c#selenium-webdriverautomated-testsnunit

Is there a way I can use the Test Description in a conditional Statement in a selenium test in C#?


I'm creating tests for an ordering system; I've created tests in C# using Visual Studio 2022 for various pages around it, and now I am creating tests for the ordering part itself. I have a BasePage class that contains, among other things, a switch statement that determines what URL to visit depending upon what value is passed into the method containing the switch (OpenBrowserandGoToURL). Each page tested has a different URL.

Everything has worked as expected so far with this model:

[TestInitialize]
public void Setup()
{
    //Code that is not germane to the question
    PlaceOrder = new OrderPage(Driver) 
    PlaceOrder.OpenBrowserandGoToURL("TheTextThatTellsTheSwitchStatementWhichURLToVisit")
}

[TestMethod]
[Description("California")]
// Nothing here or below matters

So let me just clarify a few things about the above code. This code lives in a class called OrderTest, which runs all the tests on OrderPage (where the actual tests are written). As you can no doubt tell, when I create the PlaceOrder instance of OrderPage, it uses whatever I put in the last line of the setup in quotes to get the URL from the switch statement. Up to now, each class of tests used one and only one URL because I'm testing one page at a time.

But here's the issue - I need to test the order system for three different states. All elements for each system is the same (all ids, xpaths, etc) - EXACTLY the same. It's just the text that is different. Because all of the elements are the same, I don't want to write one test for each ordering system. I just want to write one test and let some conditional statement determine which state's ordering system URL to use. I've gone over some solutions that would work, for example, I could take the PlaceOrder.OpenBrowserandGoToURL line and stick it at the top of each test method (and there will be many for each state) under the declaration statement for that method. But that would cause me to have to copy and paste code or put in the same code over and over. I've heard that's a no-no. :-). The easiest thing I can think of to do would be somehow to capture the value of the description in each [TestMethod] and use that in PlaceOrder.OpenBrowserandGoToURL. Something like this:

[TestInitialize]
public void Setup()
{
    //Code that is not germane to the question
    PlaceOrder = new OrderPage(Driver)
    string methodDescription = [the code that gets the Description from the test method]
    PlaceOrder.OpenBrowserandGoToURL(methodDescription) // Send "California" to the method in BasePage
                                                        // to be used in the switch statement.
}

[TestMethod]
[Description("California")]
// Nothing here or below matters

Is this even possible? Or is there a more efficient way to do this that will allow me not to duplicate code for the three different order systems that also will not require that I update the OpenBrowserandGoToURL method in the BasePage class (or any method in the BasePage class, for that matter) such that it no longer works for tests that have just a single URL (i.e., not break all the previous tests I've written)?

I've visited a few other Stack Overflow pages, and I've found a few articles that mention NUnit's CurrentContext, something like:

TestContext.CurrentContext.Test.Properties.Get("Description")

But I couldn't make that work - not sure if it's because I'm trying to use the description of a test method that is external to the method using the [TestInitialize] attribute, if it's outdated or for the wrong language, or if I'm just not using it correctly (I'm setting a string variable to its value - at least that's what I tried to do). I guess if there is no way to do it like this, I'll consider the overall framework when I'm done with it, but I don't want to go back and change all the work I've already done at this point, so if there is a way I can get the method under the [TestInitialize] Attribute to capture the description from each Test case with a [TestMethod] attribute, that would make my life easier. And just in case it matters, I am using Chrome for these tests. Thank you in advance!


Solution

  • It looks like you are using MSTest rather than Nunit.

    Try adding the correct using statement and update the test attributes to [Test] and [SetUp]

    using NUnit.Framework;
    
    [SetUp]
    public void Setup()
    {
        // Setup code
    }
    
    [Test]
    [Description("URL")]
    public void Test()
    {
        // Test code here
    }
    

    This will use NUnit and you should now be able to use TestContext. However, the way to achieve what you're asking would be to use test cases.

    [TestCase("URL 1")]
    [TestCase("URL 2")]
    [TestCase("URL 3")]
    public void Test(string url)
    {
    
    }