1

I have a class called Driver and I'm trying to five its attributes to another class, so I can use it for the Locators, but it is giving me this error on the driver from the locator.

using OpenQA.Selenium;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Support.UI;

namespace AutomationTest
{
   public class BaseLocator
   {
       public static RemoteWebDriver driver;
       public static WebDriverWait wait;
       public IWebElement SearchBox => driver.FindElementByCssSelector("#twotabsearchtextbox");
       public IWebElement SearchButton => driver.FindElementByCssSelector("span#nav-search-submit-text + input");
       public IWebElement GoToShoppingButton => driver.FindElementByCssSelector("#nav-cart-count");
       public IWebElement GoToYourAmazonButton => driver.FindElementByCssSelector("a#nav-your-amazon");
   }
}

and I have Driver set up as:

using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Remote;

namespace AutomationTest
{
   public class Driver
   {
       public RemoteWebDriver SetUp()
       {
           RemoteWebDriver driver = new ChromeDriver();
           return driver;
       }
   }
}
AbdelAziz AbdelLatef
  • 3,650
  • 6
  • 24
  • 52
Alan Díaz
  • 43
  • 1
  • 8

1 Answers1

0

You need to initialize driver. Unless you plan on having multiple things to setup in the driver class, it's unnecessary (don't over think it).

this example initializes the driver object inline with the definition.

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Support.UI;

namespace AutomationTest
{
   public class BaseLocator
   {
       public static RemoteWebDriver driver = new ChromeDriver();
       //public static WebDriverWait wait; //TODO: Initialize variable as above

       public IWebElement SearchBox => driver.FindElementByCssSelector("#twotabsearchtextbox");
       public IWebElement SearchButton => driver.FindElementByCssSelector("span#nav-search-submit-text + input");
       public IWebElement GoToShoppingButton => driver.FindElementByCssSelector("#nav-cart-count");
       public IWebElement GoToYourAmazonButton => driver.FindElementByCssSelector("a#nav-your-amazon");
   }
}

while this example initializes it via the default constructor method:

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium.Support.UI;

namespace AutomationTest
{
   public class BaseLocator
   {
       public static RemoteWebDriver driver;
       //public static WebDriverWait wait;

       public BaseLocator()
       {
           driver = new ChromeDriver();
       }

       public IWebElement SearchBox => driver.FindElementByCssSelector("#twotabsearchtextbox");
       public IWebElement SearchButton => driver.FindElementByCssSelector("span#nav-search-submit-text + input");
       public IWebElement GoToShoppingButton => driver.FindElementByCssSelector("#nav-cart-count");
       public IWebElement GoToYourAmazonButton => driver.FindElementByCssSelector("a#nav-your-amazon");
   }
}

If you don't need driver outside of the BaseLocator class, then you should consider making it private. Not sure it needs to be declared as a static member either.

I'm unfamiliar with Selenium, so I approached this from a C# language angle- but it should get you down the track.

recommended reading on constructors: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/using-constructors

Andy Stagg
  • 373
  • 5
  • 22