Class: SeleniumPage::Element

Inherits:
Object
  • Object
show all
Defined in:
lib/selenium_page/element.rb,
lib/selenium_page/element/errors.rb

Overview

Page

Defined Under Namespace

Classes: Errors

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(driver, base_element) ⇒ Element

Returns a new instance of Element.



8
9
10
11
12
13
14
15
16
# File 'lib/selenium_page/element.rb', line 8

def initialize(driver, base_element)
  raise Errors::WrongDriver unless driver.is_a? Selenium::WebDriver::Driver
  unless base_element.is_a? Selenium::WebDriver::Element
    raise Errors::WrongBaseElement
  end

  @driver = driver
  @base_element = base_element
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(called_method, *args, &block) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/selenium_page/element.rb', line 21

def method_missing(called_method, *args, &block)
  # FIXME: should this catch some specific methods (to_s ?) ?
  if base_element.respond_to?(called_method)
    base_element.send(called_method, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#base_elementObject (readonly)

Returns the value of attribute base_element.



19
20
21
# File 'lib/selenium_page/element.rb', line 19

def base_element
  @base_element
end

#driverObject (readonly)

Returns the value of attribute driver.



18
19
20
# File 'lib/selenium_page/element.rb', line 18

def driver
  @driver
end

Instance Method Details

#add_childrens(parent_selector, &block) ⇒ Object



37
38
39
40
# File 'lib/selenium_page/element.rb', line 37

def add_childrens(parent_selector, &block)
  @parent_selector = parent_selector
  instance_exec(&block) if block_given?
end

#element(element_name, element_selector, &block) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/selenium_page/element.rb', line 42

def element(element_name, element_selector, &block)
  define_singleton_method(element_name) do
    selector = @parent_selector + ' ' + element_selector
    if block_given?
      find_element(selector, &block)
    else
      find_element(selector)
    end
  end
end

#elements(collection_name, collection_selector, &block) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/selenium_page/element.rb', line 53

def elements(collection_name, collection_selector, &block)
  define_singleton_method(collection_name) do
    selector = @parent_selector + ' ' + collection_selector
    if block_given?
      find_elements(selector, &block)
    else
      find_elements(selector)
    end
  end
end

#respond_to_missing?(called_method) ⇒ Boolean

this fix the calls to :respond_to? in case of delegation to base_element the method will stay private rubocop best practice suggestion

Returns:

  • (Boolean)


33
34
35
# File 'lib/selenium_page/element.rb', line 33

def respond_to_missing?(called_method, *)
  base_element.respond_to?(called_method) || super
end