我的完整规格:
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)
(即,当第一个块中只有before
时),浏览器打开,单击Login链接,转到右侧页面,然后单击链接。好了。
但是在尝试点击第二个链接("Claim")之前,它又重复了一遍,这既耗时又容易出错。所以我试着用before(:all)
来解决这个问题。
但是现在,它只是打开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)登录web应用程序,确保有一个"索赔"按钮。
2)再次登录webapp ,再次打开手风琴,现在点击"Claim"按钮,看看会发生什么。所以每一步的开始都是完全相同的,浏览器一次又一次地做同样的事情。本来就应该是这样吗?
如果是,为什么当我这样做时得到错误?具体来说,对于before(:each)
,我得到这个:
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)>'
即使我看到浏览器点击按钮,将其改为"Claimed",并且没有显示模态对话框。
编辑2:我更正!毕竟是一个模态对话框!我修复了JS停止显示它,测试现在通过了。我仍然认为框架必须从头开始重复整个序列的每一步是很奇怪的(似乎是浪费工作),但不管怎样。谢谢你!
这是因为测试数据,包括capybara会话数据(如登录状态)和create(:idea)
创建的模型在规格之间被清除。
你想用before(:each)
而不是:all
,即使它更耗时。