1

我的页面在输入字段#graph_start_date 上有一个 jquery ui datepicker

我正在尝试编写以下 Cucumber 步骤

When I click the graph start date
Then I should see a datepicker

这是我对步骤定义的内容:

When /I click the graph start date/ do
  find(".//*[@id='graph_start_date']").click
end

Then /^I should see a datepicker$/ do
  page.should have_xpath(".//div[@id='ui-datepicker-div' and ?????????]")
end

jquery ui datepicker 最初插入到 dom

<div class="ui-datepicker ui-widget ui-widget-content ui-helper-clearfix ui-corner-all ui-helper-hidden-accessible" id="ui-datepicker-div"></div>

当它弹出时,dom包含

<div class="ui-datepicker ui-widget ui-widget-content ui-helper-clearfix ui-corner-all ui-helper-hidden-accessible" id="ui-datepicker-div" style="position: absolute; top: 523px; left: 167.5px; z-index: 1;">

在它被解雇后,dom 包含

<div class="ui-datepicker ui-widget ui-widget-content ui-helper-clearfix ui-corner-all ui-helper-hidden-accessible" id="ui-datepicker-div" style="position: absolute; top: 523px; left: 167.5px; z-index: 1; display: none;">
4

3 回答 3

3

有一个:visible => true选项记录Capybara::Node::Matchers#has_xpath?如下:

Then /^I should see a datepicker$/ do
  page.should have_xpath(".//div[@id='ui-datepicker-div']", :visible => true)
end

然而,这可能是一个不同的问题,但对我来说,当水豚驱动的浏览器没有焦点时,日期选择器根本不会出现,所以我的测试失败(有时)

编辑:

首先,似乎日期选择器实际上并不希望点击该字段来触发,而是一个焦点,不幸的是水豚的硒驱动程序不支持它,并且当您触发点击但浏览器没有焦点时它不会触发。

触发焦点的正确方法是:

find(".//div[@id='ui-datepicker-div']").trigger('focus')

但这引发了一个Capybara::NotSupportedByDriverError:(

要解决方法,您可以使用hackish

When /I focus the graph start date/ do
  page.execute_script("$('#graph_start_date').focus()")
end

(感谢:http ://groups.google.com/group/ruby-capybara/msg/af6caeef01d978b0 )

在 Google Groups 中有各种讨论和相关问题:

于 2011-12-21T13:33:22.220 回答
0

今天早上我遇到了同样的问题,这就是我解决它的方法。

在我的功能文件中

@javascript # You need this javascript tag 
Scenario:Adding a date to a job 
  When I go to enter a date 
  Then I should see a date picker appear

在我的步骤定义中:

When /^ I go to enter a date$/ do 
  element = find_by_id("you_date_field_id") 
  element.click 
end

The /^I should see a date picker appear$/ do 
  date = find(:xpath, "//div[@id='ui-datepicker-div']") 
end

希望这可以帮助

于 2012-03-18T23:19:06.327 回答
0

我会运行一些 #focuses 字段的 javascript,单击 1 个锚点...然后让 capybara 确保字段中有正确的值。

于 2010-11-17T01:54:16.950 回答