Running cross browser tests using selenium and .NET Core Running cross browser tests using selenium and .NET Core selenium selenium

Running cross browser tests using selenium and .NET Core


Unfortunately, XUnit does not support [TearDown] methods the way that NUnit does. More information on how to implement something like this in XUnit can be found here: http://mrshipley.com/2018/01/10/implementing-a-teardown-method-in-xunit/

In case you decide you'd like to use NUnit instead, you can refer to this code here. I've implemented my own version of a TestFixture that is responsible for creating WebDriver instances to begin a test, and closing the instance as needed. Here's an example:

using System;using NUnit.Framework;using OpenQA.Selenium;namespace MyTestAutomation.Framework{    public class BaseTestFixture    {       // this is your singleton webdriver instance -- this gets used throughout the testcase        public static AndroidDriver<AppiumWebElement> Driver { get; private set; }        [SetUp]        public void BaseTestInitialize()        {                 // create webdriver instance here            Driver = DriverFactory.Current.Create();            // call test initialize, which can get overridden in each test case class                TestInitialize();        }        [TearDown]        public void BaseTestCleanup()        {            // implement TestCleanup() override in your test case class            TestCleanup(); // clean up after test case -- delete data, etc.            // universal cleanup: close & quit driver            Driver.Close();            Driver.Quit();        }        protected virtual void TestInitialize()        {            // override this method in your test class & write initialize steps        }        protected virtual void TestCleanup()        {           // override this method in your test class & write additional cleanup steps        }    }}

To use this in action, your test class needs to inherit BaseTestFixture, like this:

public class MyTestClass : BaseTestFixture{    protected override void TestInitialize()    {        // initialize your test case here        // no need to create webdriver, base class method already does this        // do something like log in to your application here, or navigate to URL    }    [Test]    public void MyTest()    {    }    protected override void TestCleanup()    {        // clean up your test case here -- log out, delete data, etc.        // no need to implement Driver.Close(), base class method already does it     }}

With this code sample, you can write a test fixture that handles creating a WebDriver instance & closing it before and after every test. You can override TestInitialize and TestCleanup if you wish to implement more specific steps at the start and end of each test case. NUnit's [SetUp] and [TearDown] methods ensure these initialize / cleanup methods will only be run before & after each test case.


You can use using Xunit.Abstractions; and use the Dispose method. The code below also permits console output, a serious pain point with xunit is not having that.

public class MyTestClass : IDisposable{    private readonly ITestOutputHelper print;    public MyTestClass(ITestOutputHelper output)    {        this.print = output;        print.WriteLine("I'm the tear up");    }    public void Dispose()    {        print.WriteLine("I'm the tear down");    }    [Fact]    public void MyTest1()    {        print.WriteLine("This is output from test 1");    }    [Fact]    public void MyTest2()    {        print.WriteLine("This is output from test 2");    }    [Fact]    public void MyTest3()    {        print.WriteLine("This is output from test 3");    }}