简体   繁体   English

查找HTML元素Capybara的索引

[英]Finding the index of HTML element Capybara

I am trying to write the step definition, using Capybara, for a Cucumber scenario that is meant to confirm that checkboxes on the home page appear before the options. 我正在尝试使用Capybara为黄瓜场景编写步骤定义,该场景旨在确认主页上的复选框出现在选项之前。

Mechanically, I am trying to find the index of a specific html checkbox using its HTML ID and compare it with the index of a specific text on the home page. 从机械上讲,我正在尝试使用其HTML ID查找特定html复选框的索引,并将其与主页上特定文本的索引进行比较。 However, I have spent hours on this issue and have not been able to implement the step definition. 但是,我已经在这个问题上花费了数小时,并且无法实现步骤定义。

Would I for example be able to somehow convert the page into text and just search for words? 例如,我是否能够以某种方式将页面转换为文本并仅搜索单词?

For this web application I am using Ruby on Rails. 对于此Web应用程序,我正在使用Ruby on Rails。
Let: checkbox id = environment_Cool 让:复选框ID = environment_Cool
Let: text on the page = Cool 令:页面上的文字=酷

I would greatly appreciate your help. 非常感谢您的帮助。

Suppose you have a checkbox like this: 假设您有一个如下所示的复选框:

<form action="demo_form.asp">
  <span>
  <input type="checkbox" name="coolness" value="Cool" id="environment_Cool"  checked> Cool</span>
  <br>
  <span>
  <input type="checkbox" name="coolness" value="Cool"> Not So Cool</span><br>
  <input type="submit" value="Submit">
</form> 

Mechanically, I am trying to find the index of a specific html checkbox using its HTML ID 机械上,我试图使用其HTML ID查找特定html复选框的索引

page.find('[@id=environment_Cool ]')

and compare it with the index of a specific text on the home page. 并将其与首页上特定文本的索引进行比较。 Then it should return the text at the parent of the checklist, which is "Cool". 然后,它应该在清单的父级“ Cool”中返回文本。

puts page.find('[@id=environment_Cool ]').find(:xpath,".//..").text

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM