Class: Selenium::WebDriver::Elements::Element

Inherits:
Object
  • Object
show all
Defined in:
lib/selenium/webdriver/elements/element.rb

Direct Known Subclasses

Browser, Button, Checkbox, FileChooser, Form, Select, Textbox

Instance Method Summary collapse

Constructor Details

#initialize(element, browser) ⇒ Element

Returns a new instance of Element.



10
11
12
13
14
15
16
# File 'lib/selenium/webdriver/elements/element.rb', line 10

def initialize element, browser
  unless element.is_a? Selenium::WebDriver::Element
    raise TypeError.new "Can't create Element decorator for #{element.inspect}"
  end
  @element = element
  @browser = browser
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(methodname, *args) ⇒ Object



18
19
20
# File 'lib/selenium/webdriver/elements/element.rb', line 18

def method_missing methodname, *args
  @element.send methodname.to_sym, *args
end

Instance Method Details

#create_element(element, browser) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/selenium/webdriver/elements/element.rb', line 22

def create_element element, browser
  case element.tag_name
    when 'form' then Form.new element, browser
    when 'input' then
      type = element['type']
      case element['type']
        when 'text', 'password' then Textbox.new element, browser
        when 'submit', 'reset', 'button', 'image' then Button.new element, browser
        when 'checkbox' then Checkbox.new element, browser
        when 'file' then FileChooser.new element, browser
        # TODO: implement radio buttons
        #when 'radio' then Element.new element, browser
        else Element.new element, browser
      end
    when 'textarea' then Textbox.new element, browser
    when 'select' then Select.new element, browser
    else Element.new element, browser
  end
end

#element_present?(*args) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/selenium/webdriver/elements/element.rb', line 55

def element_present? *args
  find_element(*args) != nil
end

#find_element(*args) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/selenium/webdriver/elements/element.rb', line 42

def find_element *args
  begin
    el = @element.find_element *args
    create_element(el, @browser)
  rescue Selenium::WebDriver::Error::NoSuchElementError
    nil
  end
end

#find_elements(*args) ⇒ Object



51
52
53
# File 'lib/selenium/webdriver/elements/element.rb', line 51

def find_elements *args
  @element.find_elements(*args).collect { |el| create_element el, @browser }
end