问题描述
更新:
我正在运行下面的代码来执行我的示例测试用例:(Windows 7、Watir 3.0.0、Watir-webdriver-0.6.1)
I'm running the code below to execute my sample test cases: (Windows 7, Watir 3.0.0, Watir-webdriver-0.6.1)
require "watir-webdriver"
require 'test/unit'
class Teste1
$var = Watir::Browser.new :chrome
def met1
$var.goto 'google.com'
$var.text_field(:name, "q").set 'sample'
$var.button(:name =>'btnG').click
end
end
class Teste2 < Test::Unit::TestCase
$test = Teste1.new
def test_gomet1
$test.met1()
end
end
浏览器打开但脚本抛出以下错误:
The browser opens but the script throws the following error:
test_gomet1(Teste2):
Errno::ECONNREFUSED: No connection could be made because the target machine actively refused it. - connect(2)
C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.25.0/lib/selenium/webdriver/remote/http/default.rb:82:in `response_for'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.25.0/lib/selenium/webdriver/remote/http/default.rb:38:in `request'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.25.0/lib/selenium/webdriver/remote/http/common.rb:40:in `call'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.25.0/lib/selenium/webdriver/remote/bridge.rb:598:in `raw_execute'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.25.0/lib/selenium/webdriver/remote/bridge.rb:576:in `execute'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.25.0/lib/selenium/webdriver/remote/bridge.rb:99:in `get'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/selenium-webdriver-2.25.0/lib/selenium/webdriver/common/navigation.rb:14:in `to'
C:/Ruby193/lib/ruby/gems/1.9.1/gems/watir-webdriver-0.6.1/lib/watir-webdriver/browser.rb:63:in `goto'
maisum.rb:11:in `met1'
maisum.rb:21:in `test_gomet1'
有人可以帮我吗?
推荐答案
我无法为 test_closeVar
测试获得该异常,但是我可以为您的其他两个测试重现它.当您在浏览器关闭后尝试与其交互时,您会收到该异常.
I cannot get that exception for the test_closeVar
test, however I can reproduce it for your other two tests. You get that exception when you try to interact with the browser after it has been closed.
使用 Test::Unit 时,请记住测试按字母顺序运行.因此,您的 test_closeVar
测试将在 test_gomet1
和 test_gomet2
运行之前关闭浏览器.test_gomet1
和 test_gomet2
将抛出该异常,因为浏览器已关闭.
When using Test::Unit, keep in mind that the tests run in alphabetically order. Therefore your test_closeVar
test will close the browser before test_gomet1
and test_gomet2
even run. test_gomet1
and test_gomet2
will throw that exception because the browser is closed.
作为一个快速解决方案,您可以在测试名称中添加数字,以使它们按特定顺序运行.
As a quick fix, you could add numbers to the test names to get them to run in a specific order.
不过,长期修复实际上是让您的测试独立,因此顺序无关紧要.
The long term fix though is really to make your tests independent so that order does not matter.
更新
一些观察:
- 问题是使用 Selenium-Webdriver 发生的,因此不是 Watir-Webdriver 特定的问题.
- 该脚本在 Firefox 中运行良好,因此可能是 chromedriver 特定问题.
解决方法:似乎 chrome 浏览器不喜欢在测试用例之外声明.我不明白为什么,但快速解决方法是在测试用例的设置中声明浏览器.如果你想为每个测试使用相同的浏览器,如果它不存在,你可以声明它.
Workaround: It seems like the chrome browser does not like being declared outside the test case. I do not understand why, but the quick fix is to declare the browser in the setup of the test case. If you want to use the same browser for each test, you can just declare it if it does not already exist.
以下将运行(尽管我建议清理它以减少全局变量的使用):
The following will run (though I would suggest cleaning it up to reduce the usage of global variables):
class Teste1
def met1
$var.goto 'google.com'
$var.text_field(:name, "q").set 'sample'
$var.button(:name =>'btnG').click
end
end
class Teste2 < Test::Unit::TestCase
def setup()
unless defined?($var)
$var = Watir::Browser.new :chrome
end
$test = Teste1.new()
end
def test_gomet1
$test.met1()
end
end
这篇关于Watir webdriver Errno::ECONNREFUSED:无法建立连接,因为目标机器主动拒绝它.- 连接(2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!