this is weird to me, it’s got to be something obvious but when using Selenium who knows…I haven’t used Selenium until now.
But here is just basic code I think and I think it’s just a syntax/OOP issue but not sure what.
Lets say I have a base class that has a driver property and a method where I’m getting the problem happening later on:
public Abstract BaseTest
{
protected IWebTestDriver TestDriver {get; set;}
...more code
protected void WaitForGridToRender(double seconds)
{
TestDriver.Wait(TimeSpan.FromSeconds(seconds));
}
}
I then have a test class called MyTests.cs that inherits BaseTest and in it an NUnit test:
[TestFixture]
public class MyTests : BaseTest
{
[Test]
public void SomeTab_WhenClicked_ShowsSomething()
{
SomeLandingPage someLandingPage = new SomeLandingPage(TestDriver);
mainLandingPage.NavigateToTheMainPage();
... rest of code
}
.. other test methods
}
And I have a 3rd class that serves as a “PageObject” that allows me to just reuse certain elements on the page, get at some things (more DRY) than have these same common methods repeated throughout my test methods:
public class MyPageObject : BaseWebTest
{
public void NavigateToTheMainPage()
{
// wait for the user to log in
WaitForGridToRender(5);
}
}
Now here’s where my problem is and how things are being called:
1) The Base class is first initialized…meaning that TestDriver’s instance is created and set to the TestDriver property so we can use it in subclasses
2) Eventually some code in another class that I did not mention here is called from the Base Class which just sends some calls to opens up FireFox, does some stuff…nothing special here
3) Eventually control goes to my test method and eventually hits the SomeTab_WhenClicked_ShowsSomething() method which then calls the MyPageObject.NavigateToTheMainPage() method.
The NavigateToTheMainPage() tries to call the BaseTest method called WaitForGridToRender and control eventually gets there.
Now the problem is, up till when WaitForGridToRender is called from within MyPageObject, that BaseTest.TestDriver instance has been there and accessible to both my test class and my PageObject class.
But when I finally debug and get to this line in WaitForGridToRender:
TestDriver.Wait(seconds);
Now for some reason, I get a null ref on TestDriver…the reference instance for that property is gone!
No idea why which is why I’m posting this cause I’m stuck as to why this might happen.
This code
declares a protected property of type
IWebTestDrivernamedTestDriver.Unless you’re setting
TestDriverto something in a constructor or method not shown here, the backing variable forTestDrivercontains a null reference.