我需要写一系列的测试来验证我的web应用程序中不同页面上的标签顺序。我面临的问题是,据我所知,水豚的send_keys方法需要你在一个特定的元素上。让我用一个非常简单的例子来说明。
<div class="group">
<input type="text" id="first">
<input type="text" id="second">
<input type="text" id="third">
</div>在我的页面(称为www.mypage.com/home)上,我有三个输入字段,如下所示。我想要做的是转到页面,验证当我用tab键通过它时,它是从第一个到第二个再到第三个……所以基本上我想做的是:
visit 'www.mypage.com/home'
send_keys(:tab)
expect(find('#first')).to have_focus
send_keys(:tab)
expect(find('#second')).to have_focus
send_keys(:tab)
expect(find('#third')).to have_focus问题是,正如我上面提到的,水豚需要我在一个特定的元素上。因此,虽然我可以使用下面的代码测试它从第一个到第二个和第二个到第三个,但我不知道如何验证第一个跳转到的东西是first。另外,下面的代码有一堆额外的查找。
page.find('#first').send_keys(:tab)
expect(find('#second')).to have_focus
page.find('#second').send_keys(:tab)
expect(find('#third')).to have_focus发布于 2018-03-13 02:39:21
最初的Tab键按下必须发送到某个地方,所以选择一些东西,比如body元素,并将其发送到该位置。从那时起,你知道了哪个元素有焦点,以及你想要把键发送到哪里,所以只需找到它们一次并使用它们(而不是重复查找)
visit 'www.mypage.com/home'
first, second, third = find('#first'), find('#second'), find('#third')
find('body').send_keys(:tab) # or find('*:focus'), etc.
expect(first).to have_focus
first.send_keys(:tab)
expect(second).to have_focus
second.send_keys(:tab)
expect(third).to have_focushttps://stackoverflow.com/questions/49202929
复制相似问题