capybara - 使用xPath查找离开范围内

时间:2012-04-20 16:41:01

标签: ruby ruby-on-rails-3 cucumber capybara ruby-1.9.2

我正在尝试使用默认的Rails日期,时间和日期时间字段与Capybara构建日期选择器。我使用within方法查找字段的选择框,但是当我使用xPath查找正确的框时,它会离开within范围并在元素的页面上找到第一个匹配项。< / p>

这是我正在使用的代码。我正在测试的页面有2个日期时间字段,但由于此错误,我只能让它更改第一个。目前我有一个带有id的div容器,它包含了datetime字段,但我确实打算通过标签切换代码。

module  Marketron
  module DateTime

    def select_date(field, options = {})
      date_parse = Date.parse(options[:with])

      year = date_parse.year.to_s
      month = date_parse.strftime('%B')
      day = date_parse.day.to_s

      within("div##{field}") do
        find(:xpath, "//select[contains(@id, \"_#{FIELDS[:year]}\")]").select(year)
        find(:xpath, "//select[contains(@id, \"_#{FIELDS[:month]}\")]").select(month)
        find(:xpath, "//select[contains(@id, \"_#{FIELDS[:day]}\")]").select(day)
      end

    end

    def select_time(field, options = {})
      require "time"
      time_parse = Time.parse(options[:with])

      hour = time_parse.hour.to_s.rjust(2, '0')
      minute = time_parse.min.to_s.rjust(2, '0')

      within("div##{field}") do
        find(:xpath, "//select[contains(@id, \"_#{FIELDS[:hour]}\")]").find(:xpath, "option[contains(@value, '#{hour}')]").select_option
        find(:xpath, "//select[contains(@id, \"_#{FIELDS[:minute]}\")]").find(:xpath, "option[contains(@value, '#{minute}')]").select_option

      end

    end

    def select_datetime(field, options = {})
      select_date(field, options)
      select_time(field, options)
    end

    private

      FIELDS = {year: "1i", month: "2i", day: "3i", hour: "4i", minute: "5i"}

  end
end

World(Marketron::DateTime)

1 个答案:

答案 0 :(得分:29)

您应该在xpath中指定要通过在开头添加.来启动当前节点:

find(:xpath, ".//select[contains(@id, \"_#{FIELDS[:year]}\")]")

示例:

我测试了这个HTML页面(希望不要过度简化你的页面):

<html>
    <div id='div1'>
        <span class='container'>    
            <span id='field_01'>field 1</span>
        </span>
    </div>
    <div id='div2'>
        <span class='container'>
            <span id='field_02'>field 2</span>
        </span>
    </div>  
</html>

使用内部方法,您可以在执行此操作时看到问题:

within("div#div1"){ puts find(:xpath, "//span[contains(@id, \"field\")]").text }
#=> field 1
within("div#div2"){ puts find(:xpath, "//span[contains(@id, \"field\")]").text }
#=> field 1

但你可以看到,但是指定xpath来查看当前节点(即使用.),你会得到你想要的结果:

within("div#div1"){ puts find(:xpath, ".//span[contains(@id, \"field\")]").text }
#=> field 1
within("div#div2"){ puts find(:xpath, ".//span[contains(@id, \"field\")]").text }
#=> field 2