ruby-on-rails – 在Cappecbara和之前(:all)在rspec中

这是我的完整规范:

require 'spec_helper'

describe "Idea page", js: true do

  subject { page }

  before(:all) do
    create(:idea)
    visit root_path
    click_link "Log In"
  end

  context "Single idea" do
    before do
      page.find(:xpath, '//*[@id="accordion"]/div/div[1]/a').click
    end

    it { should have_selector('a',text:'Claim') }
    it "should have a button for reporting the idea"
    it "should have a button for opening all links"

    describe "Claiming" do
      before do
        click_link "Claim"
      end
      it {should have_selector('a', text:'Claimed')}
    end

  end
end

如果没有(:all)(即,在第一个块之前),浏览器将打开,单击Login链接,转到右侧页面,然后单击链接.尼斯.

但是在它尝试点击第二个链接(“声明”)之前它会再次完成它,这既费时又容易出错.所以我试着在之前解决这个问题:(全部).

但现在,它只是弹出Firefox,等待片刻,然后再做关闭而不做任何事情.测试失败说:

Failures:

  1) Idea page Single idea 
     Failure/Error: page.find(:xpath, '//*[@id="accordion"]/div/div[1]/a').click
     Capybara::ElementNotFound:
       Unable to find xpath "//*[@id=\"accordion\"]/div/div[1]/a"
     # ./spec/features/ideas_spec.rb:15:in `block (3 levels) in <top (required)>'

  2) Idea page Single idea Claiming 
     Failure/Error: page.find(:xpath, '//*[@id="accordion"]/div/div[1]/a').click
     Capybara::ElementNotFound:
       Unable to find xpath "//*[@id=\"accordion\"]/div/div[1]/a"
     # ./spec/features/ideas_spec.rb:15:in `block (3 levels) in <top (required)>'

显然,由于浏览器页面是空白的.

我错过了什么?谢谢.

编辑:也许有一些我不明白的基本知识.使用before(:each),这是测试尝试做的事情:

1)登录webapp,确保有“Claim”按钮.

2)再次登录webapp,再次打开手风琴,现在点击“Claim”按钮,看看会发生什么.

所以每一步的开头都是一样的,浏览器一次又一次地做同样的事情.这是怎么回事?

如果是这样,为什么我这样做会出错?具体来说,在之前(:每个)我得到这个:

Failures:

  1) Idea page Single idea Claiming 
     Failure/Error: it {should have_selector('a', text:'Claimed')}
     Selenium::WebDriver::Error::UnhandledAlertError:
       Modal dialog present
     # [remote server] file:///tmp/webdriver-profile20130116-3734-lw7267/extensions/fxdriver@googlecode.com/components/command_processor.js:10287:in `nsCommandProcessor.execute'
     # [remote server] file:///tmp/webdriver-profile20130116-3734-lw7267/extensions/fxdriver@googlecode.com/components/driver_component.js:7328:in `Dispatcher.executeAs/<'
     # [remote server] file:///tmp/webdriver-profile20130116-3734-lw7267/extensions/fxdriver@googlecode.com/components/driver_component.js:7488:in `Resource.handle'
     # [remote server] file:///tmp/webdriver-profile20130116-3734-lw7267/extensions/fxdriver@googlecode.com/components/driver_component.js:7435:in `Dispatcher.dispatch'
     # [remote server] file:///tmp/webdriver-profile20130116-3734-lw7267/extensions/fxdriver@googlecode.com/components/driver_component.js:10119:in `WebDriverServer/<.handle'
     # [remote server] file:///tmp/webdriver-profile20130116-3734-lw7267/extensions/fxdriver@googlecode.com/components/httpd.js:1935:in `unknown'
     # [remote server] file:///tmp/webdriver-profile20130116-3734-lw7267/extensions/fxdriver@googlecode.com/components/httpd.js:2261:in `ServerHandler.handleResponse'
     # [remote server] file:///tmp/webdriver-profile20130116-3734-lw7267/extensions/fxdriver@googlecode.com/components/httpd.js:1168:in `Connection.process'
     # [remote server] file:///tmp/webdriver-profile20130116-3734-lw7267/extensions/fxdriver@googlecode.com/components/httpd.js:1616:in `RequestReader._handleResponse'
     # [remote server] file:///tmp/webdriver-profile20130116-3734-lw7267/extensions/fxdriver@googlecode.com/components/httpd.js:1464:in `RequestReader._processBody'
     # [remote server] file:///tmp/webdriver-profile20130116-3734-lw7267/extensions/fxdriver@googlecode.com/components/httpd.js:1333:in `RequestReader.onInputStreamReady'
     # ./spec/features/ideas_spec.rb:26:in `block (4 levels) in <top (required)>'

即使我看到浏览器单击按钮,将其转为“声明”,并且没有显示模态对话框.

编辑2:我的立场得到了纠正!毕竟有一个模态对话框!我修复了JS以停止显示它,现在测试通过了.我仍然认为框架必须从头开始重复整个序列(似乎是浪费工作),但无论如何都是奇怪的.谢谢!

最佳答案 这是因为测试数据,包括水豚会话数据(例如登录状态)和create(:idea)创建的模型在规范之间被消除.

你想要之前(:每个)不是:所有,即使它更耗时.

点赞