Test Automation Framework

Seagen Test Automation framework is a common design framework that will add test automation to any project based on the types of requirements and design work being done.

The common design takes feature files written in gherkin or cucumber, ties the code to the step test, and then executes the test. The process is as follows.

  1. Elicit business use cases from the business partner or team

  2. Convert those cases to feature scenarios based on business context

  3. Run the test and make sure each scenario is inconclusive

  4. Work with the developer to create step tests

  5. Run the tests and make sure each scenario is passing

  6. As the analyst updates the feature files, the tests will be inconclusive

This simple process ensures there is a direct translation between a use case and a feature file. Obviously, the feature has a specific syntax designed to describe how a user interacts with the functions of their job. A feature is not a process or procedure but a description of the function.

Tooling

For MVP and with the initial release of test automation, Seagen uses SpecFlow, C#, Gherkin, Cucumber, Selenium, and visual studio to build and compile the test automation code.

Follow these steps

First, ensure you have the following tools installed:

  • Visual Studio (preferably the latest version)

  • .NET Core SDK

Next, install the necessary packages and extensions:

  • SpecFlow for Visual Studio

  • SpecFlow.NUnit

  • SpecFlow.Tools.MsBuild.Generation

  • Cucumber

  • Selenium.WebDriver

  • Selenium.Support

Create a new project in Visual Studio

  • Create a new .NET Core Console App project in Visual Studio.

  • Add the required NuGet packages and extensions.

Configure SpecFlow

  • Add a new folder named “Features” in the project.

  • Inside the “Features” folder, create a new file called “SampleFeature.feature”.

Write a simple Gherkin scenario for testing purposes

Feature: SampleFeature
    As a user
    I want to test a feature file
    So that I can see the test status in the dashboard

Scenario: Test sample feature
    Given I have a sample feature file
    When I execute the test
    Then I should see the test status in the dashboard

Create step definitions

  • Create a new folder named “StepDefinitions” in the project.

  • Inside the “StepDefinitions” folder, create a new class called “SampleFeatureSteps.cs”.

Implement the step definitions for the Gherkin scenario:

using NUnit.Framework;
using TechTalk.SpecFlow;

namespace TestAutomationFramework.StepDefinitions
{
    [Binding]
    public class SampleFeatureSteps
    {
        [Given(@"I have a sample feature file")]
        public void GivenIHaveASampleFeatureFile()
        {
            // Implement logic to set up the test
        }

        [When(@"I execute the test")]
        public void WhenIExecuteTheTest()
        {
            // Implement logic to execute the test
        }

        [Then(@"I should see the test status in the dashboard")]
        public void ThenIShouldSeeTheTestStatusInTheDashboard()
        {
            // Implement logic to check the test status
            Assert.IsTrue(true); // Replace this with actual test status verification
        }
    }
}

Implement the test runner

  • Add a new folder named “TestRunner” in the project.

  • Inside the “TestRunner” folder, create a new class called “TestRunner.cs”.

Implement a simple test runner using NUnit

using NUnit.Framework;
using TestAutomationFramework.StepDefinitions;

namespace TestAutomationFramework.TestRunner
{
    [TestFixture]
    public class TestRunner
    {
        private SampleFeatureSteps _sampleFeatureSteps;

        [SetUp]
        public void Setup()
        {
            _sampleFeatureSteps = new SampleFeatureSteps();
        }

        [Test]
        public void TestSampleFeature()
        {
            _sampleFeatureSteps.GivenIHaveASampleFeatureFile();
            _sampleFeatureSteps.WhenIExecuteTheTest();
            _sampleFeatureSteps.ThenIShouldSeeTheTestStatusInTheDashboard();
        }
    }
}

Create a dashboard

  • To create a dashboard to display the test results, you can use an existing test reporting tool, such as ExtentReports, or create a custom dashboard.

  • If you’re using ExtentReports, install the ExtentReports NuGet package, and add a new class to handle reporting in the “TestRunner” folder.

  • Update the “TestRunner.cs” to use the reporting class.

Execute the tests

  • Build and run the project to execute the tests.

  • Check the dashboard to see the test status.

Implement ExtentReports for reporting

To use ExtentReports for test result visualization, follow these steps:

  • Install the “ExtentReports” and “ExtentReports.NUnit” NuGet packages.

  • Create a new folder named “Reporting” in the project.

  • Inside the “Reporting” folder, create a new class called “ExtentReportManager.cs”:

using AventStack.ExtentReports;
using AventStack.ExtentReports.Reporter;

namespace TestAutomationFramework.Reporting
{
    public class ExtentReportManager
    {
        private static ExtentReports _instance;

        public static ExtentReports Instance
        {
            get
            {
                if (_instance == null)
                {
                    var htmlReporter = new ExtentHtmlReporter("TestDashboard.html");
                    _instance = new ExtentReports();
                    _instance.AttachReporter(htmlReporter);
                }
                return _instance;
            }
        }
    }
}

Update TestRunner to use ExtentReports

  • Modify the “TestRunner.cs” file to include ExtentReports:

using NUnit.Framework;
using TestAutomationFramework.StepDefinitions;
using TestAutomationFramework.Reporting;
using AventStack.ExtentReports;
using AventStack.ExtentReports.NUnit;

namespace TestAutomationFramework.TestRunner
{
    [TestFixture]
    public class TestRunner
    {
        private SampleFeatureSteps _sampleFeatureSteps;
        private ExtentReports _extent;
        private ExtentTest _test;

        [OneTimeSetUp]
        public void OneTimeSetUp()
        {
            _extent = ExtentReportManager.Instance;
        }

        [SetUp]
        public void Setup()
        {
            _sampleFeatureSteps = new SampleFeatureSteps();
            _test = _extent.CreateTest(TestContext.CurrentContext.Test.Name);
        }

        [Test]
        public void TestSampleFeature()
        {
            _sampleFeatureSteps.GivenIHaveASampleFeatureFile();
            _sampleFeatureSteps.WhenIExecuteTheTest();
            _sampleFeatureSteps.ThenIShouldSeeTheTestStatusInTheDashboard();
        }

        [TearDown]
        public void TearDown()
        {
            var status = TestContext.CurrentContext.Result.Outcome.Status;
            var stacktrace = string.IsNullOrEmpty(TestContext.CurrentContext.Result.StackTrace)
                    ? ""
                    : string.Format("{0}", TestContext.CurrentContext.Result.StackTrace);
            var errorMessage = TestContext.CurrentContext.Result.Message;

            _test.Log(status, "Test " + status + " with " + errorMessage + stacktrace);
        }

        [OneTimeTearDown]
        public void OneTimeTearDown()
        {
            _extent.Flush();
        }
    }
}

Execute tests and view the dashboard

  • Build and run the project to execute the tests.

  • After the tests have completed, you will find a “TestDashboard.html” file generated in your project’s root directory.

  • Open the “TestDashboard.html” file in a browser to view the test results dashboard.

This completes the implementation of a test automation framework using SpecFlow, C#, Gherkin, Cucumber, and ExtentReports for the dashboard. You can now extend this framework to cover more complex test scenarios, integrate with CI/CD pipelines, and customize the dashboard to fit your needs.