一尘不染

测试jQuery可选的Capybara或selenium(Ctrl +单击)

selenium

我正在使用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键,因为实际上仅选择了最后选择的日期。


阅读 234

收藏
2020-06-26

共1个答案

一尘不染

您可以使用selenium-
webdriver的动作构建器。但是,firefoxdriver中似乎存在一个错误,目前阻止该错误运行(可能是问题4863)。

这是使用Chrome的“ jQuery可选”页面的工作示例:

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
2020-06-26