2

In Capybara, you can implicitly wait for an element to appear with

find('.slide', visible: true)

How can I detect that I new/different slide is visible? So I can do something like this which currently doesn't work (Capybara finds the current .slide before it is hidden instead of waiting for the current slide to hide and new slide to show):

3.times do
  find('.slide').fill_in('image[description]', with: 'foo')
  click_link 'Next slide'
end

For example

I have markup like this:

<ul>
    <li class="slide active" style="background: green;">
    </li>
    <li class="slide" style="background: blue;">
    </li>
    <li class="slide" style="background: pink;">
    </li>
</ul>
<a href="#" class="next-slide">Next slide</a>

I have some javascript to hide the active slide and fade in the next slide:

$(document).ready(function() {
    $('.next-slide').click(function(evt) {
        evt.preventDefault();
        var $activeSlide = $('.slide.active');
        $activeSlide.next().addClass('active');
        $activeSlide.hide().removeClass('active');
    });   
});

Complete JSFiddle: http://jsfiddle.net/epylinkn/fQHmf/1/

4

2 回答 2

1

实际上你可以在不修改应用程序源代码的情况下做到这一点:

(1..3).each do |i|
  find("ul > .slide.active:nth-child(#{i})").fill_in('image[description]', with: 'foo')
  click_link 'Next slide'
end

find("ul > .slide.active:nth-child(#{i})")将等到i幻灯片变为活动状态,这是您想要的。

此代码假定前 3 个子项ul是幻灯片。

于 2013-10-15T07:44:36.530 回答
1

最简单的选择可能是给每张幻灯片一个额外的类或 id - 即

<ul>
    <li class="slide slide-0 active" style="background: green;">
    </li>
    <li class="slide slide-1" style="background: blue;">
    </li>
    <li class="slide slide-2" style="background: pink;">
    </li>
</ul>

3.times do |i|
  find(".slide-#{i}", visible: true).fill_in('image[description]', with: 'foo')
  click_link 'Next slide'
end
于 2013-10-14T18:36:32.070 回答