object reference not set to an instance of an object

using OpenQA.Selenium;

using OpenQA.Selenium.Chrome;

using OpenQA.Selenium.Firefox;

using OpenQA.Selenium.IE;

using System;

using System.Collections.Generic;
namespace Automation_Tests.WrapperFactory

{

public class BrowserFactory

{

private static readonly IDictionary<string, IWebDriver> Drivers = new Dictionary<string, IWebDriver>();

private static IWebDriver driver;
public static IWebDriver Driver

{

get

{

if (driver == null)

throw new NullReferenceException("The WebDriver browser instance was not initialized. You should first call the method InitBrowser.");

return driver;

}

private set

{

driver = value;

}

}
public static void InitBrowser(string browserName)

{

switch (browserName)

{

case "Firefox":

if (Driver == null)

{

driver = new FirefoxDriver();

Drivers.Add("Firefox", Driver);

}

break;
case "IE":

if (Driver == null)

{

driver = new InternetExplorerDriver(@"C:\PathTo\IEDriverServer");

Drivers.Add("IE", Driver);

}

break;
case "Chrome":

if (Driver == null)

{

driver = new ChromeDriver();//@"C:\PathTo\CHDriverServer"

Drivers.Add("Chrome", Driver);

}

break;

}

}
public static void LoadApplication(string url)

{

Driver.Url = url;

driver.Manage().Window.Maximize();

driver.Navigate().GoToUrl("javascript:document.getElementById('overridelink').click()");

}
public static void CloseAllDrivers()

{

foreach (var key in Drivers.Keys)

{

Drivers[key].Close();

Drivers[key].Quit();

}

}

}

}

I copied above code from  “browser factory” article (on  tools-qa site).

From my test class, I try run this:

[SetUp]

public void setup()

{

BrowserFactory.InitBrowser("Chrome");
.........

}

But, it failed, because it didn’t initialized the “driver” variable.

(I got msg: system.nullReferenceException: The WebDriver browser instance was not initialized. You should first call the method InitBrowser)

Thank you a lot!

Add Comment
  • 0 Answer(s)
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.