Selenium NUnit安装程序似乎不会在第一次测试完成后调用WebDriver

Selenium NUnit安装程序似乎不会在第一次测试完成后调用WebDriver,selenium,nunit,Selenium,Nunit,我遇到了一个似乎与webdriver/nunit有关的问题 在初次运行时,我的第一个测试[Setup]和[Teardown]工作。当系统试图运行我的下一个测试时,它需要一个[Setup],它似乎没有调用WebDriver [SetUp] //This has to be done for all tests. It is the setup I say! public void Setup() { LaunchBrowser(); driver.Manage().Timeout

我遇到了一个似乎与
webdriver/nunit
有关的问题

在初次运行时,我的第一个测试
[Setup]
[Teardown]
工作。当系统试图运行我的下一个测试时,它需要一个
[Setup]
,它似乎没有调用
WebDriver

[SetUp]
//This has to be done for all tests.  It is the setup I say!
public void Setup()
{
    LaunchBrowser();
    driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(45));
}

[TearDown]
//This has to be done for all tests.  It is the teardown I say!
public void TearItDown()
{
      driver.Dispose();
}
如果我注释掉
[Teardown]
,那么我所有的测试运行都没有问题

LaunchBrowser()

public static IWebDriver driver = new InternetExplorerDriver();

public string landingPage = "http://www.smartdrive.net";

public void LaunchBrowser() 
{
    driver.Navigate().GoToUrl(landingPage);
}
我在上一次测试中尝试添加
驱动程序.Dispose()计算我可以在所有测试运行后关闭会话,但它似乎不起作用

[Teardown]
就位时收到错误

Result Message: 
OpenQA.Selenium.WebDriverException : 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:51089
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, 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)
at OpenQA.Selenium.Remote.DriverServiceCommandExecutor.Execute(Command commandToExecute)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
Result StackTrace:  
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(String mechanism, String value)
at OpenQA.Selenium.By.FindElement(ISearchContext context)
at UnitTestProject1.Browser_Landing.getLandingPlacement() in c:\Users\erikag\Desktop\AutomationTests\VS_NetSeleniumTest\UnitTestProject1\UnitTestProject1\Browser_Landing.cs:line 30
            at UnitTestProject1.ServiceConsoleTest.PlacementChk() in c:\Users\erikag\Desktop\AutomationTests\VS_NetSeleniumTest\UnitTestProject1\UnitTestProject1\TestSuite_LandLog.cs:line 46

我确实在某个地方读到,这个问题与
webdriver
相关,在
[Teardown]
之后重新启动时出现问题,但我似乎不知道如何解决这个问题。

您在每次运行之后都会处理驱动程序,但在每次运行之前都不会实例化。所以驱动程序不能按预期工作。在
LaunchBrowser()方法中实例化驱动程序,然后重新运行测试

只需更多信息
Dispose()
重置潜水员,因此该实例对于您发布的代码块不再有效。看

而且,最好的做法可能是使用
Quit
而不是
Dispose
,因为
Quit()
调用
Dispose
,并进行更多的清理,所以您不必单独使用
Dispose

并且,更改后的
LaunchBrowser()
方法应如下所示:

public IWebDriver Driver;

public string landingPage = "http://www.smartdrive.net";

public void LaunchBrowser() 
{
    driver = new InternetExplorerDriver();
    driver.Navigate().GoToUrl(landingPage);
}

谢谢你的回复。我换成了司机。退出();并且得到了相同的错误:[拆卸]//所有测试都必须这样做。这是我说的眼泪!public void tearitton(){driver.Quit();}还有其他想法吗?您需要在
LaunchBrowser()
public void LaunchBrowser(){driver=new InternetExplorerDriver();driver.Navigate().gotour(landingPage);}中实例化驱动程序
我的launchbrowser浏览器中的编辑不是已经存在了吗?如果这就是问题所在,我的第一次测试不是失败了吗?第一次测试没有问题,我的所有测试都通过了。那么它会与WebDriver的安装相关吗?如果这就是问题所在,那么不是所有的测试都会在安装时失败吗?抱歉,我将测试firefox驱动程序,看看我是否有任何不同的行为,我倾向于发现很多都与IE驱动程序有关,但如果是这样的话,我必须使用IE进行测试。感谢您的帮助@不,不是。我正在用
driver=newinternetexplorerdriver()实例化
driver
内部
LaunchBrowser()
你在外面做一次。它第一次工作的原因是
LaunchBrowser()
方法所在的类第一次实例化。之后,您将处理驱动程序,并调用
LaunchBrowser()
方法,该方法没有
driver=new InternetExplorerDriver()行,因此不实例化驱动程序。希望有意义现在有意义了,谢谢!我现在正在做,它完全起作用了!我真的很感谢你的解释。正是这些小的安置细节,我仍然在学习不要想当然。我真的很感谢你抽出时间!!击掌!