C# + VisualStudio + Multiple WebDriver instances = SocketExc

本文关键字:SocketExc instances Multiple VisualStudio WebDriver | 更新日期: 2023-09-27 18:31:01

我们的 Web 应用程序的基于 WebDriver 的测试需要同时打开多个浏览器窗口:一个打开面向消费者的门户,另一个打开管理员门户(以确保在另一个窗口中看到其中一个的更改)。 这是通过创建两个单独的WebDriver对象来完成的:例如

IWebDriver driver1 = DriverFactory.NewChromeDriver();
IWebDriver driver2 = DriverFactory.NewChromeDriver();

测试可以根据需要访问这两个 Web 驱动程序。 我们的测试在 SpecFlow 中指定,并作为 MSTest 案例实现。 这些测试是在VisualStudio 2013 Premium中开发的,驱动程序只需使用本地计算机的ChromeDriver实例。 一切都很好。

当测试在 Driver2 中执行某些操作(例如,登录)然后移回 Driver1 时,我最终会抛出 SocketException,因为 localhost 主动拒绝了连接。 Windows防火墙已被告知允许所有端口上的应用程序,出厂设置了正确的代理。 精确的堆栈跟踪是:

System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 127.0.0.1:50467
Result StackTrace:  
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)

Firefox 打开一个浏览器,然后放弃。 当打开多个窗口时,IE甚至找不到浏览器。

(在我安装VS2013的最新更新之前,我从未遇到过这个问题...

有什么想法吗? 谢谢团队。

C# + VisualStudio + Multiple WebDriver instances = SocketExc

如果 DOM 中不存在元素,Selenium 确实可能会抛出 WebDriver 异常。 最后,我不得不将我的访问代码包装在一个 try-catch 中:如果 WebDriver 异常包装了一个包装 SocketException 的 WebException,而 SocketException 的 SocketErrorCode 是 ConnectionRefused,这意味着该元素确实没有出现在 DOM 中。