ruby - 面对这个问题@driver.quit 在之后给出错误(:context ) in rspec

标签 ruby selenium testing rspec automation

我正在尝试这个

require 'rspec'
require 'selenium-webdriver'

RSpec.describe 'New test' do

  before :each do
    @driver = Selenium::WebDriver.for (:firefox)
  end

  after :all do
    @driver.quit
  end

  it 'should signup and create a new user' do
    p 'testing'
  end
end

这是我得到的错误

rspec test.rb                                                                                                                     [13:04:18]
"testing"
.
An error occurred in an `after(:context)` hook.
Failure/Error: @driver.quit

NoMethodError:
  undefined method `quit' for nil:NilClass
# ./test.rb:11:in `block (2 levels) in <top (required)>'


Finished in 4.61 seconds (files took 0.17828 seconds to load)
1 example, 0 failures

我确信这是我丢失的一件小东西,但找不到它,我们将不胜感激。

这是 FF 还是 selenium 版本问题?

我正在使用 FF 45 和 selenium 2.53 以及 rspec 3.5.4。

最佳答案

这是一个 RSpec 问题。 before/after(:all) Hook 只能访问在其范围或外部范围级别定义的实例变量,但不能访问在当前运行的规范中声明的实例变量。 after(:each) 钩子(Hook)确实可以访问当前规范中声明的实例变量。

before :all do
 @a = 2
end

context "blah" do
  before :all do
    @b = 3
  end

  it "blah blah" do
    @c = 4
    # @a,@b,@c all accessible here
  end

  after :each do
   #@a, @b, @c accessible here
  end

  after :all do
    # @a,@b  accessible here
  end
end

after :all do
   #can access @a
end

关于ruby - 面对这个问题@driver.quit 在之后给出错误(:context ) in rspec,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42828055/

相关文章:

android - 如何在android中测试执行时间的功能?

ruby-on-rails - 使用 Knock 的 Rails API 的 Auth0

ruby-on-rails - 具有不同名称的 Rails 委托(delegate)

python - 使用 send_keys Internet Explorer 时出现先前的评估未完成错误

selenium - 如何使用Selenium WebDriver(Eclipse)单击下拉列表(不是由select创建的)?

visual-studio-2012 - 未使用 Spec Explorer 2010 完成测试套件

javascript - 我可以在 chrome 控制台中使用 javascript 测试动态 html View 吗?

ruby-on-rails - Rails - 使用嵌套属性为 has_many 创建 seeds.rb

ruby-on-rails - FactoryGirl 在数据库中找不到实例

java - 马文能做到这一点吗?运行selenium测试,生成报告,然后执行jar文件