我在网上尝试了几个例子,但没有运气.我期待确认删除链接的确认消息.最后一次尝试是下面的代码,但是导致了Capybara :: NotSupportedByDriverError错误.
def confirm_dialog
page.evaluate_script('window.confirm = function() { return true; }')
end
Run Code Online (Sandbox Code Playgroud) 我正在写一个黄瓜测试,我想在一个元素中获取HTML.
例如:
within 'table' do
# this works
find('//tr[2]//td[7]').text.should == "these are the comments"
# I want something like this (there is no "html" method)
find('//tr[2]//td[7]').html.should == "these are the <b>comments</b>"
end
Run Code Online (Sandbox Code Playgroud)
有人知道怎么做吗?
对于HTML
<select id="date">
<option value="20120904">Tue 4 Sep 2012</option>
<option value="20120905">Wed 5 Sep 2012</option>
<option value="20120906">Thu 6 Sep 2012</option>
</select>
Run Code Online (Sandbox Code Playgroud)
我有以下Capybara Ruby代码:
select "20120905", :from => "date"
Run Code Online (Sandbox Code Playgroud)
但是这个错误有:
cannot select option, no option with text '20120905' in select box 'date' (Capybara::ElementNotFound)
Run Code Online (Sandbox Code Playgroud)
但是,如果我这样做
select "Wed 5 Sep 2012", :from => "date"
Run Code Online (Sandbox Code Playgroud)
没关系.
是否可以通过Value not Text选择Capybara中的选项?
谢谢
我需要current_user在Rspec/capybara请求规范中存根方法的响应.该方法在ApplicationControllerhelper_method中定义并使用helper_method.该方法应该只返回一个用户ID.在测试中,我希望这种方法每次都返回相同的用户ID.
或者,我可以通过设置session[:user_id]规范(这是什么current_user返回)来解决我的问题...但这似乎也不起作用.
这些都可能吗?
编辑:
这是我得到的(它不工作.它只运行正常的current_user方法).
require 'spec_helper'
describe "Login" do
before(:each) do
ApplicationController.stub(:current_user).and_return(User.first)
end
it "logs in" do
visit '/'
page.should have_content("Hey there user!")
end
end
Run Code Online (Sandbox Code Playgroud)
也不工作:
require 'spec_helper'
describe "Login" do
before(:each) do
@mock_controller = mock("ApplicationController")
@mock_controller.stub(:current_user).and_return(User.first)
end
it "logs in" do
visit '/'
page.should have_content("Hey there user!")
end
end
Run Code Online (Sandbox Code Playgroud) 我必须为网站编写测试.我试图获取下拉框的选定值.到目前为止,我可以通过这样做得到下拉列表的内容
find_field('restrictions__rating_movies').text
Run Code Online (Sandbox Code Playgroud)
返回 - 不允许电影G PG M R13 R15 R16 R18 R RP16允许所有电影
我可以获得所选对象的值.
find_field('restrictions__rating_movies').value
Run Code Online (Sandbox Code Playgroud)
返回 - 1000
这对我没什么帮助,因为我试图从下拉框中获取所选项目的文本.
<select class="" id="restrictions__rating_movies" name="restrictions[][rating_movies]">
<option value="0">Don't Allow Movies</option>
<option value="100">G</option>
<option value="200">PG</option>
<option value="300">M</option>
<option value="325">R13</option>
<option value="350">R15</option>
<option value="375">R16</option>
<option value="400">R18</option>
<option value="500">R</option>
<option value="600">RP16</option>
<option value="1000" selected="selected">Allow All Movies</option></select>
Run Code Online (Sandbox Code Playgroud)
在这种情况下显示我需要获得值'允许所有电影'我已经尝试了上述两个示例的许多不同组合.
我有以下帮助方法将字符串输入到输入字段并按Enter键,但似乎从未按下回车键.我看到字符串输入到输入字段中,但是在命中输入时发生的事件从未发生过.
我在实际的浏览器中测试过,输入键正确地触发了预期的事件.我不确定我错过了什么.
def fill_and_trigger_enter_keypress(selector, value)
page.execute_script %Q(
var input = $('#{selector}');
input.val('#{value}');
input.trigger("keypress", [13]);
)
end
Run Code Online (Sandbox Code Playgroud)
编辑:
我也试过以下无济于事:
find('#q_name').native.send_keys(:return)
find('#q_name').native.send_keys(:enter)
Run Code Online (Sandbox Code Playgroud)
它们不会导致任何错误,但仍然没有按下回车键.
javascript jquery ruby-on-rails capybara selenium-chromedriver
我已经发现,当我想设置值的文本字段,文本区域或密码字段,我可以用ID,名称或标记为something在fill_in something, :with => some_value.然而,当我尝试将值设置为<input type="hidden">字段时,这种方法失败了(我想这样做,因为那些通常是填充的客户端脚本,我单独测试).我怎么能用Capybara设置这样一个隐藏的场地?可能吗?
HTML:
<input id='offer_latitude' name='offer[latitude]' type='hidden'>
<input id='offer_longitude' name='offer[longitude]' type='hidden'>
Run Code Online (Sandbox Code Playgroud)
规格:
describe "posting new offer" do
it "should add new offer" do
visit '/offer/new'
fill_in 'offer[latitude]', :with => '11.11'
fill_in 'offer[longitude]', :with => '12.12'
click_on 'add'
end
end
Run Code Online (Sandbox Code Playgroud)
得到:
1) posting new offer should add new offer
Failure/Error: fill_in 'offer[latitude]', :with => '11.11'
Capybara::ElementNotFound:
cannot fill in, no text field, text area or password field with id, name, or …Run Code Online (Sandbox Code Playgroud) 我是Capybara的新手并且一般都在Rails上进行测试,如果这是一个简单的答案,请原谅我.
我有这个测试
it "should be able to edit an assignment" do
visit dashboard_path
select(@project.client + " - " + @project.name, :from => "assignment_project_id")
select(@team_member.first_name + " " + @team_member.last_name, :from => "assignment_person_id")
click_button "Create assignment"
page.should have_content(@team_member.first_name)
end
Run Code Online (Sandbox Code Playgroud)
它按原样传递,但如果我添加:js => true则失败
cannot select option, no option with text 'Test client - Test project' in select box 'assignment_project_id'
Run Code Online (Sandbox Code Playgroud)
我正在使用FactoryGirl来创建数据,并且由于测试在没有JS的情况下通过,我知道该部分正在运行.
我尝试使用默认的JS驱动程序,并使用:webkit驱动程序(安装了capybara-webkit)
我想我不太了解为Capybara开启JS的目的是什么.
为什么测试会因JS而失败?
我正在尝试将请求标头"Referer"设置为欺骗来自其他网站的请求.我们需要使用特定引用者的能力测试,它将特定的表单返回给用户,否则给出另一种形式.
我可以通过以下方式在恶作剧中做到这一点:
page.driver.headers = {"Referer" => referer_string}
Run Code Online (Sandbox Code Playgroud)
但我找不到selemium驱动程序的等效功能.
如何在capybara selenium驱动程序中设置请求标头?
capybara ×10
rspec ×3
javascript ×2
ruby ×2
cucumber ×1
jquery ×1
phantomjs ×1
poltergeist ×1
testing ×1
webdriver ×1