第一次测试完成后,NUnit Setup似乎没有调用WebDriver

时间:2015-02-03 18:22:55

标签: 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().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);
}

我已经尝试过在最后一个测试中添加一个driver.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]之后重新启动问题相关联,但我似乎无法弄清楚如何解决这个问题。

1 个答案:

答案 0 :(得分:0)

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

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

而且,最佳做法可能是使用Quit代替Dispose,因为Quit()会调用Dispose并进行更多清理,因此您不必使用<{1}}分开。

并且,随着更改,Dispose方法应如下所示:

LaunchBrowser()