Selenium Testing with Multiple Instances

Category: visual studio unittest


Robert Werner (Rakuten) on Tue, 15 Nov 2016 21:37:40

Using C#, I successfully built a Selenium testing project using XUnit.  It worked fine if I was individually running all tests within one class but when I tried to run all tests at once it failed.  The culprit turned out to be the fact that tests were running in parallel, with multiple browsers opening up.  My code was not accounting for the multiple instance nature of what was occurring.

To resolve this, I've created an "Instance" property in the TestFixture class and then passed "Instance" to every single method which performs a browser operation.  It works but it seems rather clumsy.

I'm curious if this is the right approach or is there a better way?


    public class TestFixture : IDisposable
        public IWebDriver Instance { get; set; }

        #region Internal functionality - don't modify

        public TestFixture()

        public void Dispose()


        /// <summary>
        /// This is run once, before any tests are executed.
        /// </summary>
        private void SetUp()
            Instance = Driver.Initialize();
            // Login
            Common.GoTo(Instance, "user/login");
            UserProfile user1 = new UserProfile(1);
            LoginPage.LogIn(Instance, user1);

            // TODO: Once "automation.testAdmin" user exists (with "IsAdmin = true") then
            //       add code here to ensure that Orgs/Repos/Users exist precisely in the
            //       'default' status they should.

        /// <summary>
        /// This is run once, after all tests have completed.
        /// </summary>
        private void TearDown()


Oscar_Wu on Wed, 16 Nov 2016 10:45:49

Hi Robert Werner,

>>I'm curious if this is the right approach or is there a better way?

According to your code, I think your code is a good way to implement your issue. Just as you said, under the same class, the test methods in VS could be run in parallel.

And I have not found any usefully information from selenium settings that could implement this idea, so I think using a signal instance is a good way.