Sha256: b5e3cc25821b5cc0b658ecd870ab2992380075a40c6fdde66bc70def1acb2c3d

Contents?: true

Size: 1.23 KB

Versions: 45

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true

Capybara.add_selector(:field, locator_type: [String, Symbol]) do
  visible { |options| :hidden if options[:type].to_s == 'hidden' }

  xpath do |locator, **options|
    invalid_types = %w[submit image]
    invalid_types << 'hidden' unless options[:type].to_s == 'hidden'
    xpath = XPath.descendant(:input, :textarea, :select)[!XPath.attr(:type).one_of(*invalid_types)]
    locate_field(xpath, locator, **options)
  end

  expression_filter(:type) do |expr, type|
    type = type.to_s
    if %w[textarea select].include?(type)
      expr.self(type.to_sym)
    else
      expr[XPath.attr(:type) == type]
    end
  end

  filter_set(:_field) # checked/unchecked/disabled/multiple/name/placeholder

  node_filter(:readonly, :boolean) { |node, value| !(value ^ node.readonly?) }

  node_filter(:with) do |node, with|
    val = node.value
    (with.is_a?(Regexp) ? with.match?(val) : val == with.to_s).tap do |res|
      add_error("Expected value to be #{with.inspect} but was #{val.inspect}") unless res
    end
  end

  describe_expression_filters do |type: nil, **|
    " of type #{type.inspect}" if type
  end

  describe_node_filters do |**options|
    " with value #{options[:with].to_s.inspect}" if options.key?(:with)
  end
end

Version data entries

45 entries across 34 versions & 4 rubygems

Version Path
capybara-3.32.1 lib/capybara/selector/definition/field.rb
capybara-3.32.0 lib/capybara/selector/definition/field.rb
tdiary-5.1.1 vendor/bundle/ruby/2.7.0/gems/capybara-3.31.0/lib/capybara/selector/definition/field.rb
capybara-3.31.0 lib/capybara/selector/definition/field.rb
capybara-3.30.0 lib/capybara/selector/definition/field.rb