0

Recently I have been struggling with flaky UI tests in Selenium. After doing a lot of research on ways to make tests a lot more robust (explicit waits, etc.) I came across some people here on stack overflow that were suggesting soak testing the selenium scripts to try and work out all the small, seemingly random, failures that I was getting. I seems like a great idea but there doesn't appear to be a whole lot of resources on how to accomplish this. My Current thought process is to find some way to write unit tests in MSTest that take the Selenium tests I have, run them multiple times and log the results. But so far I've had no luck with that approach. Specifically, I've tried something like this:

 [TestClass]
    public abstract class MSTestClass
    {
        public TestContext TestContext { get; set; }

        [AssemblyInitialize]
        public static void SetupAssembly(TestContext testContext)
        {

        }
        [TestInitialize]
        public void SetupTest()
        {
            //pull the browser type variable out of the test.runsettings file via the TestContext object
            string browserType = TestContext.Properties["Browser"].ToString();
            //Start a new Browser for the current test
            Browser.Start(browserType);
            //Resize the Browser to your application's target width
            Browser.ResizeTo(1440, 900);
            //pull the site URl from the AppSettings file
            string siteURL = System.Configuration.ConfigurationManager.AppSettings["URL"];
            Browser.Visit(siteURL);
        }

        [TestMethod]
        public void Test()
        {

        }

        [TestCleanup]
        public void CleanupTest()
        {
            Browser.Shutdown();
        }
    }



[TestClass]
    public class ExampleSoakTests
    {
        [TestMethod]
        public void ActualSoakTest()
        {
           ExampleTest testClass = new ExampleTest();
            for(int i = 0; i<10; i++)
            {
               try
               {
                  testClass.SetupTest();
                  testClass.ActualTest();
                  testClass.CleanupTest();
                  Log.Success();
               }
               catch(Exception e)
               {
                  Log.Exception(e);
               }

            }

        }
    }

The issue that I have been having with the above code is that the TestContext property doesn't get initialized. I'm assuming that this is because the test method wasn't called by the Visual Studio Test Runner?

In conclusion any help with my current approach or any ideas on other ways to go about Soak Testing my Selenium Scripts would be a great help.

  • I'd consider try/catch each Selenium action. Then run the tests multiple times to see what pops up. If they are seemingly random I'd suspect something during a webdriverwait... possibly stale element exceptions. – pcalkins Jan 03 '20 at 19:39
  • TestContext is static and can be called without instantiating it. Take out your initialization at the top of MSTestClass and add the using statement ```Microsoft.VisualStudio.TestTools.UnitTesting;``` – RockinWatson Jan 07 '20 at 18:54
  • @RockinWatson I'm already using the Mircrosoft.VisualStudio.TestTools.UnitTesting package. Taking out the statement: public TestContext TestContext { get; set; }, gives me an error that says "an object reference is required for the non-static field,method or property 'TestContext.Properties'" if I had to guess I'd say that the TestContext property isn't static? – MitchelB123 Jan 09 '20 at 16:15
  • @MitchelB123 this might help you out with TestContext with MSTest a little more. https://stackoverflow.com/questions/24249133/understanding-the-mstest-testcontext – RockinWatson Jan 09 '20 at 16:50

0 Answers0