Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在rspec中使用capybara单击下拉选项_Ruby On Rails_Testing_Rspec_Capybara_Logout - Fatal编程技术网

Ruby on rails 如何在rspec中使用capybara单击下拉选项

Ruby on rails 如何在rspec中使用capybara单击下拉选项,ruby-on-rails,testing,rspec,capybara,logout,Ruby On Rails,Testing,Rspec,Capybara,Logout,我正在使用RubyonRails3.2.3和capybara来帮助创建请求规范。我的目标是创建一个测试注销的规范请求。网页: <li class="dropdown"> <a class="dropdown-toggle" data-toggle="dropdown"> Welcome <%= current_user.first_name + " "+ current_user.last_name%> <b class="caret

我正在使用RubyonRails3.2.3和capybara来帮助创建请求规范。我的目标是创建一个测试注销的规范请求。网页:

<li class="dropdown">
  <a class="dropdown-toggle" data-toggle="dropdown">
    Welcome <%= current_user.first_name + " "+ current_user.last_name%>
    <b class="caret"></b>
  </a>
  <ul class="dropdown-menu">
    <a href="#">
      <%= link_to "Sign out", destroy_user_session_path, :method => :delete%>
    </a>
  </ul>
</li>
我不知道如何使用capybara单击注销,因为它位于下拉菜单中,因此在页面上不可见。有人知道吗


有没有其他方法可以单击下拉菜单中的某个元素?

您好,我最终找到了答案。我必须使用xpath来查找链接。基于上面的html,以下是我所做的:

在rspec中,我写道:

page.find(:xpath, "//a[@href='/users/sign_out']").click

我之所以使用//a[@href='/users/sign\u out']是因为要注销、销毁用户会话的链接路径::method=>:delete被编译为

我只需单击两个链接就可以测试下拉列表

click_link "Welcome #{current_user.first_name} #{current_user.last_name}"
click_link 'sub link'

一个简单得多的解决方案是:

选择要从菜单中选择的选项,:from=>标签名称

注意:标签名称是标签的实际文本。但是,您也可以使用here的id或name属性,我认为这样更好,因为您可能会在某个时候更改标签文本,如果更改,您的测试仍然会通过

这是我的例子

find("ol.nya-bs-select.btn-default.form-control[ng-model='scope.values.countries']")
    .find_button("Nothing selected").click

我也有类似的问题,但我的下拉列表没有任何文本,只有图标:

%liuser-menu.dropdown %a、 下拉切换{href:,数据:{toggle:dropdown},角色:按钮,aria:{haspopup:true,expanded:false} %i、 fa.fa-user %插入符号 %下拉菜单 %li.logout text=link\u注销,销毁用户会话路径,:method=>:delete 在我的规范中,我使用了:

def当我注销时 查找“用户菜单”。单击 单击“注销” 终止
我已经能够通过两次点击来测试一个非常相似的下拉列表,一次打开下拉列表,一次点击链接。我也是这么想的。你能发一些代码吗?如果可以应用到上面的html,那就太好了。谢谢。我相信这里缺少的是为请求规范启用Javascript;通常是水豚网箱。一旦启用此功能,那么单击将在浏览器运行时起作用。您好,通过这种方式,测试可以在不启用js的情况下单击。通过xpath查找比这些答案中引用的单击链接方法提供了更多的控制。这应该有更多的投票!如何单击链接下拉链接。当然,您可能需要将变量current\u user替换为为测试创建的用户对象尝试使用JavaScript驱动程序运行它,以便查看发生了什么。我是直接从我自己的项目中复制出来的,我不明白。如何使用javascript驱动程序运行。对不起,如果我的问题不好。谢谢你改变它我的例子做它我的例子,js:true doBy default这将启动Firefox并使用selenium运行这个例子,所以你可以看到它运行。您可以在末尾添加睡眠,以便有时间了解发生的情况select不适用于非html选择框元素。问题是关于按钮下拉菜单,比如TwitterBootsRap one,它不是html选择框。这个答案实际上是针对问题的标题。我想这就是为什么它拥有最多的投票权。代码不是选择代码,而是引导程序的注销代码。因此,选择将不起作用。我已经在Twitter引导按钮下拉菜单上解决了相同的问题,方法是选择特定的链接标签:find'a',:text=>option\u title.click
find("ol.nya-bs-select.btn-default.form-control[ng-model='scope.values.countries']")
    .find_button("Nothing selected").click