Class: TestCentricity::ScreenObject

Inherits:
Object
  • Object
show all
Includes:
Test::Unit::Assertions
Defined in:
lib/testcentricity/app_core/screen_objects_helper.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeScreenObject

Returns a new instance of ScreenObject.



9
10
11
12
# File 'lib/testcentricity/app_core/screen_objects_helper.rb', line 9

def initialize
  raise "Screen object #{self.class.name} does not have a page_name trait defined" unless defined?(page_name)
  @locator = page_locator if defined?(page_locator)
end

Instance Attribute Details

#locatorObject (readonly)

Returns the value of attribute locator.



7
8
9
# File 'lib/testcentricity/app_core/screen_objects_helper.rb', line 7

def locator
  @locator
end

Class Method Details

.alert(element_name, locator) ⇒ Object

def self.alert(element_name, locator)

class_eval(%(def #{element_name};@#{element_name} ||= TestCentricity::AppAlert.new("#{element_name}", self, #{locator}, :page);end))

end



193
194
195
196
197
198
199
200
# File 'lib/testcentricity/app_core/screen_objects_helper.rb', line 193

def self.alert(element_name, locator)
  define_method(element_name) do
    ivar_name = "@#{element_name}"
    ivar = instance_variable_get(ivar_name)
    return ivar if ivar
    instance_variable_set(ivar_name, TestCentricity::AppAlert.new(element_name, self, locator, :page))
  end
end

.button(element_name, locator) ⇒ Object

def self.button(element_name, locator)

class_eval(%(def #{element_name};@#{element_name} ||= TestCentricity::AppButton.new("#{element_name}", self, #{locator}, :page);end))

end



41
42
43
44
45
46
47
48
# File 'lib/testcentricity/app_core/screen_objects_helper.rb', line 41

def self.button(element_name, locator)
  define_method(element_name) do
    ivar_name = "@#{element_name}"
    ivar = instance_variable_get(ivar_name)
    return ivar if ivar
    instance_variable_set(ivar_name, TestCentricity::AppButton.new(element_name, self, locator, :page))
  end
end

.buttons(element_hash) ⇒ Object



50
51
52
53
54
# File 'lib/testcentricity/app_core/screen_objects_helper.rb', line 50

def self.buttons(element_hash)
  element_hash.each do |element_name, locator|
    button(element_name, locator)
  end
end

.checkbox(element_name, locator) ⇒ Object

def self.checkbox(element_name, locator)

class_eval(%(def #{element_name};@#{element_name} ||= TestCentricity::AppCheckBox.new("#{element_name}", self, #{locator}, :page);end))

end



98
99
100
101
102
103
104
105
# File 'lib/testcentricity/app_core/screen_objects_helper.rb', line 98

def self.checkbox(element_name, locator)
  define_method(element_name) do
    ivar_name = "@#{element_name}"
    ivar = instance_variable_get(ivar_name)
    return ivar if ivar
    instance_variable_set(ivar_name, TestCentricity::AppCheckBox.new(element_name, self, locator, :page))
  end
end

.checkboxes(element_hash) ⇒ Object



107
108
109
110
111
# File 'lib/testcentricity/app_core/screen_objects_helper.rb', line 107

def self.checkboxes(element_hash)
  element_hash.each do |element_name, locator|
    checkbox(element_name, locator)
  end
end

.element(element_name, locator) ⇒ Object

def self.element(element_name, locator)

class_eval(%(def #{element_name};@#{element_name} ||= TestCentricity::AppUIElement.new("#{element_name}", self, #{locator}, :page);end))

end



22
23
24
25
26
27
28
29
# File 'lib/testcentricity/app_core/screen_objects_helper.rb', line 22

def self.element(element_name, locator)
  define_method(element_name) do
    ivar_name = "@#{element_name}"
    ivar = instance_variable_get(ivar_name)
    return ivar if ivar
    instance_variable_set(ivar_name, TestCentricity::AppUIElement.new(element_name, self, locator, :page))
  end
end

.elements(element_hash) ⇒ Object



31
32
33
34
35
# File 'lib/testcentricity/app_core/screen_objects_helper.rb', line 31

def self.elements(element_hash)
  element_hash.each do |element_name, locator|
    element(element_name, locator)
  end
end

.image(element_name, locator) ⇒ Object

def self.image(element_name, locator)

class_eval(%(def #{element_name};@#{element_name} ||= TestCentricity::AppImage.new("#{element_name}", self, #{locator}, :page);end))

end



174
175
176
177
178
179
180
181
# File 'lib/testcentricity/app_core/screen_objects_helper.rb', line 174

def self.image(element_name, locator)
  define_method(element_name) do
    ivar_name = "@#{element_name}"
    ivar = instance_variable_get(ivar_name)
    return ivar if ivar
    instance_variable_set(ivar_name, TestCentricity::AppImage.new(element_name, self, locator, :page))
  end
end

.images(element_hash) ⇒ Object



183
184
185
186
187
# File 'lib/testcentricity/app_core/screen_objects_helper.rb', line 183

def self.images(element_hash)
  element_hash.each do |element_name, locator|
    image(element_name, locator)
  end
end

.label(element_name, locator) ⇒ Object

def self.label(element_name, locator)

class_eval(%(def #{element_name};@#{element_name} ||= TestCentricity::AppLabel.new("#{element_name}", self, #{locator}, :page);end))

end



117
118
119
120
121
122
123
124
# File 'lib/testcentricity/app_core/screen_objects_helper.rb', line 117

def self.label(element_name, locator)
  define_method(element_name) do
    ivar_name = "@#{element_name}"
    ivar = instance_variable_get(ivar_name)
    return ivar if ivar
    instance_variable_set(ivar_name, TestCentricity::AppLabel.new(element_name, self, locator, :page))
  end
end

.labels(element_hash) ⇒ Object



126
127
128
129
130
# File 'lib/testcentricity/app_core/screen_objects_helper.rb', line 126

def self.labels(element_hash)
  element_hash.each do |element_name, locator|
    label(element_name, locator)
  end
end

.list(element_name, locator) ⇒ Object

def self.list(element_name, locator)

class_eval(%(def #{element_name};@#{element_name} ||= TestCentricity::AppList.new("#{element_name}", self, #{locator}, :page);end))

end



136
137
138
139
140
141
142
143
# File 'lib/testcentricity/app_core/screen_objects_helper.rb', line 136

def self.list(element_name, locator)
  define_method(element_name) do
    ivar_name = "@#{element_name}"
    ivar = instance_variable_get(ivar_name)
    return ivar if ivar
    instance_variable_set(ivar_name, TestCentricity::AppList.new(element_name, self, locator, :page))
  end
end

.lists(element_hash) ⇒ Object



145
146
147
148
149
# File 'lib/testcentricity/app_core/screen_objects_helper.rb', line 145

def self.lists(element_hash)
  element_hash.each do |element_name, locator|
    list(element_name, locator)
  end
end

.section(section_name, class_name, locator = 0) ⇒ Object



202
203
204
# File 'lib/testcentricity/app_core/screen_objects_helper.rb', line 202

def self.section(section_name, class_name, locator = 0)
  class_eval(%(def #{section_name};@#{section_name} ||= #{class_name}.new("#{section_name}", self, #{locator}, :page);end))
end

.sections(section_hash) ⇒ Object



206
207
208
209
210
# File 'lib/testcentricity/app_core/screen_objects_helper.rb', line 206

def self.sections(section_hash)
  section_hash.each do |section_name, class_name|
    section(section_name, class_name)
  end
end

.selectlist(element_name, locator) ⇒ Object

def self.selectlist(element_name, locator)

class_eval(%(def #{element_name};@#{element_name} ||= TestCentricity::AppSelectList.new("#{element_name}", self, #{locator}, :page);end))

end



155
156
157
158
159
160
161
162
# File 'lib/testcentricity/app_core/screen_objects_helper.rb', line 155

def self.selectlist(element_name, locator)
  define_method(element_name) do
    ivar_name = "@#{element_name}"
    ivar = instance_variable_get(ivar_name)
    return ivar if ivar
    instance_variable_set(ivar_name, TestCentricity::AppSelectList.new(element_name, self, locator, :page))
  end
end

.selectlists(element_hash) ⇒ Object



164
165
166
167
168
# File 'lib/testcentricity/app_core/screen_objects_helper.rb', line 164

def self.selectlists(element_hash)
  element_hash.each do |element_name, locator|
    selectlist(element_name, locator)
  end
end

.switch(element_name, locator) ⇒ Object

def self.switch(element_name, locator)

class_eval(%(def #{element_name};@#{element_name} ||= TestCentricity::AppSwitch.new("#{element_name}", self, #{locator}, :page);end))

end



79
80
81
82
83
84
85
86
# File 'lib/testcentricity/app_core/screen_objects_helper.rb', line 79

def self.switch(element_name, locator)
  define_method(element_name) do
    ivar_name = "@#{element_name}"
    ivar = instance_variable_get(ivar_name)
    return ivar if ivar
    instance_variable_set(ivar_name, TestCentricity::AppSwitch.new(element_name, self, locator, :page))
  end
end

.switches(element_hash) ⇒ Object



88
89
90
91
92
# File 'lib/testcentricity/app_core/screen_objects_helper.rb', line 88

def self.switches(element_hash)
  element_hash.each do |element_name, locator|
    switch(element_name, locator)
  end
end

.textfield(element_name, locator) ⇒ Object

def self.textfield(element_name, locator)

class_eval(%(def #{element_name};@#{element_name} ||= TestCentricity::AppTextField.new("#{element_name}", self, #{locator}, :page);end))

end



60
61
62
63
64
65
66
67
# File 'lib/testcentricity/app_core/screen_objects_helper.rb', line 60

def self.textfield(element_name, locator)
  define_method(element_name) do
    ivar_name = "@#{element_name}"
    ivar = instance_variable_get(ivar_name)
    return ivar if ivar
    instance_variable_set(ivar_name, TestCentricity::AppTextField.new(element_name, self, locator, :page))
  end
end

.textfields(element_hash) ⇒ Object



69
70
71
72
73
# File 'lib/testcentricity/app_core/screen_objects_helper.rb', line 69

def self.textfields(element_hash)
  element_hash.each do |element_name, locator|
    textfield(element_name, locator)
  end
end

.trait(trait_name, &block) ⇒ Object



14
15
16
# File 'lib/testcentricity/app_core/screen_objects_helper.rb', line 14

def self.trait(trait_name, &block)
  define_method(trait_name.to_s, &block)
end

Instance Method Details

#exists?Boolean

Returns:

  • (Boolean)


227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/testcentricity/app_core/screen_objects_helper.rb', line 227

def exists?
  @locator.is_a?(Array) ? tries ||= 2 : tries ||= 1
  if @locator.is_a?(Array)
    loc = @locator[tries - 1]
    find_element(loc.keys[0], loc.values[0])
  else
    find_element(@locator.keys[0], @locator.values[0])
  end
  true
rescue
  retry if (tries -= 1) > 0
  false
end

#load_pageObject



261
262
263
264
265
# File 'lib/testcentricity/app_core/screen_objects_helper.rb', line 261

def load_page
  navigate_to unless exists?
  verify_page_exists
  PageManager.current_page = self
end


257
# File 'lib/testcentricity/app_core/screen_objects_helper.rb', line 257

def navigate_to; end

#open_portalObject



213
214
215
216
217
# File 'lib/testcentricity/app_core/screen_objects_helper.rb', line 213

def open_portal
  environment = Environ.current

  Environ.portal_state = :open
end

#populate_data_fields(data, wait_time = nil) ⇒ Object



327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/testcentricity/app_core/screen_objects_helper.rb', line 327

def populate_data_fields(data, wait_time = nil)
  timeout = wait_time.nil? ? 5 : wait_time
  data.each do |data_field, data_param|
    unless data_param.blank?
      # make sure the intended UI target element is visible before trying to set its value
      data_field.wait_until_visible(timeout)
      if data_param == '!DELETE'
        data_field.clear
      else
        case data_field.get_object_type
        when :checkbox
          data_field.set_checkbox_state(data_param.to_bool)
        when :textfield
          data_field.clear
          data_field.set("#{data_param}\t")
        end
      end
    end
  end
end

#verify_page_existsObject



219
220
221
222
223
224
225
# File 'lib/testcentricity/app_core/screen_objects_helper.rb', line 219

def verify_page_exists
  wait = Selenium::WebDriver::Wait.new(timeout: Environ.default_max_wait_time)
  wait.until { exists? }
  PageManager.current_page = self
rescue
  raise "Could not find page_locator for screen object '#{self.class.name}' (#{@locator}) after #{Environ.default_max_wait_time} seconds"
end

#verify_page_uiObject



259
# File 'lib/testcentricity/app_core/screen_objects_helper.rb', line 259

def verify_page_ui; end

#verify_ui_states(ui_states) ⇒ Object



269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/testcentricity/app_core/screen_objects_helper.rb', line 269

def verify_ui_states(ui_states)
  ui_states.each do |ui_object, object_states|
    object_states.each do |property, state|

      puts "#{ui_object.get_name} - #{property} = #{state}" if ENV['DEBUG']

      case property
      when :class
        actual = ui_object.get_attribute(:class)
      when :exists
        actual = ui_object.exists?
      when :enabled
        actual = ui_object.enabled?
      when :disabled
        actual = ui_object.disabled?
      when :visible
        actual = ui_object.visible?
      when :hidden
        actual = ui_object.hidden?
      when :checked
        actual = ui_object.checked?
      when :selected
        actual = ui_object.selected?
      when :value
        actual = ui_object.get_value
      when :caption
        actual = ui_object.get_caption
      when :name
        actual = ui_object.tag_name
      when :placeholder
        actual = ui_object.get_placeholder
      when :readonly
        actual = ui_object.read_only?
      when :maxlength
        actual = ui_object.get_max_length
      when :items
        actual = ui_object.get_list_items
      when :itemcount
        actual = ui_object.get_item_count
      when :width
        actual = ui_object.width
      when :height
        actual = ui_object.height
      when :x
        actual = ui_object.x_loc
      when :y
        actual = ui_object.y_loc
      end
      error_msg = "Expected UI object '#{ui_object.get_name}' (#{ui_object.get_locator}) #{property} property to"
      ExceptionQueue.enqueue_comparison(state, actual, error_msg)
    end
  end
rescue ObjectNotFoundError => e
  ExceptionQueue.enqueue_exception(e.message)
ensure
  ExceptionQueue.post_exceptions
end

#wait_until_exists(seconds = nil) ⇒ Object



241
242
243
244
245
246
247
# File 'lib/testcentricity/app_core/screen_objects_helper.rb', line 241

def wait_until_exists(seconds = nil)
  timeout = seconds.nil? ? Environ.default_max_wait_time : seconds
  wait = Selenium::WebDriver::Wait.new(timeout: timeout)
  wait.until { exists? }
rescue
  raise "Screen object #{self.class.name} not found after #{timeout} seconds" unless exists?
end

#wait_until_gone(seconds = nil) ⇒ Object



249
250
251
252
253
254
255
# File 'lib/testcentricity/app_core/screen_objects_helper.rb', line 249

def wait_until_gone(seconds = nil)
  timeout = seconds.nil? ? Environ.default_max_wait_time : seconds
  wait = Selenium::WebDriver::Wait.new(timeout: timeout)
  wait.until { !exists? }
rescue
  raise "Screen object #{self.class.name} remained visible after #{timeout} seconds" if exists?
end