我正在使用jQuery Selectable来管理日历。这个功能非常有用,只需要进行自动化测试。
我需要从日历网格中选择多个非连续日期。
我尝试过一些事情,并不是真的希望它们能够发挥作用
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
我为一系列日期执行此操作但似乎没有考虑ctrl键,因为实际上只选择了最后选择的日期。
答案 0 :(得分:3)
您可以使用selenium-webdriver的动作构建器。但是,firefoxdriver中似乎存在一个错误,阻止它在此刻工作(可能是issue 4863)。
以下是使用Chrome的JQuery Selectable页面的工作示例:
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