RE: Browser Factory or WebDriver Factory Tutorial

Near the end of the Browser Factory or WebDriver Factory Tutorial (http://toolsqa.com/selenium-webdriver/c-sharp/browser-factory-or-webdriver-factory/), there is a code block titled “LogInTest TestCase”. Inside that code snippet there is a variable named “driver” used and I don’t see where it is coming from? There is no BaseTest it is inherited from? What am I missing?

I’m trying to implement the BrowserFactory and PageFactory patterns, but I’m not sure where the IWebDriver should come from that is passed into the Page constructor.

crusaderDave Default Asked on August 24, 2016 in Selenium WebDriver.
Add Comment
3 Answers

I’ve resolved problem with the test. Use code below for both BrowserFactory.cs file and actual test and you should be good to go :-). I might have few extra methods in the test, but main problem was referencing Driver in the switch statement instead of referencing driver. Hope this is helps.

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.IE;
using System;
using System.Collections.Generic;

namespace SeleniumProject.WrapperFactory
{
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();
Drivers.Add(“IE”, Driver);
}
break;

case “Chrome”:
if (driver == null)
{
driver = new ChromeDriver();
Drivers.Add(“Chrome”, Driver);
}
break;
}
}

public static void LoadApplication(string url)
{
Driver.Url = url;
}

public static void CloseAllDrivers()
{
foreach (var key in Drivers.Keys)
{
Drivers[key].Close();
Drivers[key].Quit();
}
}
}
}

 

 

[Test]
public void Test()
{
BrowserFactory.InitBrowser(“Chrome”);
var driver = BrowserFactory.Driver;
BrowserFactory.LoadApplication(ConfigurationManager.AppSettings[“URL”]);

var homePage = new HomePage(driver);
homePage.ClickOnMyAccount();

var loginPage = new LoginPage(driver);
loginPage.LoginToApplication(“PageObjectLoginTest”);

loginPage.LogoutFromApp();

BrowserFactory.CloseAllDrivers();
}

Default Answered on June 9, 2017.
Add Comment

Your Answer

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