- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有几个关于 capybara 的问题。我不妨在这里问一下,因为 github page for Capybara 中的 RDOC非常适合设置和运行它。但是 API 或可用方法列表在哪里?
首先。每个 *_spec.rb 文件,scenario
应该只存在一次吗?或者在一个文件中包含多个场景
是否合适?
例如,在spec/request/user_spec.rb
中:
require 'spec_helper'
feature 'User actions' do
background do
data = {
:first_name => 'foo',
:last_name => 'bar',
...
}
user = User.new(data, :as => :user)
user.save
end
scenario 'User can browse home page' do
visit root_path
page.should have_content('Homepage')
end
scenario 'User should not be able to visit the dashboard' do
visit dashboard_root_path
page.should have_content('You are not authorized to access this page.')
end
end
如果上面的代码结构有什么问题,或者是否有改进的余地。我是开放的反馈。
其次。我注意到上面的代码。如果我在 spec/spec_helper.rb
中有 config.use_transactional_fixtures = false
,它会为用户节省两次。这意味着,在我的测试数据库/用户表中,我将有 2 个名为“foo bar”的用户。这正常吗?
第三。 我有一个带有 HTML 按钮的表单。当用户点击这个按钮时,jQuery 提交表单。我将如何用 Capybara 测试它?我不认为 click_button "Add"
会成功。
第四。我如何在 Capybara 中登录用户?我正在使用设计。 sign_in User.first
会成功吗?我可以在 Capybara 中访问 current_user
吗?
最后,如果有人知道任何关于 Rspec + Capybara 的“入门”指南/教程。请务必提及。
最佳答案
自从我决定不再喜欢 Cucumber 后,我也转而编写请求规范。
ONE) 有多个场景确实很好。您可以使用 rspec 的所有其他强大功能,因此我建议您也使用底部代码中的上下文。
二)这可能可以通过使用 Rspec Set Gem 来解决。和数据库清洁 gem 。还有:The Original Rationale for Set
警告:确保在使用 set 时正确设置 DatabaseCleaner。我自己的设置(可能有点矫枉过正,但对我有用):
config.before(:suite) do
DatabaseCleaner.clean_with :truncation
end
config.before(:all) do
DatabaseCleaner.clean_with :truncation
end
config.after(:all) do
DatabaseCleaner.clean_with :truncation
end
config.after(:suite) do
DatabaseCleaner.clean_with :truncation
end
三)是的! click_button“添加”应该可以工作! The complete capybara API很有用,但花了我一段时间才理解。最重要的是 Action 和 rspec 匹配器。
例子:
click_button "Add"
page.should have_content("Successfully Added")
您可以使用元素查找器缩小范围。
第四)设计提供帮助。有一个登录助手。阅读 dox :)。这是一个演示:
feature 'User actions' do
background do
data = {
:first_name => 'foo',
:last_name => 'bar',
...
}
@user = User.new(data, :as => :user)
@user.save
end
context "no user is signed in" do
scenario 'User can browse home page' do
visit root_path
page.should have_content('Homepage')
end
scenario 'User should not be able to visit the dashboard' do
visit dashboard_root_path
page.should have_content('You are not authorized to access this page.')
end
end
context "user is signed in" do
before :each do
sign_in @user
end
[more scenarios]
end
end
当然,最终您可能希望将其拆分为更具体的功能。可能有一个“公共(public)导航”功能用于所有关于客人看到内容的测试,然后一个单独的功能用于用户登录等。
关于ruby-on-rails-3 - 关于 capybara 的几个问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6404632/
我有一个文件字段,其中包含 opacity: 0并且正在重叠一个假按钮。它是一种常见的 css 技术,可以伪造一种在不同浏览器中一致显示的“上传按钮”。 Capybara 不允许我打电话 attach
我正在尝试为我的 ROR 应用程序编写一些 capybara cucumber 脚本,但发现很难通过每次都进行更改并运行完整功能来使脚本正确。 是否有适用于 Capybara 的 REPL(Read
像这样的html 我的代码是 attach_file("ok","./fileset/publisher/upload_pic.jpg") 但我失败了: 失败: Capybara::Element
我想在我的代码中设置不同的 Capybara 等待时间,具体取决于它们通常需要多长时间才能完全加载?我是否必须重复更改 Capybara.default_wait_time 还是有更好的方法? 最佳答
在 Capybara 中,是否可以在新窗口而不是当前窗口中打开链接? 最佳答案 如果此元素是链接,您只需获取其 href 并在新窗口中打开它: url = find('.some_link')[:hr
对于编辑记录的集成测试,我尝试用新文本替换表单输入字段中的现有文本: find("input[@id='course_title']").set("a safer workplace") 但是,每次我
我有一个动态生成的表单,如下所示: Do you like Pizza? [ ] Yes [ ] No HTML 看起来像这样: Do you like Pizza?
我正在尝试填写两个标有 id="admin_passsword"的文本框。我可以轻松访问第一个文本框,但由于除了占位符和上面的标签之外没有任何区别,我不知道如何访问第二个文本框来填写该字段。
我是一个新手,尝试使用 Capybara 测试我的 Rails 项目,但是当我尝试从我的 div 中检测一些字符串时,我对 page 和 page.body 的含义感到困惑:(in :js=>true
我在使用 rspec、capybara、capybara-webkit 和 timecop 的某些集成规范中看到错误。 Capybara::FrozenInTime: time appears
我使用以下 html 堆栈创建了自定义上传表单: 文件字段通过 display: none 的 css 隐藏属性(property)。因此,当她单击标签(自定义样式)时,用户会调用文件
假设我在 Mac OS X 上安装了 PhantomJS,我可以编写一个普通的旧 Ruby 脚本(没有 Cucumber,没有 RSpec)来驱动 Poltergeist 吗?换句话说,我想要一些 g
我希望单击我的测试应用程序上出现的弹出消息(如果存在)。我对 capybara 很陌生,似乎找不到办法做到这一点。我以前有过使用 watir 的经验,如果我使用 watir 的话,会是这样的: if
我目前使用 Watir-webdriver 进行所有前端测试,但开发团队使用 Capybara 在 Jenkins CI 上运行测试。我们都使用相同的 Cucumber 功能。 值得我们进行两次有效的
Capybara的API似乎不支持配置HTTP代理。有没有什么方法可以和它一起使用? 上下文:我使用 capybara 和 cucumber 来测试 Rails 应用程序,并使用 akephalos
我很好奇是否有更好的方法来测试页面中是否存在完整的 URL(包括协议(protocol))。 使用 Capybara 2.10.2 和 Rails 5.0.0.1,我有以下设置: rails_help
我有以下测试: it 'shows the current quantity of items inside cart' do item = create(:item) visit
我正在使用 Capybara 自动截取一些屏幕截图。 我需要 chrome 浏览器最大化运行,但我似乎无法让它工作。 基于 https://sites.google.com/a/chromium.or
下面是两个单选按钮的 html 代码,它们只是“值”属性不同 我想做的是选择“value =2”的单选按钮 我尝试使用“选择(“AmountOption”)”选择第一个单选按钮,但我想选择第二个单
我尝试使用几种变体来检查 Capybara 的 200 Ok HTTP 响应,但没有一个变体不起作用: response.should be_success page.status.should be
我是一名优秀的程序员,十分优秀!