这里是我的完整规格:水豚之前(:所有)在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)
(即,当它只是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)>'
很明显,因为浏览器页面是空白的。
我错过了什么?谢谢。
编辑:也许有一些基本的东西我不明白。用before(:each)
这里是测试试图做的事情:
1)登录到web应用程序,确保有'Claim'按钮。
2)登录到web应用程序再次,打开手风琴再次,现在单击“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/[email protected]/components/command_processor.js:10287:in `nsCommandProcessor.execute'
# [remote server] file:///tmp/webdriver-profile20130116-3734-lw7267/extensions/[email protected]/components/driver_component.js:7328:in `Dispatcher.executeAs/<'
# [remote server] file:///tmp/webdriver-profile20130116-3734-lw7267/extensions/[email protected]/components/driver_component.js:7488:in `Resource.handle'
# [remote server] file:///tmp/webdriver-profile20130116-3734-lw7267/extensions/[email protected]/components/driver_component.js:7435:in `Dispatcher.dispatch'
# [remote server] file:///tmp/webdriver-profile20130116-3734-lw7267/extensions/[email protected]/components/driver_component.js:10119:in `WebDriverServer/<.handle'
# [remote server] file:///tmp/webdriver-profile20130116-3734-lw7267/extensions/[email protected]/components/httpd.js:1935:in `unknown'
# [remote server] file:///tmp/webdriver-profile20130116-3734-lw7267/extensions/[email protected]/components/httpd.js:2261:in `ServerHandler.handleResponse'
# [remote server] file:///tmp/webdriver-profile20130116-3734-lw7267/extensions/[email protected]/components/httpd.js:1168:in `Connection.process'
# [remote server] file:///tmp/webdriver-profile20130116-3734-lw7267/extensions/[email protected]/components/httpd.js:1616:in `RequestReader._handleResponse'
# [remote server] file:///tmp/webdriver-profile20130116-3734-lw7267/extensions/[email protected]/components/httpd.js:1464:in `RequestReader._processBody'
# [remote server] file:///tmp/webdriver-profile20130116-3734-lw7267/extensions/[email protected]/components/httpd.js:1333:in `RequestReader.onInputStreamReady'
# ./spec/features/ideas_spec.rb:26:in `block (4 levels) in <top (required)>'
即使我看到浏览器点击按钮,将其以“爆料”,并没有显示出有模态对话框。
编辑2:我站好了!那里是那里的模态对话!我修正了JS停止显示它,现在测试通过了。我仍然认为框架必须从头开始重复整个序列(看起来像浪费工作),但无论如何,这很奇怪。谢谢!
如果您使用[shared_context](https://www.relishapp.com/rspec/rspec-core/docs/example-groups/shared-context),则可以将登录过程放在该处,并使其更清晰你在做什么'include_context“登录后''...它减少了打字! – iain
@iain再次来救援! :) 谢谢!肯定会检查出来。 – ezuk