0

我正在使用Specflowand测试一个网站WebDriver。我正在使用 specflow 事件来启动和退出驱动程序。我启动驱动程序[BeforeFeature]并退出它[AfterFeature]

我使用静态公共类来保存驱动程序并拥有操作它的方法。

在对一个功能运行所有测试并且下一个功能开始运行之后,就会出现问题。我收到以下错误:

如果在[AfterFeature]我做一个Driver.Close()我得到错误:

    -> 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

如果我执行Driver.Quit()我会收到错误消息:

-> error: Cannot deserialize JSON object into type 'System.String'. Line 1, position 35.

如果我不关闭或退出驱动程序,每个功能都可以正常运行,但我会打开很多 Firefox Windows。

我的代码是:

[Binding]
public class Events
{
    [BeforeFeature]
    public static void BeforeFeature()
    {
        Common.CreateDriver();
    }

    [AfterFeature]
    public static void AfterFeature()
    {
        Common.QuitDriver();
    }
}

和...

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();
    }
}
4

1 回答 1

1

才发现问题出在哪里。我会把它放在这里以供将来参考。

问题在于我在 BaseWebObject(所有 PageObjects 之父)上读取驱动程序的方式。

我有这个:

    public class BaseWebObject
{        
    protected static IWebDriver Driver = Common.Driver;

}   

这失败了,因为驱动程序只会在静态构造函数中被读取一次。

为了使它工作,我必须在实例化时读取驱动程序,所以在实例构造函数中像这样:

    public class BaseWebObject
{        
    protected static IWebDriver Driver;

    public BaseWebObject()
    {
        Driver = Common.Driver;
    }    
}
于 2012-04-11T16:33:31.907 回答