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

Asked

Viewed 1,351 times

2

I am developing tests with Selenium webdriver in C#, for each test class the browser is initialized and after running all 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 class "Seleniumbase" for the methods [Testfixturesetup] and [Testfixtureteardown]

  • A suggestion is to divide the test into "@Before", "@Test" and "@After", and put the tearDown() method into "@After". Or use a Try, catch, Finally and call tearDown() { Selenium.stop(); } in Finally.

1 answer

1

The best way I could find was to implement the Singleton standard 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 above class will already solve your problem. Prorem I had the need to implement for other Webdrives. So I 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;
    }

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.