Selenium WebDriver测试失败,并出现System.Net.WebException无法连接 [英] Selenium WebDriver tests fail with System.Net.WebException Unable to connect

查看:289
本文介绍了Selenium WebDriver测试失败,并出现System.Net.WebException无法连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一组通过命令行调用MSTest执行的Selenium WebDriver测试(不是Selenium RC且未使用Selenium-Server)测试.对于我们计划测试的每个浏览器(目前为Chrome,Firefox和IE9),我将它们分为测试类,每个测试类都有一个启动方法,该方法可以登录到被测系统,然后执行所需的测试.还有一个TearDown方法,该方法使用WebDriver.Quit()关闭浏览器.

I'm working on a suite of Selenium WebDriver (not Selenium RC and not using Selenium-Server) tests that are being executed by a command-line call to MSTest. I've broken them into test classes for each browser we plan on testing (Chrome, Firefox and IE9, for now) and each test class has a startup method that logs in to the system under test and then executes the test(s) requested and a TearDown method that closes the browser using WebDriver.Quit().

在我的开发机器上,所有内容都是笨拙的,但是在我们的构建机器上,类初始化/启动方法可以很好地执行并登录到站点,但是,一旦第一个实际测试开始,测试运行程序就会因错误而关闭讯息:

On my development machine everything is hunky-dory, but on our build machine the class initialization/start-up method executes fine and logs into the site, but as soon as the first actual test starts the test runner closes with the error 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:59982

显然,端口会根据要测试的浏览器而变化;当我运行整个套件时,针对IE,Firefox和Chrome的测试在不同的端口上会出现错误-每个浏览器都会出现两个错误(因为该套件目前每个浏览器都有两个测试)

obviously the port changes depending on which browser's being tested; when I run the whole suite I get errors on different ports for the tests for IE, Firefox, and Chrome - two errors for each browser (as the suite currently has two tests for each browser)

构建机器的Windows防火墙已关闭,并且在运行测试之前检查netstat表示该端口未使用;如果我调试测试并在启动代码运行之后但在测试运行之前停止,则该端口当然正在使用中. 127.0.0.1和localhost也相同.

Windows Firewall is off for the build machine, and checking netstat before running the tests shows that the port's not in use; if I debug the tests and stop after the startup code runs but before the test runs, of course the port is in use. 127.0.0.1 and localhost are the same as well.

无论是否通过命令行调用测试,使用VS Test工具在Visual Studio中运行/调试它,或者执行控制台应用程序(通过Process.Start()调用MSTest),我都会遇到相同的错误.第一次调用FindElement()时会引发异常.

I get the same error whether or not I call the test via the command line, run/debug it from within Visual Studio using the VS Test tools, or by executing my console app, which calls MSTest via Process.Start(). The exception is thrown on the first FindElement() call.

我可以从哪里开始找出为什么这在构建机器上而不是在我的开发箱上失败了?

Where can I start looking to figure out why this is failing on the build machine and not on my dev box?

推荐答案

这里的问题是,在尝试在启动过程中实现一些更强大的错误处理时,我在try/catch的finally块中放置了对Teardown代码的调用包裹在登录代码中,所以一旦完成测试工具的登录,我就关闭并清理WebDriver实例,因此在运行测试时就没有要与之通信的WebDriver.

The issue here was that in trying to implement some more robust error handling during startup I place a call to the Teardown code in the finally block of a try/catch wrapped around the login code, so as soon as the test harness was done logging in I was closing and cleanin up the WebDriver instance, so when the test ran there wasn't a WebDriver there to communicate with.

我怀疑当我在开发机器上查看相同"代码时,实际上正在运行的库中没有该块.

I suspect when I as looking at the "same" code on my dev machine the libraries that were actually running didn't have that block in them.

这篇关于Selenium WebDriver测试失败,并出现System.Net.WebException无法连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆