测试后 Selenium-Webdriver/RSpec 关闭窗口 [英] Selenium-Webdriver/RSpec closing window after tests

查看:43
本文介绍了测试后 Selenium-Webdriver/RSpec 关闭窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行 RSpec 和 Selenium-Webdriver 的问题.我正在滚动我自己的框架,并在每次测试运行后遇到问题.我的 spec_helper.rb 设置如下所示:

Running an issue running RSpec and Selenium-Webdriver. Im rolling my own framework and am running into an issue after each test gets ran. My spec_helper.rb setup looks like so:

require 'selenium-webdriver'

Dir['./spec/support/**/*.rb'].each { |file| require file }

RSpec.configure do |config|

  config.before(:each) do
    # Default browser is chrome, otherwise look for ENV variables
    case ENV['browser'] ||= 'chrome'
    when 'chrome'
      @driver = Selenium::WebDriver.for :chrome
    when 'firefox'
      @driver = Selenium::WebDriver.for :firefox
    end

    # Clear cookies between each example
    @driver.manage.delete_all_cookies   

    # Set up implicit waits
    @driver.manage.timeouts.implicit_wait = 5

    # Default base_url is set to website, otherwise look for ENV variables
    case ENV['base_url'] ||= 'https:www.website.com' #redacted real website
    when 'local'
      ENV['base_url'] = 'local_url_here'
    when 'development'
      ENV['base_url'] = 'https:www.website.com' #redacted real website
    when 'production'
      ENV['base_url'] = 'prod_url_here'
    end


    # Close browser window after each test
    config.after(:each) do 
      @driver.close
    end

  end
end

我的实际 rspec 测试的设置格式如下:

My actual rspec tests are setup in the format of:

Rspec.describe 'something' do
  context 'some context' do
  #multiple it 'stuff' do's
  end
 end
end

这是非常典型的.然而,第一个测试将运行良好,在第一次测试后,每个测试运行良好,但浏览器(在本例中为 Chromedriver)在每次测试后关闭并给出错误:Selenium::WebDriver::Error::WebDriverError: no such会话.

Which is pretty typical. However the first test will run fine, after the first test each test runs fine but the browser (Chromedriver in this case) closes after each test and gives the error: Selenium::WebDriver::Error::WebDriverError: no such session.

所以我尝试了:

config.after(:all) do 
  @driver.quit
end

相反.这会成功运行所有测试,但在测试结束时我也遇到了 n 个错误(其中 n = 总测试数)undefined methodquit' for nil:NilClass`.它还为每个测试打开一个新的浏览器实例(我不想这样做).

Instead. This runs the tests all successfully, but I also get n errors at the end of the test (where n = number of total tests) undefined methodquit' for nil:NilClass`. It also opens a new browser instance for each test (Which I don't want to do).

RSpec 似乎关闭了驱动程序,即使没有 @driver.quit,我也无法分辨.所以我真的很困惑在这里做什么.我不希望新浏览器打开每个实例,但我希望浏览器在每次测试后关闭并打开一个新浏览器(或者这可能是个坏主意?,我正在删除 cookie,所以如果它只是切换到一个新的 URL,因为我正在为每个测试做 visit 会起作用吗?)

RSpec seems to close the driver down from what I can tell even without @driver.quit. So im really confused what to do here. I don't want a new browser opening every single instance, but I would like the browser to close after each test and open a new one (Or maybe this is a bad idea?, I am deleting cookies, so if it would just switch to a new URL since I am doing a visit for each test would work?)

处理这个问题的最佳方法是什么?

What's the best way to handle this?

推荐答案

是的,当你使用 chrome 驱动程序时,它会在最后自动关闭浏览器.

Yes, that happens when you use chrome driver, it automatically closes the browser at the end.

解决办法是,为驱动对象编写如下代码

The solution is to, write the following code for driver object

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

这将阻止 chrome 浏览器最终关闭.

This will stop the chrome browser getting closed at the end.

我建议你使用 WATIR,它是 Ruby selenium 绑定的包装器.

And I suggest you to use WATIR which is the wrapper around Ruby selenium binding.

这篇关于测试后 Selenium-Webdriver/RSpec 关闭窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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