Testing jQuery Selectable capybara or selenium (ct

2019-02-20 00:41发布

I'm using jQuery Selectable to manage a calendar. This function works great, its just a matter of getting in to test automation.

I need to select multiple non-consecutive dates from the calendar grid.

I've tried a few things, not really expecting them to work

date = '2013-05-02'
page.execute_script %{
    var e = jQuery.Event("keydown");
    e.ctrlKey = true; // # Some key code value
    e.keyCode = 17
    $("body").trigger(e);
}
find("td[data-date='#{date}']").click

I do this for a series of dates but it seems to ctrl key isn't being considered because only the last date selected actually gets selected.

1条回答
Bombasti
2楼-- · 2019-02-20 00:55

You can use selenium-webdriver's action builder. However, there seems to be a bug in the firefoxdriver that prevents this from working at the moment (possibly issue 4863).

Here is a working example of the JQuery Selectable page using Chrome:

require 'capybara'
require 'capybara/dsl'
include Capybara::DSL

#Use selenium-webdriver with chrome
Capybara.register_driver :selenium do |app|
  Capybara::Selenium::Driver.new(app, :browser => :chrome)
end
Capybara.current_driver = :selenium

#Go to the JQuery Selectable example page
Capybara.app_host = 'http://jqueryui.com/selectable/'
page.visit('')

#The controls are in a frame, so need to switch to it
within_frame 0 do
    #Create a selenium-webdriver action builder
    builder = page.driver.browser.action

    #Hold control key down
    builder.key_down(:control)

    #Click all elements that you want, in this case we click all lis
    #Note that you can retrieve the elements using capybara's
    #  standard methods. When passing them to the builder
    #  make sure to do .native
    elements = page.all('ol#selectable li')
    elements.each do |e|        
        builder.click(e.native)
    end

    #Release control key
    builder.key_up(:control)

    #Do the action setup
    builder.perform
end
查看更多
登录 后发表回答