module Bauxite::SelectorModule

Selector common state and behavior.

Public Class Methods

new(ctx, default_selector) click to toggle source

Constructs a new test selector instance.

# File lib/bauxite/core/selector.rb, line 28
def initialize(ctx, default_selector)
        @ctx = ctx
        @default = default_selector
end

Public Instance Methods

find(selector, &block) click to toggle source

Searches for elements using the specified selector string.

For more information see Bauxite::Context#find.

Selectors calling this method should forward their block as well.

For example:

# === selectors/example.rb ======= #
class Selector
    # :category: Selector Methods
    def example(arg, &block)
        find(arg, &block)
    end
end
# === end selectors/example.rb === #
# File lib/bauxite/core/selector.rb, line 49
def find(selector, &block)
        data = selector.split('=', 2)
        type = data[0]
        arg  = data[-1]
        unless data.length == 2 and type =~ /^[a-z_]+$/i
                type = @default
                arg  = selector
        end
        raise ArgumentError, "Invalid selector type '#{type}'" unless Context::selectors.include? type
        
        custom_selectors = Context::selectors(false)
        return send(type            , arg, &block) if custom_selectors.include? type
        return send(type+'_selector', arg, &block) if custom_selectors.include? type+'_selector'
        selenium_find(type, arg, &block)
end

Protected Instance Methods

selenium_find(type, selector) { |element| ... } click to toggle source

Searches for elements using standard Selenium selectors.

Selectors calling this method should forward their block as well.

# === selectors/data.rb ======= #
class Selector
    # :category: Selector Methods
    def data(arg, &block)
        # selector code goes here, for example:
        selenium_find(:css, "[data='#{arg}']", &block)
    end
end
# === end selectors/data.rb === #
# File lib/bauxite/core/selector.rb, line 80
def selenium_find(type, selector)
        element = @ctx.driver.find_element(type, selector)
        yield element if block_given?
        element
end