Close the browser only after running all tests with Selenium Webdriver C #

2

I'm developing tests with Selenium webdriver in C #, for each test class the browser is initialized and after running all the tests of this class the browser is closed. How do I initialize only once and close only after running all tests of all classes?

I created a "SeleniumBase" class for the [TestFixtureSetUp] and [TestFixtureTearDown] methods

    
asked by anonymous 21.09.2015 / 19:40

1 answer

1

The best way I found was to implement the Singleton pattern for the IWebDriver object.

Ex:

Base class:

    public class Base
    {
        public readonly IWebDriver driverIe;

        public Base() 
        {
            driverIe = SingletonWebDriver.GetInstance().GetDriver();
        }
...

Singleton WebDriver class:

public class SingletonWebDriver
    {

        private string IEServerselenium = ConfigurationManager.AppSettings["IEDriverServer"].ToString();
        private IWebDriver _driver;

        private static SingletonWebDriver _uniqueInstance;

        /// <summary>
        /// Construtor receberá qualquer classe que implemente IWebDriver conforme fala a assinatura da classe  
        /// </summary>
        /// <param name="type">Qual quer classe que implemente a interface IWebDriver</param>
        public SingletonWebDriver() 
        {
            _driver = GetWebDriver();
        }

        /// <summary>
        /// Retorna uma nova instancia de WebDriver de acordo com o tipo
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        private IWebDriver GetWebDriver()
        {
            return new InternetExplorerDriver(IEServerselenium);
        }

        /// <summary>
        /// Retorna o WebDriver 
        /// </summary>
        /// <returns></returns>
        public IWebDriver GetDriver()
        {
            return _driver;
        }

        public static SingletonWebDriver GetInstance()
        {            
            if (_uniqueInstance == null)
            {
                _uniqueInstance = new SingletonWebDriver();
            }

            return _uniqueInstance;
        }

    }

The class above will solve your problem. Prorem I had the need to implement for other Webdrives. So I've implemented a solution with generics.

    /// <summary>
    /// Classe responsável por impedir que existam várias instancias de WebDriver, 
    /// o tipo T apenas pode ser uma classe que implementa IWebDriver
    /// </summary>
    /// <typeparam name="T"></typeparam>

public class SingletonWebDriver<T> where T : IWebDriver
{
    /// <summary>
    /// Pega o caminho do executável para criar o web driver do IE 
    /// </summary>
    private string IEServerselenium = ConfigurationManager.AppSettings["IEDriverServer"].ToString();
    private IWebDriver _driver;

    /// <summary>
    /// Atributo estatico onde terá a unica intancia do web driver
    /// </summary>
    private static SingletonWebDriver<T> _uniqueInstance;

    /// <summary>
    /// Construtor receberá qualquer classe que implemente IWebDriver conforme fala a assinatura da classe  
    /// </summary>
    /// <param name="type">Qual quer classe que implemente a interface IWebDriver</param>
    public SingletonWebDriver() 
    {
        _driver = GetWebDriver();
    }

    /// <summary>
    /// Retorna uma nova instancia de WebDriver de acordo com o tipo
    /// </summary>
    /// <param name="type"></param>
    /// <returns></returns>
    private IWebDriver GetWebDriver()
    {
        if(typeof(T) == typeof(InternetExplorerDriver))
        {
            return new InternetExplorerDriver(IEServerselenium);
        }
        else if (typeof(T) == typeof(FirefoxDriver))
        {
            return new FirefoxDriver();
        }
        else if (typeof(T) == typeof(OperaDriver))
        {
            return new OperaDriver();
        }

        return null;
    }

    /// <summary>
    /// Retorna o WebDriver 
    /// </summary>
    /// <returns></returns>
    public IWebDriver GetDriver()
    {
        return _driver;
    }

    /// <summary>
    /// Retorna a unica instancia de SingletonWebDriver caso ainda não tenha sido instanciado
    /// </summary>
    /// <param name="type"></param>
    /// <returns></returns>
    public static SingletonWebDriver<T> GetInstance()
    {            
        if (_uniqueInstance == null)
        {
            _uniqueInstance = new SingletonWebDriver<T>();
        }

        return _uniqueInstance;
    }

}
    
30.09.2015 / 19:27