Testing JQuery autocomplete ui with cucumber

后端 未结 4 991
借酒劲吻你
借酒劲吻你 2021-01-06 09:44

I got this cucumber sceanario:

When I fill in \"End User\" with \"john\"
Then wait
Then wait
When I click \"John Doe\"
Then show me the page
<
相关标签:
4条回答
  • 2021-01-06 10:12

    I myself bumped into the same pain spot too. After spending few hours on this, I have one good helper that works both with selenium and polstergeist plus no usage of sleep(). The following code has been tested with Capybara 2.1.0:

      def fill_autocomplete(field, options = {})
        fill_in field, with: options[:with]
    
        page.execute_script %Q{ $('##{field}').trigger('focus') }
        page.execute_script %Q{ $('##{field}').trigger('keydown') }
        selector = %Q{ul.ui-autocomplete li.ui-menu-item a:contains("#{options[:select]}")}
    
        page.should have_selector('ul.ui-autocomplete li.ui-menu-item a')
        page.execute_script %Q{ $('#{selector}').trigger('mouseenter').click() }
      end
    

    Basically, I tell Capybara to fill in the input field then use JS to trigger the keydown event to activate autocomplete. However instead of sleep(), I take advantage of page.should have_selector('ul.ui-autocomplete li.ui-menu-item a') that wait till the dropdown list appeared. Then I use JS to trigger the mouseenter event then click. I wish that there are better way than doing things with JS eval, but this is the most reliable solution that I could come up.

    0 讨论(0)
  • 2021-01-06 10:15

    While this is not a solution, this could lead you down the path of the solution:

    the click event is bound to the UL, no the a or li:

    $('ul.ui-autocomplete').click();

    However, this didn't work for me. I imagine the click event relies on some sort of state with the (a)s and the (li)s. It adds a few classes and an ID to the currently hovered item which I simulated...

    $('a.ui-corner-all').attr('id','ui-active-menuitem') $('a.ui-corner-all').addClass('ui-active-menuitem')

    Still no dice. No errors, but no action either.

    This should lead to the correct path...I just wish I could have figured it out!

    0 讨论(0)
  • 2021-01-06 10:23

    You need to first trigger a mouseover, then a click.

    0 讨论(0)
  • 2021-01-06 10:36

    Give this a go

    When /^I type in "([^\"]*)" into autocomplete list "([^\"]*)" and I choose "([^\"]*)"$/ do |typed, input_name,should_select|
       page.driver.browser.execute_script %Q{ $('input[data-autocomplete]').trigger("focus") }
       fill_in("#{input_name}",:with => typed)
       page.driver.browser.execute_script %Q{ $('input[data-autocomplete]').trigger("keydown") }
       sleep 1
       page.driver.browser.execute_script %Q{ $('.ui-menu-item a:contains("#{should_select}")').trigger("mouseenter").trigger("click"); }
    end
    

    Use like so

    And I type in "Foo" into autocomplete list "input_id" and I choose "Foobar"
    
    0 讨论(0)
提交回复
热议问题