How can I share one Selenium webdriver instance in NUnit and C#?
Asked Answered
F

3

5

I want easy way to launch Selenium webdriver instance and run various tests on it. I'm trying to do this in Suite file, but it doesn't work. Instance is killed instantly. Is there any alternatives on how to do this?

Potentially I want to add more drivers (IE, Chrome) in this suite and if possible launch separately. Any suggestions welcome.

namespace NUnit.Tests
{
   public class AllTests
   {
        private static IWebDriver _Driver;

        [TestFixtureSetUp]
        public void SuiteSetUp() 
        {
           _Driver = new FirefoxDriver();
         }

        [TestFixtureTearDown]
        public void SuiteTearDown()
        {
           try
           {
              _Driver.Quit();
           }
              catch (Exception)
           {
                    // Ignore errors if unable to close the browser
            }
         }

         [Suite]
         public static TestSuite Suite
         {
            get
            {
               LoginTest lt = new LoginTest { Driver=_Driver };
               suite.Add(lt);
               AnotherTest at = new AnotherTest { Driver=_Driver };
               suite.Add(at);
               return suite;
             }
          }


   }
}
Flick answered 3/1, 2012 at 14:15 Comment(0)
F
3

Trying to run this with base class / extended classes failed. As webdriver instance didn't get initialized properly and couldn't be killed properly. Instead I created SetupIE(), SetupChrome(), SetupFirefox() methods in Suite and also created teardown method that would work as last test for suite.

Here is the code:

namespace TestNamespace
{
    using System;
    using NUnit.Framework;
    using NUnit.Core;
    using SeleniumTests;
    using OpenQA.Selenium;
    using OpenQA.Selenium.IE;
    using OpenQA.Selenium.Firefox;
    using OpenQA.Selenium.Chrome;
    using OpenQA.Selenium.Support.UI;

    class AllTests
    {
        public static IWebDriver WebDriver { get; private set; }

        [Suite]
        public static TestSuite Suite
        {
            get
            {
                TestSuite suite = new TestSuite("All Tests");

                //Setup a Web driver (see methods below for different browsers) - SetupIE(), SetupChrome(), SetupFirefox()
                SetupIE();

                // Add tests to suite
                suite.Add(new FlashLoadedTest { Driver = WebDriver });

                // Tear down a Web driver
                suite.Add(new TearDownTest { DriverToTearDown = WebDriver });

                // return suite to NUnit
                return suite;
            }
        }

        // Method that's initialises FireFox Driver
        private static void SetupFireFox()
        {
            WebDriver = new FirefoxDriver();
        }

        // Method that's initialises IE Driver
        private static void SetupIE()
        {
            WebDriver = new InternetExplorerDriver(); 
        }


        // Can't get this working, but this is how its supposed to work
        private static void SetupChrome()
        {
            WebDriver = new ChromeDriver(@"C:\Users\<user>\AppData\Local\Google\Chrome\Application");
        }


        // Class with a test that tears down browser instance
        [TestFixture]
        class TearDownTest
        {
            public IWebDriver DriverToTearDown;

            [Test]
            public void TearDownBrowser()
            {
                if (DriverToTearDown == null)
                    Assert.Fail("No Browser to Tear Down");

                try
                {
                    DriverToTearDown.Close();
                    DriverToTearDown.Dispose();
                }
                catch
                {
                    Assert.Fail("Browser failed to tear down");
                }
            }
        }

    }
}
Flick answered 12/3, 2012 at 15:7 Comment(0)
A
3

I did this in Java, I made a base class, declared the webdriver as static, put my startup/config methods in this class and then extended it in to each test class i made.

Im sure its the same for C#.

Anemograph answered 3/1, 2012 at 14:39 Comment(3)
I will try that. Any ideas how to launch different browsers parallely in this case?Flick
I used TestNG, I then used its parametrized variables feature to run various browsers. It also has an option for parallel execution. Sorry but im not familiar with NUnit.Anemograph
After trying this with base class and extended classes, it didn't work. So I created separate tests in Suite to setup and teardown browsers. Added those as first / last to the suite and it worked like magic. Thanks for comments though.Flick
F
3

Trying to run this with base class / extended classes failed. As webdriver instance didn't get initialized properly and couldn't be killed properly. Instead I created SetupIE(), SetupChrome(), SetupFirefox() methods in Suite and also created teardown method that would work as last test for suite.

Here is the code:

namespace TestNamespace
{
    using System;
    using NUnit.Framework;
    using NUnit.Core;
    using SeleniumTests;
    using OpenQA.Selenium;
    using OpenQA.Selenium.IE;
    using OpenQA.Selenium.Firefox;
    using OpenQA.Selenium.Chrome;
    using OpenQA.Selenium.Support.UI;

    class AllTests
    {
        public static IWebDriver WebDriver { get; private set; }

        [Suite]
        public static TestSuite Suite
        {
            get
            {
                TestSuite suite = new TestSuite("All Tests");

                //Setup a Web driver (see methods below for different browsers) - SetupIE(), SetupChrome(), SetupFirefox()
                SetupIE();

                // Add tests to suite
                suite.Add(new FlashLoadedTest { Driver = WebDriver });

                // Tear down a Web driver
                suite.Add(new TearDownTest { DriverToTearDown = WebDriver });

                // return suite to NUnit
                return suite;
            }
        }

        // Method that's initialises FireFox Driver
        private static void SetupFireFox()
        {
            WebDriver = new FirefoxDriver();
        }

        // Method that's initialises IE Driver
        private static void SetupIE()
        {
            WebDriver = new InternetExplorerDriver(); 
        }


        // Can't get this working, but this is how its supposed to work
        private static void SetupChrome()
        {
            WebDriver = new ChromeDriver(@"C:\Users\<user>\AppData\Local\Google\Chrome\Application");
        }


        // Class with a test that tears down browser instance
        [TestFixture]
        class TearDownTest
        {
            public IWebDriver DriverToTearDown;

            [Test]
            public void TearDownBrowser()
            {
                if (DriverToTearDown == null)
                    Assert.Fail("No Browser to Tear Down");

                try
                {
                    DriverToTearDown.Close();
                    DriverToTearDown.Dispose();
                }
                catch
                {
                    Assert.Fail("Browser failed to tear down");
                }
            }
        }

    }
}
Flick answered 12/3, 2012 at 15:7 Comment(0)
S
3

I appreciate this is a little late but may prove useful for future readers.

I created a base class containing a firefox driver with the following and it works perfectly for me. You can then simply reference the base class (Driver in this instance) from your derived test class. Worth noting I'm using C# and Nunit.

Code for base class is:

namespace yournamespace
{
    public class Driver
    {
        public IWebDriver driver;
        public StringBuilder verificationErrors;

        public Driver()
        {
            driver = new FirefoxDriver(); //replace with required driver
            verificationErrors = new StringBuilder();
        }

    }
}

Then simply called the 'Driver' class from my test class:

[TestFixture]
public class IMSLogin : Driver
{
   //.. all the usual bits and bobs!
Stellastellar answered 12/4, 2012 at 11:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.