Watir webdriver Errno::ECONNREFUSED: No connection could be made because the target machine actively refused it. - connect(2)

♀尐吖头ヾ 提交于 2019-12-10 18:26:51

问题


Updated:

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'

Can anyone help me on that?


回答1:


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.

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.

Update

A couple of observations:

  • The problem occurs using Selenium-Webdriver, so not a Watir-Webdriver specific issue.
  • The script runs fine in Firefox, so it might be a chromedriver specific issue.

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


来源:https://stackoverflow.com/questions/11584896/watir-webdriver-errnoeconnrefused-no-connection-could-be-made-because-the-tar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!