Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 sample code

public class Class1

{
    private FirefoxDriver driver;
    public Class1()
    {
         driver = new FirefoxDriver();
    }

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

}
like image 942
user522170 Avatar asked Apr 02 '14 07:04

user522170


People also ask

Does xUnit create a new instance for each test?

xUnit.net creates a new instance of the test class for every test that is run, so any code which is placed into the constructor of the test class will be run for every single test.

What is used for the initialization of a test class in xUnit?

Hence, there are no [SetUp] and [TearDown] attributes in xUnit.net. Alternatively, xUnit developers use the constructor for initiazilation and IDisposable for the de-initialization of a test class.

How do you write xUnit test cases in .net core?

To write a test you simply create a public method that returns nothing and then decorate it with the Fact attribute. Inside that method you can put whatever code you want but, typically, you'll create some object, do something with it and then check to see if you got the right result using a method on the Assert class.


2 Answers

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");
    }    
}
like image 60
Mark Seemann Avatar answered Oct 11 '22 15:10

Mark Seemann


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");
    }
}
like image 2
user522170 Avatar answered Oct 11 '22 15:10

user522170