Jquery ui jquery ui日期选择器的xpath选择器

Jquery ui jquery ui日期选择器的xpath选择器,jquery-ui,cucumber,capybara,Jquery Ui,Cucumber,Capybara,我的页面在输入字段#graph_start_date上有一个jquery ui日期选择器 我试着写以下步骤 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 datep

我的页面在输入字段#graph_start_date上有一个jquery ui日期选择器

我试着写以下步骤

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
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
jQueryUIDatePicker最初插入到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;">
<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;">

在它被解除后,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;">
<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;">


我只需运行一些javascript,集中字段,单击其中一个锚。。。然后让capybara确保字段中的值正确。

:visible=>true
选项,该选项记录在
capybara::Node::Matchers\has#u xpath?
中,因此:

Then /^I should see a datepicker$/ do
  page.should have_xpath(".//div[@id='ui-datepicker-div']", :visible => true)
end
然而,这可能是另一个问题,但对我来说,当水豚驱动的浏览器没有焦点时,日期选择器根本不会出现,因此我的测试失败(有时)

编辑:

首先,似乎datepicker实际上并不希望单击字段来触发焦点,但不幸的是,capybara的selenium驱动程序不支持该焦点,当您触发单击但浏览器没有焦点时,它不会触发

触发焦点的正确方法是:

find(".//div[@id='ui-datepicker-div']").trigger('focus')
但这会引发一个
Capybara::NotSupportedByDriverError
:(

要解决此问题,您可以使用hackish:

(感谢:)

谷歌集团对此进行了各种讨论和相关问题:


    • 今天早上我遇到了同样的问题,下面是我如何解决的

      在我的功能文件中

      @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 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
      
      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
      

      希望这有帮助

      没有问题。???我试过page.body应该有xpath(//div[@id='ui-datepicker-div'和contains(@style,'display:block')]),但是page.body甚至不包含js生成的代码,所以我该如何编写断言?实际上这也失败了(表明选择器已经弹出或曾经弹出过)page.应该有_xpath(“//div[contains(@style,'position:absolute')]),所以可能单击没有真正起作用?但是当我暂停调试器时,我可以在Webdriver Firefox窗口中看到datepicker。