4

I'm learning Selenium/Webdriver NUnit testing and run into a problem when executing a test:

using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using NUnit.Framework;

class NUnitTest
{
    IWebDriver driver;
    FirefoxOptions options;

   [SetUp]
    public void Initialize()
    {
        driver = new FirefoxDriver();
    }

    [Test]
    public void OpenAppTest()
    {
       driver.Navigate().GoToUrl("http:/www.demoqa.com");
    }

    [TearDown]
    public void EndTEst()
    {
        driver.Quit();
    }
}

When running test, I'm getting an exception:

OpenQA.Selenium.WebDriverException: Cannot start the driver service on localhost TearDown: System.NUllReferenceException: Object reference not set to an instance of an object

I have no clue how to fix it.

However, the following approach works:

using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;    
using Microsoft.VisualStudio.TestTools.UnitTesting;

public class UnitTest1
{
    IWebDriver driver;
    [TestMethod]
    public void VerifyTitle()
    {
        //Write Actual Test
        string title = driver.Title;
        Assert.AreEqual(title, "Demoqa | Just another WordPress site");
    }

    [TestInitialize]
    public void Setup()
    {
        //start browser and open url
        driver = new FirefoxDriver();
        driver.Navigate().GoToUrl("http:/www.demoqa.com");
    }

    [TestCleanup]
    public void CleanupTest()
    {
        //close browser
        driver.Quit();
    }
}

Here, I'm using the same approach to start the webdriver as in the failing example. The only difference is that the failing example is using NUnit.Framework and the correct one is using Microsoft.VisualStudio.TestTools.UnitTesting

I'm not sure what is wrong with the second approach?

gene
  • 2,098
  • 7
  • 40
  • 98
  • I would recommend editing your title to make it clear that your problem is with WebDriver and "NUnit" specifically. I am curious why it works through a MS unit test but not with NUnit. – jibbs Dec 02 '16 at 23:09
  • What version of the .NET bindings are you using? What version of Firefox? From your description, it sounds like you're using version 3.0.0 of Selenium, and a recent version of Firefox.(version >= 48). If so, you need the [geckodriver executable](https://github.com/mozilla/geckodriver/releases) to go along with your project. – JimEvans Dec 02 '16 at 23:48
  • I had similar issues with NUnit and it was because the actual exe that runs the tests is not in the same directory as your project's bin output. I used: driver = new FirefoxDriver(FirefoxDriverService.CreateDefaultService("path to driver directory")); – mbrdev Jun 23 '17 at 13:41

0 Answers0