title: Call Methods from two classes in Selenium C#
body: <p>This is probably a stupid question but I haven't found an answer that leads me to a solution yet</p>
  
  <p>Say I have a testmethod to verify the functionality of a login portal.  It's in TestClassA.
  I want to run that method in TestClassB's TestInitialize method so I can reliably have selenium start on a blank slate for testing features past that login portal.</p>
  
  <p>Here's the test code in question</p>
  
  <pre><code>using Login_Elements;
  using Dashboard;
  
  namespace Test_Dashboard_Elements
  {
  [TestClass]
  public class DashboardTests
  {
      IWebDriver _driver;
      DashboardElements dash;
  
      [TestInitialize]
      public void Test_Setup()
      {
          dash = new DashboardElements(_driver);
          LoginPage login = new LoginPage(_driver);
          _driver = new FirefoxDriver();
          _driver.Navigate().GoToUrl("exampleurl/login");
          login.Login();
      }
  }
  </code></pre>
  
  <p>Which calls an instance of DashboardElements and passes the selenium webdriver, then calls an instance of LoginPage and passes the selenium webdriver (which I assume is the issue), and calls the login method from LoginPage</p>
  
  <pre><code>    IWebDriver _driver;
  
      //Username field
      [FindsBy(How = How.Id, Using = "username")]
      private IWebElement userName;
  
      //Password field
      [FindsBy(How = How.Id, Using = "password")]
      private IWebElement password;
  
      //Submit Button
      [FindsBy(How = How.ClassName, Using = "btn")]
      private  IWebElement submit_button;
  
      //Constructor
      public LoginPage(IWebDriver driver)
      {
          this._driver = driver;
          PageFactory.InitElements(driver, this);
      }
  
      //Sends passed string to username field
      public void sendUserName(string strUsername)
      {
          userName.SendKeys(strUsername);
      }
  
      //Sends passed string to password field
      public void sendPassword(string strPassword)
      {
          password.SendKeys(strPassword);
      }
  
      //Clicks submit button
      public void submit()
      {
          submit_button.Click();
      }
  
      public void Login()
      {
          sendUserName("username");
          sendPassword("password!");
          submit();
      }
  </code></pre>
  
  <p><strong>This returns</strong></p>
  
  <pre><code>&gt;Message: Initialization method Test_Dashboard_Elements.DashboardTests.Test_Setup threw exception.  System.ArgumentNullException: System.ArgumentNullException: searchContext may not be null Parameter name: searchContext
  </code></pre>
  
  <p>I feel like this has to do with passing _driver twice but I'm not sure how else to do it</p>
  
  <p><strong>Stack trace:</strong></p>
  
  <pre><code>&gt;Test Name: Test_Link_Reports
  Test FullName:  Test_Dashboard_Elements.DashboardTests.Test_Link_Reports
  Test Source:    c:\Users\%USER%\Documents\Visual Studio 2013\Projects\%PATH TO DIR%\Page Tests\Dashboard Tests.cs : line 29
  Test Outcome:   Failed
  Test Duration:  0:00:06.0255821
  </code></pre>
  
  <p><strong>Result Message:</strong> </p>
  
  <pre><code>&gt;Initialization method Test_Dashboard_Elements.DashboardTests.Test_Setup threw exception. System.ArgumentNullException: System.ArgumentNullException: searchContext may not be null
  Parameter name: searchContext.
  Result StackTrace:  
  at OpenQA.Selenium.Support.PageObjects.DefaultElementLocatorFactory.LocateElement(ISearchContext searchContext, IEnumerable`1 bys)
     at OpenQA.Selenium.Support.PageObjects.WebElementProxy.get_WrappedElement()
     at OpenQA.Selenium.Support.PageObjects.WebElementProxy.SendKeys(String text)
     at Login_Elements.LoginPage.sendUserName(String strUsername) in c:\Users\%USER%\Documents\Visual Studio 2013\Projects\%PATH TO DIR%\Page Elements\Login Elements.cs:line 39
     at Login_Elements.LoginPage.Login() in c:\Users\%USER%\Documents\Visual Studio 2013\Projects\%PATH TO DIR%\Page Elements\Login Elements.cs:line 56
     at Test_Dashboard_Elements.DashboardTests.Test_Setup() in c:\Users\%USER%\Documents\Visual Studio 2013\Projects\%PATH TO DIR%\Page Tests\Dashboard Tests.cs:line 24
  DefaultElementLocatorFactory.LocateElement(ISearchContext searchContext, IEnumerable 1 bys)
  WebElementProxy.get_WrappedElement()
  WebElementProxy.SendKeys(String text)
  LoginPage.sendUserName(String strUsername)
  LoginPage.Login()
  DashboardTests.Test_Setup()
  </code></pre>