0

I an new in specflow i am implementing framework. When i create one feature file with multiple scenario and execute my test than it open one browser instance and run successful when i add one more feature file with multiple scenario and execute my test than it launch multiple browser instance one instance for each scenario can anyone help me out what's wrong in my code

Start.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TechTalk.SpecFlow;

namespace Orange_HRM
{
    class Start : SeleniumDriver
    {

      [BeforeScenarioBlock]
        public void Setup()
        {
            Intitialize();
            WebDriver.Navigate().GoToUrl(BaseAddress);
        } 

        [AfterScenarioBlock]
        public void TearDown()
        {
            Close();
        }
    }
}

SeleniumDriver.cs

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Orange_HRM
{
    class SeleniumDriver
    {

        public static IWebDriver WebDriver;


        public static string BaseAddress
        {
            get { return Constants.Url; }
        }

        public static void Intitialize()
        {
            WebDriver = new ChromeDriver();
            WebDriver.Manage().Window.Maximize();
            TurnOnWait();
        }

        public static void Navigate()
        {
            WebDriver.Navigate().GoToUrl(BaseAddress);
        }

        public static void Close()
        {
            WebDriver.Close();
        }

        public static void Quit()
        {
            WebDriver.Quit();
        }

        private static void TurnOnWait()
        {
            WebDriver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(5);
            WebDriver.Manage().Timeouts().PageLoad = TimeSpan.FromMinutes(2);
        }

    }
}
Ashish Verma
  • 19
  • 1
  • 9

2 Answers2

0

You are using SpecFlow of techtalk. Concept name is Hooks.
to perform automatic logic operations at specific timings.So, hooks are event bindings. running tests in multiple threads with SpecFlow+ Runner.

So, you can put orders,

class Start : SeleniumDriver
{
[BeforeScenario(Order = 0)]
public void Setup()
        {
            Intitialize();
            WebDriver.Navigate().GoToUrl(BaseAddress);
        } 

[BeforeScenario(Order = 1)]
public void TearDown()
        {
            Close();
        }
}

i like to suggest you dont use them, Bindings Class without Hooks. i.e. [BeforeTestRun] [AfterTestRun] use them instead.

[AfterScenario]
public void CleanUp()
{
    if (seleniumDriver != null)
    {
        SeleniumDriver.Dispose();
        seleniumDriver = null;
    }
}
Ashish Kamble
  • 2,555
  • 3
  • 21
  • 29
0

You can initialise and close the driver at Test Run level instead of scenario level. And maintain the url navigation alone at Scenario level. So that the driver will be initiated before test starts and quit after the test completes. Also the page will get refreshed before each scenario run.

namespace Orange_HRM
{
    class Start : SeleniumDriver
    {

      [BeforeTestRun]
        public static void Setup()
        {
            Intitialize();
        } 

        [AfterTestRun]
        public static void TearDown()
        {
            Quit();
        }

        [AfterScenarioBlock]
        public void navigateToUrl()
        {
            WebDriver.Navigate().GoToUrl(BaseAddress);
        } 
    }
}

Also you have used WebDriver.Close() instead WebDriver.Quit(). If we use WebDriver.Close() for the main window, then session will be terminated. Then if we try to access the same webdriver object again, it will throw No such session error.

WebDriver.Close() is meant for closing child window if we are working with mulitple windows. So to close main window, we have to use WebDriver.Quit() directly.

Navarasu
  • 8,209
  • 2
  • 21
  • 32
  • Hi Navarasu, I follow the same approach which you provide and now it's giving me an error, Test doesn't start just display an error. TearDown : System.NullReferenceException : Object reference not set to an instance of an object – Ashish Verma Oct 15 '18 at 14:54
  • Oops typo. I misplaced AfterScenarioBlock and AfterTestRun hooks. I edited above code. Can you check now with changed code? – Navarasu Oct 15 '18 at 15:05
  • Hi Navarasu, I tried with updated code but now its giving me an error while execute the test – Ashish Verma Oct 16 '18 at 07:06
  • I am getting following error NUnit3TestExecutor converted 6 of 6 NUnit test cases TearDown failed for test fixture CompleteAutomation.Feature.TestOrangeHRMLoginFunctionalityFeature TechTalk.SpecFlow.BindingException : The binding methods for before/after feature and before/after test run events must be static! CompleteAutomation:Orange_HRM.Hooks.Setup() TearDown : System.NullReferenceException : Object reference not set to an instance of an object. – Ashish Verma Oct 16 '18 at 07:16
  • I also change the Befor and after mehod to static and now i am getting the following error System.ArgumentExceptiion: The SearchContext of the locator object cannot be null Parameter name: locator Please suggest any other solution. Thanks in advance. – Ashish Verma Oct 16 '18 at 07:17
  • i think you need to explore more about basics – Ashish Kamble Oct 16 '18 at 09:37
  • Looks like your using PageFactory and the page factory get initiated before Initialiaze call. Check which line is calling it before calling Initialize method in debug mode. Add the test class and page object class to the quesion. – Navarasu Oct 16 '18 at 22:19