Why does Selenium WebDriver not quit Firefox after a failed test under MSBuild?


I'm using Selenium WebDriver in .NET environment. I'm using WebDriver inside NUnit test runner like that:

    public static void Should_register_user()
        IWebDriver driver = new FirefoxDriver();



It works quite fine. If I run this test from MSBuild script like that: packages\NUnit.Runners.2.6.2\Tools\nunit-console.exe /nologo S022.SeleniumTests.dll /xml=NUnit.Report.xml (no matter if I'm using NUnit community task or exec task). The test performs but it it fails the Firefox browser remains opened and the execution in script waits for me to close the browser window. When I do that the script executes further.

Here it is what Selenium says:

ProcessModel: Default    DomainUsage: Single
  Execution Runtime: net-3.5
  Tests run: 1, Errors: 0, Failures: 1, Inconclusive: 0, Time: 10,3118323 secon
    Not run: 0, Invalid: 0, Ignored: 0, Skipped: 0

  Errors and Failures:
  1) Test Failure : S022.SeleniumTests.RegistrationTests.Should_register
       Expected string length 6 but was 11. Strings differ at index 0.
    Expected: "Log-in"
    But was:  "News"

  at S022.SeleniumTests.RegistrationTests.Should_register_user() in S022.SeleniumTests\T
  ests\RegistrationTests.cs:line 22

If the test passes the execution goes along as expected.

What am I doing wrong?

You're creating and destroying the browser instance within the [Test] method. When the test case fails, an exception is thrown, and your call to driver.Quit() is never getting executed. You probably want to create the driver instance in the [SetUp] method, and call driver.Quit() in the [TearDown] method.