Class: ElementBase

Inherits:
Object
  • Object
show all
Includes:
ElementBaseOptionParser, ElementFinder
Defined in:
lib/CalabashPageObjects/element_base.rb

Overview

The main class for functionality for defined elements.

Direct Known Subclasses

AElement, IElement

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(locator) ⇒ ElementBase

Constructor for ElementBase. Takes an argument for the Calabash Query that locates the object.



14
15
16
# File 'lib/CalabashPageObjects/element_base.rb', line 14

def initialize(locator)
  @locator = locator
end

Instance Attribute Details

#locatorObject (readonly)

Returns the value of attribute locator.



8
9
10
# File 'lib/CalabashPageObjects/element_base.rb', line 8

def locator
  @locator
end

Instance Method Details

#check(options = {}) ⇒ Object

Set a checkbox element to ‘checked’ state. Can take an argument for timeout. Default is 1 second. Can take an argument for parent. Default is nil. Can take an argument for webview. Default is false.



89
90
91
92
93
94
95
# File 'lib/CalabashPageObjects/element_base.rb', line 89

def check(options = {})
  opts = options_parser(options, timeout: 1, parent: nil, webview: false)

  find(opts[:timeout], opts[:parent], opts[:webview])
  puts "Setting checkbox with locator #{@locator} to checked..." if CPO_LOGGING
  query("#{@locator}", setChecked: true)
end

#checked?(options = {}) ⇒ Boolean

Find the checked status of a checkbox element. Can take an argument for timeout. Default is 1 second. Can take an argument for parent. Default is nil. Can take an argument for webview. Default is false.

Returns:

  • (Boolean)


113
114
115
116
117
118
119
# File 'lib/CalabashPageObjects/element_base.rb', line 113

def checked?(options = {})
  opts = options_parser(options, timeout: 1, parent: nil, webview: false)

  find(opts[:timeout], opts[:parent], opts[:webview])
  puts "Checking status of checkbox element with locator #{@locator}." if CPO_LOGGING
  query("#{@locator}", :isChecked)[0]
end

#input(value, options = {}) ⇒ Object

Clears the text in an element and then enters the text that is passed into the method. Always takes an argument for ‘value’. Can take an argument for timeout. Default is 1 second. Can take an argument for parent. Default is nil. Can take an argument for webview. Default is false.



76
77
78
79
80
81
82
83
# File 'lib/CalabashPageObjects/element_base.rb', line 76

def input(value, options = {})
  opts = options_parser(options, timeout: 1, parent: nil, webview: false)
  find(opts[:timeout], opts[:parent], opts[:webview])
  puts "Clearing text from element with locator #{@locator}..." if CPO_LOGGING
  clear_text_in(@locator)
  puts "Entering text in element with locator #{@locator}..." if CPO_LOGGING
  enter_text(@locator, value)
end

#look_for(options = {}) ⇒ Object

Search the whole screen for an element, scrolling down if it doesn’t find it after the timeout. Can take an argument for timeout. Default is 1 second. Can take an argument for parent. Default is nil. Can take an argument for webview. Default is false.



137
138
139
140
# File 'lib/CalabashPageObjects/element_base.rb', line 137

def look_for(options = {})
  opts = options_parser(options, timeout: 1, parent: nil, webview: false)
  fail @wait_error, "Timeout waiting for element with locator #{@locator}" unless find(opts[:timeout], opts[:parent], opts[:webview])
end

#present?(options = {}) ⇒ Boolean

Checks to see if the element is present. Can optionally scroll down to try and find the element. Can take an argument for timeout. Default is 0.5 as 0 can cause calabash to hang. Can take an argument for parent. Default is nil Can take an argument for webview. Default is false Can take an argument for scroll. Default is false

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/CalabashPageObjects/element_base.rb', line 44

def present?(options = {})
  opts = options_parser(options, timeout: 0.5, parent: nil, webview: false, scroll: false)
  if opts[:scroll]
    find(opts[:timeout], opts[:parent], opts[:webview])
  else
    puts "Checking for the presence of an element with locator #{@locator} after #{opts[:timeout]} seconds..." if CPO_LOGGING
    begin
      wait_for_element_exists(@locator, timeout: opts[:timeout], screenshot_on_error: false)
      true
    rescue
      false
    end
  end
end

#prod(options = {}) ⇒ Object

Taps the element. Can take an argument for timeout. Default is 1 second Can take an argument for parent. Default is nil. Can take an argument for webview. Default is false.



63
64
65
66
67
68
69
# File 'lib/CalabashPageObjects/element_base.rb', line 63

def prod(options = {})
  opts = options_parser(options, timeout: 1, parent: nil, webview: false)
  find(opts[:timeout], opts[:parent], opts[:webview])
  sleep 0.1
  puts "Touching an element with locator #{@locator}." if CPO_LOGGING
  touch(@locator)
end

#screen_queryObject

Queries the current screen using the elements locator.



19
20
21
# File 'lib/CalabashPageObjects/element_base.rb', line 19

def screen_query
  query("#{@locator}")
end

#text(options = {}) ⇒ Object

Retrieve the text attribute of an element. Can take an argument for timeout. Default is 1 second. Can take an argument for parent. Default is nil. Can take an argument for webview. Default is false.



125
126
127
128
129
130
131
# File 'lib/CalabashPageObjects/element_base.rb', line 125

def text(options = {})
  opts = options_parser(options, timeout: 1, parent: nil, webview: false)

  find(opts[:timeout], opts[:parent], opts[:webview])
  puts "Retrieving text from element with locator #{@locator}..." if CPO_LOGGING
  query("#{@locator}", :text)[0]
end

#uncheck(options = {}) ⇒ Object

Set a checkbox element to ‘unchecked’ state. Can take an argument for timeout. Default is 1 second. Can take an argument for parent. Default is nil. Can take an argument for webview. Default is false.



101
102
103
104
105
106
107
# File 'lib/CalabashPageObjects/element_base.rb', line 101

def uncheck(options = {})
  opts = options_parser(options, timeout: 1, parent: nil, webview: false)

  find(opts[:timeout], opts[:parent], opts[:webview])
  puts "Setting checkbox with locator #{@locator} to unchecked..." if CPO_LOGGING
  query("#{@locator}", setChecked: false)
end

#when_not_visible(options = {}) ⇒ Object

Waits for an element to not be visible on the current screen. Can take an argument for timeout. Default is 10 seconds.



33
34
35
36
37
# File 'lib/CalabashPageObjects/element_base.rb', line 33

def when_not_visible(options = {})
  opts = options_parser(options, timeout: 10)
  puts "Waiting for element with locator #{@locator} to not be present..." if CPO_LOGGING
  wait_for_element_does_not_exist(@locator, timeout: opts[:timeout], screenshot_on_error: false)
end

#when_visible(options = {}) ⇒ Object

Waits for an element to be visible on the current screen. Can take an argument for timeout. Default is 10 seconds.



25
26
27
28
29
# File 'lib/CalabashPageObjects/element_base.rb', line 25

def when_visible(options = {})
  opts = options_parser(options, timeout: 10)
  puts "Waiting for element with locator #{@locator} to appear..." if CPO_LOGGING
  wait_for_element_exists(@locator, timeout: opts[:timeout], screenshot_on_error: false)
end