2

This is my BrowserRunnerFactory.cs where I have defined to choose chrome and Firefox browser:

public class BrowserRunnerFactory
{
    public static IBrowserInterface InitiateBrowser(BrowserType BrowserType)
    {

        switch (BrowserType)
        {
            case BrowserType.Chrome:
                return new ChromeRunner();
            case BrowserType.Firefox:
            default:
                return new FirefoxRunner();
        }
    }

I have called it and defined two different filed browser and driver in a calss baseInitializer.cs:

public void GetDriver()
    {
      if (browser is null)
        { 
            browser = 
            BrowserRunnerFactory.InitiateBrowser(BrowserType.Chrome);
            driver = browser.Driver;
        }
    }

I'm trying to create automated testing and with browser I am trying to call chrome browser. And with the driver field I want to manipulate the selenium properties like driver.Manage(), driver.FindElement(By.Id()), driver.Close(),* etc.


So in the baseInitializer.cs class it is working fine. I am getting the value of both of the field browser and driver. The browser filed should be called only once in the baseInitializer class such that the chrome Browser opens up to run all UI Automated Tests.

But when I try to call driver from a separate class say ClientTab.cs where I am trying to test some tab of a page like shown below:
BaseInitializer baseinItializer = new Baseinitializer();
public IWebDriver driver = null;
 public bool TabIsOpen()
    {
        driver = baseInitializer.driver; 
        driver.Manage().Timeouts().PageLoad = TimeSpan.FromMinutes(1);
        try
        {
            driver.FindElement(By.XPath(_configModel.NewRepository)).Click();
            return true;
                       }
        catch (Exception e)
        {
            _logger.Log("Error occured while looking for CLientTabId:" + 
             e.Message);
            return false;
        }
        finally
        {
            driver.SwitchTo().DefaultContent();
        }
    }

After I run the test I will get the NullReferenceException : Object reference not set to an instance of an object. While Debugging I find that the driver is null. While I want the driver as defined in the GetDriver method

driver = browser.Driver()

I tried this design pattern for the first time and also I'm a beginner of in C#. So any feedback, as well as suggestion regarding my coding practices, would be so helpful.

sigdelsanjog
  • 499
  • 1
  • 11
  • 29
  • 1
    you set `public IWebDriver driver = null;` and there's nothing that sets it to any other value... you are not calling `GetDriver` either... something is missing here. – Kiril S. Sep 28 '18 at 15:22
  • @KirilS. I Added the missing part. But still driver is null. Please have a look again – sigdelsanjog Sep 28 '18 at 15:39

0 Answers0