Capybara integration tests with jquery.selectize

后端 未结 5 1933
隐瞒了意图╮
隐瞒了意图╮ 2021-01-17 17:49

How can I write a capybara integration test with a form using jquery.selectize?

I\'d like to test a user entering a couple of values.

5条回答
  •  有刺的猬
    2021-01-17 18:44

    I created a helper that I mix in to my capybara feature specs:

    module PageSteps
      def fill_in_selectized(key, *values)
        values.flatten.each do |value|
          page.execute_script(%{
            $('.#{key} .selectize-input input').val('#{value}');
            $('##{key}').selectize()[0].selectize.createItem();
          })
        end
      end
    end
    

    And here's an example of how it is used:

    # Single value
    fill_in_selectized('candidate_offices', 'Santa Monica')
    
    # Multiple values
    fill_in_selectized('candidate_offices', ['San Francisco', 'Santa Monica'])
    

    The first parameter is a "key" and works in our application given our markup. Depending on your markup, you might need a little tweak. This requires a Javascript enabled capybara driver (we use poltergeist).

提交回复
热议问题