Luis Valencia
Luis Valencia

Reputation: 33998

Cannot find a file named webdriver.json' or an embedded resource with the id 'WebDriver.FirefoxPreferences

I am trying to do some selenium tests on my new unit test project:

My code is:

using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.PhantomJS;
using System;

namespace HelloWorldDevOpsUnitTests
{
    [TestClass]
    public class ChucksClass1
    {
        private string baseURL = "http://localhost:5000/";
        private RemoteWebDriver driver;
        private string browser;
        public TestContext TestContext { get; set; }

        [TestMethod]
        [TestCategory("Selenium")]
        [Priority(1)]
        [Owner("FireFox")]

        public void TireSearch_Any()
        {
            driver = new FirefoxDriver(new FirefoxBinary(@"C:\Program Files\Mozilla Firefox\Firefox.exe"), new FirefoxProfile(), TimeSpan.FromMinutes(10));
            driver.Manage().Window.Maximize();
            driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(30));
            driver.Navigate().GoToUrl(this.baseURL);
            driver.FindElementById("search - box").Clear();
            driver.FindElementById("search - box").SendKeys("tire");
            //do other Selenium things here!
        }

        [TestCleanup()]
        public void MyTestCleanup()
        {
            driver.Quit();
        }

        [TestInitialize()]
        public void MyTestInitialize()
        {
        }
    }
}

I got these errors when running:

Message: Test method HelloWorldDevOpsUnitTests.ChucksClass1.TireSearch_Any threw exception: OpenQA.Selenium.WebDriverException: Cannot find a file named 'C:\Users\valencil\DevOpsTest\HelloWorldDevOpsUnitTests\bin\Debug\webdriver.json' or an embedded resource with the id 'WebDriver.FirefoxPreferences'. TestCleanup method HelloWorldDevOpsUnitTests.ChucksClass1.MyTestCleanup threw exception. System.NullReferenceException: System.NullReferenceException: Object reference not set to an instance of an object..

Upvotes: 4

Views: 976

Answers (1)

Treker
Treker

Reputation: 398

When you build your project, the Selenium web driver binary should be copied to the output directory of your test project (e.g. /bin/Debug/netcoreapp2.0/). Try passing in the relative path to this binary to the XDriver constructor:

IWebDriver driver;
switch (browser.ToLower())
{
    case "firefox":
       driver = new FirefoxDriver("./");
    break;
    case "chrome":
       driver = new ChromeDriver("./");
    break;
    case "ie":
    case "internetexplorer":
       driver = new InternetExplorerDriver("./");
    break;
    case "phantomjs":
       driver = new PhantomJSDriver("./");
    break;
    default:
        driver = new PhantomJSDriver("./");
    break;   
}

Upvotes: 1

Related Questions