Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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;
             }
          }


   }
}
like image 325
Gita Avatar asked Jan 03 '12 14:01

Gita


2 Answers

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#.

like image 200
MostWanted Avatar answered Nov 30 '22 14:11

MostWanted


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");
                }
            }
        }

    }
}
like image 37
Gita Avatar answered Nov 30 '22 15:11

Gita