简体   繁体   中英

Connection refused error when I try to close a Watir browser?

This thing just seems to give me problem after problem.

I posted another question earlier, trying to solve the problem of retaining my session state between closing and opening through Watir. Firefox achieves this on its own, so I figured if I just set the preferences correctly, it'd save my state. I ended up having to go into the selenium-webdriver source and make some changes in order to achieve this in reality.

So, I was just testing my application. Part of its behavior is to loop through a bunch of pages and extract text from them. While it's looping, I simply have it in a while true loop, and figured "hey, I can just stop the program with Ctrl+C". Well, this worked fine up to now, until it came to saving the states.

Ctrl+C causes it not to save its state. My guess as to why is that you need to actually close the browser (and I actually recreated the bug in IRB, so I'm pretty sure this is the case). Simple, right? Why not just use an ensure block with @browser.close in it? That was my first thought.

So, when I try it this way, it does hit the ensure block, and the ensure block calls a method called kill . Kill calls @browser.close if @browser.exists? . The problem is that when it tries to execute this line, I get a nice long list of errors leading up to selenium-webdriver . It seems as if it's trying to make an HTTP request as part of its close functionality, and is failing because, perhaps, Ctrl+C exited the application.

The stack trace is located at https://gist.github.com/Inkybro/5557085

The very last thing I thought was that maybe I needed to let any calls to the @browser object complete, so I placed a bunch of trap('INT', 'IGNORE') and trap('INT', 'DEFAULT') lines around these pieces of code. This also doesn't seem to do the trick.

I'm not really sure as to why, which is why I'm posting here. What I think needs to happen is that whatever processing is going on at the time of Ctrl+C needs to finish processing before @browser.close can be called. If anybody has experience with Watir and/or Selenium, or even if you don't, perhaps you could help me out?

Are you supposed to be testing the browser itself, or your product? Because really, if you are doing things like above, (logging in, closing and re-opening the browser to see if you are still logged in) it sounds to me like you are basically testing the browser's ability to store and read cookies and properly provide them when making requests of a page.

What about if you presume for a moment that the browser in fact does what it is supposed to do with regard to cookie management and usage? If you do that, then what becomes important? I think it would be that your app tells the browser to create the proper cookie, with the proper contents.

So maybe rather than actually trying to test that firefox will in fact use it's cookies correctly (or any other browser doing that for that matter) why not simply test that when the user has logged in, that the proper cookies have been created? You may also have to test that the cookie is updated periodically so that a user's session does not expire while they are actively using the site. again pretty easy to test by just looking at the cookies

You might also want to test that your server is sensitive to changes in the cookie, or a missing cookie. That the server is looking at the cookie and is not depending on session variables is also pretty easy to test, login, alter or clear the cookie, try to access a page and see if it fails.

So with that you get three things 1) Proper cookies created upon login. 2) Cookies kept updated as various pages are accessed. 3) Missing or altered cookie == no soup for your user. (#4 site works properly if cookies present is implied by the three above and all the rest of your tests that exercise your site/app)

Stop trying to do the work of the mozilla test team, and refocus on testing your application.

I'm still not sure what you try to achieve, but this hacky piece of code ensures browser is closed on Ctrl+C and no your exception is raised:

require 'watir-webdriver'

begin
  browser = Watir::Browser.new
  loop do
    # some code
  end
rescue SystemExit, Interrupt
  puts 'Exiting!'
ensure
  begin
    browser.close
  rescue Errno::ECONNREFUSED
    # do nothing
  end
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM