2

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.

recursive_acronym
  • 2,981
  • 6
  • 40
  • 59

1 Answers1

3

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
Justin Ko
  • 46,526
  • 5
  • 91
  • 101
  • seems like builder.key_down(:control).perform is the way to go – recursive_acronym Aug 20 '13 at 18:34
  • @recursive_acronym can you mark this as the accepted answer? This helped me as well and pointed me to the direction of [Selenium Action Builder](http://selenium.googlecode.com/svn/trunk/docs/api/rb/Selenium/WebDriver/ActionBuilder.html) – Ransom Apr 04 '14 at 17:49