Selenium WebDriver in Ruby: Preventing the test from closing the browser window at end

亡梦爱人 提交于 2019-12-14 03:03:59

问题


I appear to be having the exact opposite problem many other people have - in that my Selenium tests in Ruby will close the browser window at test end, no matter what the end result is. Pass or fail, it will always close the browser. I would like to stop this.

Context:

Previously I coded tests in Java using IntelliJ IDEA. Browser windows for Selenium tests in this case would NOT close at all period unless you used driver.quit(). This is actually quite useful as it means that the browser window would stay open if the test failed - which meant I could look at where it stopped in the browser and help figure out why it failed. This was also useful for test writing as it meant that I could essentially pick up where I left off to write the next block instead of having to keep a parallel tab going in another browser by hand to get the next set of selectors in the given screen.

I've found in Ruby using RubyMine that the browser will close when the test ends in any capacity. This is a bit of a problem. While technically I could take a screencap on failure, it'd mean that I'd have a harder time retracing why it failed (back button on browser, typing in fields to work out if a quirk in our UI caused it, etc). And of course, screencaps take up hard drive space. ;)

I've tried the detach=true (and True) command switch for Chrome and that has not worked.

Setup:

  • IDE: RubyMine
  • Gems: selenium-webdriver
  • Browser: Chrome, using ChromeDriver. (In Ruby this just involves using WebDriver)
  • OS: OSX

Not headless, using no other frameworks/testing environments. It is quite literally a few lines of setup and then hitting the run button in RubyMine in a bog-standard .rb.

Summary:

I haven't been able to find any existing questions here or in other places online for Ruby specifically for keeping a Selenium test in RubyMine from closing the window on test end. "Test end" in this case refers to success (reaching the end of the .rb) or failure (Tracebacks, in this case). I would prefer that the window would stay open until it'd hit a driver.quit line. Is there any way I can set this up?

Thank you very much. I hope this isn't redundant. I also hope this will help other testers in the future :)


回答1:


You can use the :desired_capabilities to set this flag:

caps = Selenium::WebDriver::Remote::Capabilities.chrome("goog:chromeOptions" => {detach: true})
driver = Selenium::WebDriver.for :chrome, desired_capabilities: caps

Note that old examples will be using "chromeOptions", but for newer Chrome versions, it will need to be "goog:chromeOptions".




回答2:


@JustinKo has posted an answer here

Chrome detach option is no longer working

You can pass Chrome Options using the following format:

    browser = Watir::Browser.new(
      :chrome,
      'goog:chromeOptions' => {detach: true}
    )


来源:https://stackoverflow.com/questions/58920198/selenium-webdriver-in-ruby-preventing-the-test-from-closing-the-browser-window

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