这是我的完整规格:
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
Without (:all)
(即,当它只是before
在第一个块中),浏览器打开,单击“登录”链接,转到右侧页面,然后单击该链接。好的。
但在尝试单击第二个链接(“索赔”)之前,它会再次执行此操作,这既耗时又容易出错。所以我尝试用以下方法解决这个问题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)>'
显然,因为浏览器页面是空白的。
我缺少什么?谢谢。
Edit:也许有一些基本的东西我不明白。和before(:each)
这是测试尝试执行的操作:
1) 登录网络应用程序,确保有“声明”按钮。
2)登录网络应用程序again, 打开手风琴again,然后单击“声明”按钮看看会发生什么。
所以每一步的开始都是完全相同的,浏览器一次又一次地做同样的事情。事情就应该这样吗?
如果是这样,为什么我这样做时会出现错误?具体来说,与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/[email protected] /cdn-cgi/l/email-protection/components/command_processor.js:10287:in `nsCommandProcessor.execute'
# [remote server] file:///tmp/webdriver-profile20130116-3734-lw7267/extensions/[email protected] /cdn-cgi/l/email-protection/components/driver_component.js:7328:in `Dispatcher.executeAs/<'
# [remote server] file:///tmp/webdriver-profile20130116-3734-lw7267/extensions/[email protected] /cdn-cgi/l/email-protection/components/driver_component.js:7488:in `Resource.handle'
# [remote server] file:///tmp/webdriver-profile20130116-3734-lw7267/extensions/[email protected] /cdn-cgi/l/email-protection/components/driver_component.js:7435:in `Dispatcher.dispatch'
# [remote server] file:///tmp/webdriver-profile20130116-3734-lw7267/extensions/[email protected] /cdn-cgi/l/email-protection/components/driver_component.js:10119:in `WebDriverServer/<.handle'
# [remote server] file:///tmp/webdriver-profile20130116-3734-lw7267/extensions/[email protected] /cdn-cgi/l/email-protection/components/httpd.js:1935:in `unknown'
# [remote server] file:///tmp/webdriver-profile20130116-3734-lw7267/extensions/[email protected] /cdn-cgi/l/email-protection/components/httpd.js:2261:in `ServerHandler.handleResponse'
# [remote server] file:///tmp/webdriver-profile20130116-3734-lw7267/extensions/[email protected] /cdn-cgi/l/email-protection/components/httpd.js:1168:in `Connection.process'
# [remote server] file:///tmp/webdriver-profile20130116-3734-lw7267/extensions/[email protected] /cdn-cgi/l/email-protection/components/httpd.js:1616:in `RequestReader._handleResponse'
# [remote server] file:///tmp/webdriver-profile20130116-3734-lw7267/extensions/[email protected] /cdn-cgi/l/email-protection/components/httpd.js:1464:in `RequestReader._processBody'
# [remote server] file:///tmp/webdriver-profile20130116-3734-lw7267/extensions/[email protected] /cdn-cgi/l/email-protection/components/httpd.js:1333:in `RequestReader.onInputStreamReady'
# ./spec/features/ideas_spec.rb:26:in `block (4 levels) in <top (required)>'
即使我看到浏览器单击该按钮,将其切换为“已声明”,并且没有显示模式对话框。
Edit 2:我纠正了!那里was毕竟是一个模式对话框!我修复了 JS 以停止显示它,测试现在通过了。我仍然认为框架必须从头开始每一步重复整个序列(似乎是浪费工作),但无论如何。谢谢你!