I’m testing a website with Specflow and WebDriver.
I’m using events of specflow to start and quit the Driver. I start the Driver in [BeforeFeature] and quit it in [AfterFeature].
I use a static Common Class to hold the Driver and have the methods to operate it.
The problem happens after all tests are run on a feature and the next feature starts to run. I get the following errors.:
If in the [AfterFeature] I do a Driver.Close() I get the error:
-> error: Unexpected error. System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 127.0.0.1:7055
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception)
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context)
at System.Net.HttpWebRequest.GetRequestStream()
at OpenQA.Selenium.Remote.HttpCommandExecutor.Execute(Command commandToExecute) in c:\Projects\WebDriver\trunk\dotnet\src\WebDriver\Remote\HttpCommandExecutor.cs:line 91
at OpenQA.Selenium.Firefox.Internal.ExtensionConnection.Execute(Command commandToExecute) in c:\Projects\WebDriver\trunk\dotnet\src\WebDriver\Firefox\Internal\ExtensionConnection.cs:line 128
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(DriverCommand driverCommandToExecute, Dictionary`2 parameters) in c:\Projects\WebDriver\trunk\dotnet\src\WebDriver\Remote\RemoteWebDriver.cs:line 795
If I do a Driver.Quit() I get the error:
-> error: Cannot deserialize JSON object into type 'System.String'. Line 1, position 35.
If I don’t close or quit the driver, Every Features run with no problem, but I get lots of opened Firefox Windows.
My code is:
[Binding]
public class Events
{
[BeforeFeature]
public static void BeforeFeature()
{
Common.CreateDriver();
}
[AfterFeature]
public static void AfterFeature()
{
Common.QuitDriver();
}
}
And…
public static class Common
{
public static IWebDriver Driver { get; set; }
public static void CreateDriver(){
Driver = new FirefoxDriver();
Driver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, 5));
}
public static void CloseDriver()
{
Driver.Close();
}
public static void QuitDriver()
{
Driver.Quit();
}
}
Just found out what the problem was.
I will put it here for future reference.
The problem was in the way I was reading the Driver on my BaseWebObject, the father of all PageObjects.
I had this:
This Fails because the Driver will only be read once in the static constructor.
To make it work I must read the Driver at the instantiation time, so in the instance constructor like so: