我有两个具有相同类和ID的div。我在内部使用,以便我可以模拟点击?

时间:2021-04-20 20:31:58

I work on a Rails project that uses Capybara for front end testing. I ended up in a situation where I have duplicate div tags with the same ID. How can I use capybara scoping so that I select only one of them and perform my tests within that div?

我正在开发一个使用Capybara进行前端测试的Rails项目。我最终遇到了具有相同ID的重复div标签的情况。我怎样才能使用水豚范围,以便我只选择其中一个并在该div中执行我的测试?

1 个解决方案

#1


1  

Given the html

鉴于html

<div id="wrapper1">
  <div id="conflict">...</div>
</div>
<div id="wrapper2">
  <div id="conflict">...</div>
</div>

then you should be able to do

那么你应该能够做到

within("#wrapper1") do
  find("#conflict") # will find the matching element inside the wrapper1 div
end

However you really should just fix the HTML and any JS that uses those divs, since it is technically illegal HTML which can cause any number of unpredictable behaviors

但是你真的应该修复HTML和使用这些div的任何JS,因为它在技术上是非法的HTML,它可能导致任何数量的不可预测的行为

#1


1  

Given the html

鉴于html

<div id="wrapper1">
  <div id="conflict">...</div>
</div>
<div id="wrapper2">
  <div id="conflict">...</div>
</div>

then you should be able to do

那么你应该能够做到

within("#wrapper1") do
  find("#conflict") # will find the matching element inside the wrapper1 div
end

However you really should just fix the HTML and any JS that uses those divs, since it is technically illegal HTML which can cause any number of unpredictable behaviors

但是你真的应该修复HTML和使用这些div的任何JS,因为它在技术上是非法的HTML,它可能导致任何数量的不可预测的行为