ScenarioContet object not set to an instance of a object

685 views
Skip to first unread message

Matt Young

unread,
Nov 14, 2016, 10:40:24 AM11/14/16
to SpecFlow
Hello , 


Since I have updated specflow to 2.1 and remove baseclass.contrib from my project . Im using the below code  to  get the driver :


 public class DriverContext
    {
        private static IWebDriver _driver;
        public static IWebDriver Driver
        {
            get
            {
                
                if (!ScenarioContext.Current.ContainsKey("browser"))
                    ScenarioContext.Current["browser"] = new ChromeDriver();
                return ScenarioContext.Current["browser"]as ChromeDriver;
            }          
            
        }

When I run the specflow test im getting the following error:

Code  were it try to access Driver context class:

DriverContext.Driver.Navigate().GoToUrl(ConfigurationManager.AppSettings["seleniumBaseUrl"]);


Error

System.NullReferenceException: Object reference not set to an instance of an object.

Any help would be appreciated 

Sam Holder

unread,
Nov 14, 2016, 10:52:40 AM11/14/16
to specflow
When you debug through your code, which object is actually null?

--
You received this message because you are subscribed to the Google Groups "SpecFlow" group.
To unsubscribe from this group and stop receiving emails from it, send an email to specflow+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Matt Young

unread,
Nov 14, 2016, 11:03:48 AM11/14/16
to SpecFlow
Test 'M:PhoenixAutomation.BHGLoanPreQualificationRequiredFieldsSteps.GivenIHaveLoggedIntoPhoenix' failed: Object reference not set to an instance of an object.
System.NullReferenceException: Object reference not set to an instance of an object.
C:\Projects\PhoenixTestFramework\BHG.Master.Framework\BHGVisionAutomation1\Utilities\Driver.cs(22,0): at BHG.Test.MasterFramework.Utilities.DriverContext.get_Driver()
C:\Projects\PhoenixTestFramework\BHG.Master.Framework\BHGVisionAutomation1\BHGBaseObjects\BHGBasePage.cs(19,0): at BHG.Test.MasterFramework.BHGBaseObjects.BHGBasePage..ctor()
at PhoenixAutomation.BHGLoanPreQualificationRequiredFieldsSteps..ctor()

It returns if (!ScenarioContext.Current.ContainsKey("browser"))    as Null

Sam Holder

unread,
Nov 14, 2016, 11:21:01 AM11/14/16
to specflow
it looks like the ScenarioContext.Current is the null thing. I don't want to sound patronizing or mean, but really you should learn how to use the debugger, and identify exactly which object is null. this skill will save you many hours in your future. Set a break point on the if statement then add a watch for ScenarioContext.Current and see if it is null.

It looks like you are trying to use this object before specflow has initialized the ScenarioContext. Are you running the tests in parallel?

--

Matt Young

unread,
Nov 14, 2016, 11:27:38 AM11/14/16
to SpecFlow
Not running testing in Parallel. I use the debugger and the ScenarioContext.Current = NULL . Thanks you for your assistants
To unsubscribe from this group and stop receiving emails from it, send an email to specflow+u...@googlegroups.com.

Matt Young

unread,
Nov 14, 2016, 12:28:44 PM11/14/16
to SpecFlow
Any suggestions on how to fix this ?

Sam Holder

unread,
Nov 14, 2016, 12:52:06 PM11/14/16
to specflow
it would be helpful to see the constructors of:

BHGBaseObjects.BHGBasePage
PhoenixAutomation.BHGLoanPreQualificationRequiredFieldsSteps

and to know if you have any [BeforeScenario] or [BeforeFeature] methods 

To unsubscribe from this group and stop receiving emails from it, send an email to specflow+unsubscribe@googlegroups.com.

Matt Young

unread,
Nov 14, 2016, 1:05:10 PM11/14/16
to SpecFlow

it would be helpful to see the constructors of:

 

BHGBaseObjects.BHGBasePage

PhoenixAutomation.BHGLoanPreQualificationRequiredFieldsSteps

 

and to know if you have any [BeforeScenario] or [BeforeFeature] methods 

- show quoted text -

 

 

namespace BHG.Test.MasterFramework.BHGBaseObjects

{

    /// <summary>

    /// BasePage abstract class that holds intitialization for all derived page objects.

    /// </summary>

    public abstract class BHGBasePage :BHGBase

    {

   

            /// <summary>

            /// BasePage Constructor that initializes page elements for all child pages.

            /// </summary>

            protected BHGBasePage()

            {

                PageFactory.InitElements(DriverContext.Driver, this);

 

            }

 

        }

    }

 

 

PhoenixAutomation.BHGLoanPreQualificationRequiredFieldsSteps

 Is the step file for the specflow

 

namespace PhoenixAutomation

{

    [Binding]

    public class BHGLoanPreQualificationRequiredFieldsSteps : BHGBasePage

    {

        [Given(@"I have logged into Phoenix")]

        public void GivenIHaveLoggedIntoPhoenix()

        {

          

            DriverContext.Driver.Navigate().GoToUrl(ConfigurationManager.AppSettings["seleniumBaseUrl"]);

           // DriverContext.Driver.Navigate().GoToUrl("http://bhgdev/phoenix/#/login");

            DriverContext.Driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(30));

            DriverContext.Driver.Manage().Window.Maximize();

            DriverWait.Wait = new WebDriverWait(DriverContext.Driver, TimeSpan.FromSeconds(30));

            _CurrentPage = GetInstance<BHGLoginPage>();

            _CurrentPage = _CurrentPage.As<BHGLoginPage>().LoginAs("******").WithPassword("******").LoginAndPressSubmit();

            //Need to add login info or create login method??

        }

 

 

In my Hook.cs File I have

 

Before testRun

 

[BeforeTestRun]

        public static void BeforeTestRun()

        {

            var envVars = Environment.GetEnvironmentVariables();

            if(envVars["test"].Equals("bvt"))

                CreateTestRunBVT();

            else if (envVars["test"].Equals("regression"))

                CreateTestRunRegression();

            else

                throw new Exception("No matching categories!");

        }

 

After

[AfterScenario()]

        public void AfterScenario()

        {

            if (ScenarioContext.Current.TestError != null)

            {

                string fileNameBase = $"error_{ScenarioContext.Current.ScenarioInfo.Title.ToIdentifier()} {DateTime.Now.ToString("yyyyMMMMdd_HHmmss")}";

                Screenshot file = ((ITakesScreenshot)DriverContext.Driver).GetScreenshot();

                file.SaveAsFile(fileNameBase + "_screenshot.jpg", ImageFormat.Jpeg);

                if (ScenarioContext.Current.ContainsKey("Name"))

                    MongoDBHelper.DeleteApplicationByFirstName(ScenarioContext.Current["Name"].ToString());

                ClearBrowserCache();

                DriverContext.Driver.Quit();

                ReportTestResults();

            }

            else

            {

                if (ScenarioContext.Current.ContainsKey("MongoName"))

                    MongoDBHelper.DeleteApplicationByFirstName(ScenarioContext.Current["MongoName"].ToString());

                ClearBrowserCache();

                if (DriverContext.Driver != null)

                    DriverContext.Driver.Quit();

                DriverWait.Wait = null;

                ReportTestResults();

            }

        }

 

xDazedx

unread,
Nov 14, 2016, 2:35:43 PM11/14/16
to SpecFlow
I did not see a path to the chromedriver itself. Do you have the path set somewhere?

ie: 
                case "chrome":
                    ChromeOptions options = new ChromeOptions();
                    options.AddArguments("--disable-extensions");
                    _webDriver = new ChromeDriver(Path.Combine(GetBasePath, @"Drivers\"), options);
                    _wait = new WebDriverWait(_webDriver, TimeSpan.FromSeconds(10));
                    _webDriver.Manage().Window.Maximize();
                    break;


Matt Young

unread,
Nov 14, 2016, 3:04:32 PM11/14/16
to SpecFlow
Shouldn't have to define driver path,  it is using the default directory.  Currently the scenarioContext.current object is showing NULL  ... 

Matt Young

unread,
Nov 14, 2016, 3:20:51 PM11/14/16
to SpecFlow
By changing  to :

  public static IWebDriver Driver
        {
            get { return new ChromeDriver();
                
            }

Im able to have the browser launch. Only issue now is it loads  the domain site  then afterwards continue to open new windows.  How to I prevent the additional instances from launching after the test run have begun?  

Sam Holder

unread,
Nov 15, 2016, 6:03:02 AM11/15/16
to specflow
public static IWebDriver Driver
        {
            private static IWebDriver webdriver = new ChromeDriver();
            get { return webDriver;}
                
            }

?

Although you might be better following this sort of pattern:

public class WebDriverContext
{
     IWebDriver WebDriver{get;set;}
}

[Binding]
public class Hooks
{
    private WebDriverContext webDriverContext;
    public Hooks(WebDriverContext webDriverContext)
    { 
         this.webDriverContext = webDriverContext;
    }

    [BeforeScenario]
    public void BeforeScenario()
    {
         webDriverContext.WebDriver = new ChromeDriver();
    }
}

[Binding]
public class MySteps
{
    private WebDriverContext webDriverContext;
    
    public MySteps(WebDriverContext webDriverContext)
    {
         this.webDriverContext = webDriverContext;
    }

    [Given("Something")]
    public void Somehting()
    {
         webDriverContext.WebDriver.Navigate().GoToUrl(ConfigurationManager.AppSettings["seleniumBaseUrl"]);
    }
}

to ensure you get a new driver for each scenario.



--
You received this message because you are subscribed to the Google Groups "SpecFlow" group.
To unsubscribe from this group and stop receiving emails from it, send an email to specflow+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages