Xunit create new instance of Test class for every new Test ( using WebDriver and C#)

前端 未结 3 1050
一向
一向 2021-01-04 22:56

Is there any way to run multiple tests in same browser using Webdriver (Selenium) using Xunit, , at present xunit launches new browser for every new test , below is the samp

相关标签:
3条回答
  • 2021-01-04 23:07

    after some investigation able to find the solution here it is and also updated FirefoxDriver to IWebDriver::

       public class SampleFixture : IDisposable
       {
        private IWebDriver driver;
        public SampleFixture()
        {
            driver = new FirefoxDriver();
            Console.WriteLine("SampleFixture constructor called");
    
        }
    
        public IWebDriver InitiateDriver()
        {
            return driver;
        }
    
        public void Dispose()
        {
           // driver.Close();
            driver.Quit();
            Console.WriteLine("Disposing Fixture");
        }
    }
    
    public class Class1 : IUseFixture<SampleFixture>
    {
        private IWebDriver driver;
    
        public void SetFixture(SampleFixture data)
        {
            driver = data.InitiateDriver();
        }
    
        [Fact]
        public void Test()
        {
            driver.Navigate().GoToUrl("http://google.com");
            driver.FindElement(By.Id("gbqfq")).SendKeys("Testing");
        }
    
        [Fact]
        public void Test2()
        {
            driver.Navigate().GoToUrl("http://google.com");
            driver.FindElement(By.Id("gbqfq")).SendKeys("Testing again");
        }
    }
    
    0 讨论(0)
  • 2021-01-04 23:22

    IUseFixture doesn't exist any more and seems replaced by IClassFixture. But I can't directly inject the FirefoxDriver as posted by @Mark Seemann:

    public class DashboardCategoryBoxes : IClassFixture<FirefoxDriver> {
        IWebDriver driver;
        public DashboardCategoryBoxes(FirefoxDriver driver) {
            //this.driver = wrapper.Driver;
            this.driver = driver;
        }
    }
    

    This throw an error

     System.AggregateException : One or more errors occurred. (Class fixture type 'OpenQA.Selenium.Firefox.FirefoxDriver' may only define a single public constructor.) (The following constructor parameters did not have matching fixture data: FirefoxDriver driver)
        ---- Class fixture type 'OpenQA.Selenium.Firefox.FirefoxDriver' may only define a single public constructor.
        ---- The following constructor parameters did not have matching fixture data: FirefoxDriver driver
    

    As a workaround, we could create some wrapper class without constructor

    public class FirefoxWrapper {
        FirefoxDriver driver = new FirefoxDriver();
        public FirefoxWrapper Driver {
            get { return driver; }
        }
    }
    

    and fetch the driver from there

    public class DashboardCategoryBoxes : IClassFixture<FirefoxWrapper> {
        IWebDriver driver;
        public DashboardCategoryBoxes(FirefoxWrapper wrapper) {
            driver = wrapper.Driver;
        }
    }
    
    0 讨论(0)
  • 2021-01-04 23:27

    While I don't know Selenium, I do know that xUnit.net creates a new instance of your test class for every test method, so that probably explains why you are seeing the behaviour you're reporting: the driver field is initialized anew for each test method, because the constructor is invoked every time.

    In order to reuse a single FirefoxDriver instance, you can use xUnit.net's IUseFixture<T> interface:

    public class Class1 : IUseFixture<FirefoxDriver>
    {
        private FirefoxDriver driver;
    
        public void SetFixture(FirefoxDriver data)
        {
            driver = data;
        }
    
        [Fact]
        public void Test()
        {
            driver.Navigate().GoToUrl("http://google.com");
            driver.FindElementById("gbqfq").SendKeys("Testing");
        }
    
        [Fact]
        public void Test2()
        {
            driver.Navigate().GoToUrl("http://google.com");
            driver.FindElementById("gbqfq").SendKeys("Testing again");
        }    
    }
    
    0 讨论(0)
提交回复
热议问题