Class: Parfait::Control

Inherits:
ParfaitArtifact show all
Defined in:
lib/parfait/control.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ParfaitArtifact

#add_check, #add_generic_present, #add_present, #check, #is_present_defined?, #present, #verify_presence

Constructor Details

#initialize(opts = {}) ⇒ Control

Create a new control object

Options

name

the name used to identify this control

logtext

the text to be used when referring to this control in logs

aliases

specifies an array of aliases for the control

parent

specifies the parent object (Region or Page) of this control

Example

newcontrol = Parfait::Control.new(
  :name => "User ID",
  :logtext = "user ID"
)


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/parfait/control.rb', line 23

def initialize(opts = {})
o = {
    :name => nil,
    :logtext => nil,
    :aliases => [],
    :parent => nil
  }.merge(opts)
  @name = o[:name]
  @logtext = o[:logtext]
  @aliases = o[:aliases]
  @parent = o[:parent]

  @set_method = nil
  @get_method = nil
  @update_method = nil
  @retrieve_method = nil
  @verify_method = nil
  @confirm_method = nil
  @goto_method = nil
  @navigate_method = nil

  if @name
    unless @name.is_a?(String)
      raise "Name must be a String when adding a control"
    end
  else
    raise "Name must be specified when adding a control"
  end
  if @logtext
    unless @logtext.is_a?(String)
      raise "Logtext must be a String when adding a control"
    end
  else
    raise "Logtext must be specified when adding a control" 
  end
  if @aliases
    unless @aliases.is_a?(Array)
      raise "Parfait::Control requires aliases to be an array"
    end
    @aliases.each do |my_alias|
      raise "Parfait::Control requires each alias in the array to be a string" unless my_alias.is_a?(String)
    end
  end

  if @parent
    if @parent.is_a? Parfait::Page
      add_to_page(@parent)
    else
      if @parent.is_a? Parfait::Region
        add_to_region(@parent)
      else
        raise "Parent specified for Control \"#{@name}\", but parent object type unrecognized."
      end
    end
  end

  super
end

Instance Attribute Details

#aliasesObject (readonly)

Returns the value of attribute aliases.



5
6
7
# File 'lib/parfait/control.rb', line 5

def aliases
  @aliases
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/parfait/control.rb', line 5

def name
  @name
end

Instance Method Details

#add_confirm(&block) ⇒ Object

Method description

Options

option

specifies something

Example

$$$ Need an example $$$


373
374
375
# File 'lib/parfait/control.rb', line 373

def add_confirm(&block)
  @confirm_method = block
end

#add_generic_confirmObject

Method description

Depends on get, retrieve

Options

option

specifies something

Example

$$$ Need an example $$$


485
486
487
488
489
490
491
492
493
494
# File 'lib/parfait/control.rb', line 485

def add_generic_confirm()
  add_confirm { |value,opts|
    retval = false
    found_value = retrieve()
    if value == found_value
      retval = true
    end
    retval
  }        
end

#add_generic_navigateObject

Method description

Depends on goto

Options

option

specifies something

Example

$$$ Need an example $$$


508
509
510
511
512
513
# File 'lib/parfait/control.rb', line 508

def add_generic_navigate()
  add_navigate { |opts|
    Parfait.log("Navigating to #{@logtext}")
    goto(opts)
  }
end

#add_generic_retrieveObject

Method description

Options

option

specifies something

Example

$$$ Need an example $$$


443
444
445
446
447
# File 'lib/parfait/control.rb', line 443

def add_generic_retrieve()
  add_retrieve { |opts|
    get(opts)
  }
end

#add_generic_updateObject

Method description

Depends on get, retrieve, set

Options

option

specifies something

Example

$$$ Need an example $$$


418
419
420
421
422
423
424
425
426
427
428
429
430
431
# File 'lib/parfait/control.rb', line 418

def add_generic_update()
  add_update { |value,opts|
    new_value = value
    found_value = retrieve()
    if new_value == found_value
      capital_text = @logtext
      capital_text[0] = capital_text[0].capitalize
      Parfait.log("#{capital_text} is already set to \"#{new_value}\"")
    else
      Parfait.log("Entering #{@logtext}: \"#{new_value}\" (was \"#{found_value}\")")
      set(new_value,opts)
    end
  }
end

#add_generic_verifyObject

Method description

Depends on get, retrieve

Options

option

specifies something

Example

$$$ Need an example $$$


461
462
463
464
465
466
467
468
469
470
471
# File 'lib/parfait/control.rb', line 461

def add_generic_verify()
  add_verify { |value,opts|
    found_value = retrieve()
    if value == found_value
      Parfait.log("Verified #{@logtext} to be \"#{value}\"")
    else
      raise "Expected #{@logtext} to be \"#{value}\", but found \"#{found_value}\" instead"
    end
    true
  }    
end

#add_get(&block) ⇒ Object

Method description

Options

option

specifies something

Example

$$$ Need an example $$$


292
293
294
295
296
297
298
299
300
301
# File 'lib/parfait/control.rb', line 292

def add_get(&block)
  @get_method = block
  
  add_generic_retrieve() unless @retrieve_method
  add_generic_confirm() unless @confirm_method
  add_generic_verify() unless @verify_method
  if @set_method != nil
    add_generic_update unless @update_method
  end 
end

#add_goto(&block) ⇒ Object

Method description

Options

option

specifies something

Example

$$$ Need an example $$$


387
388
389
390
# File 'lib/parfait/control.rb', line 387

def add_goto(&block)
  @goto_method = block
  add_generic_navigate() unless @navigate_method
end

#add_navigate(&block) ⇒ Object

Method description

Options

option

specifies something

Example

$$$ Need an example $$$


402
403
404
# File 'lib/parfait/control.rb', line 402

def add_navigate(&block)
  @navigate_method = block
end

#add_retrieve(&block) ⇒ Object

Method description

Options

option

specifies something

Example

$$$ Need an example $$$


345
346
347
# File 'lib/parfait/control.rb', line 345

def add_retrieve(&block)
  @retrieve_method = block
end

#add_set(&block) ⇒ Object

Method description

Options

option

specifies something

Example

$$$ Need an example $$$


313
314
315
316
317
318
319
# File 'lib/parfait/control.rb', line 313

def add_set(&block)
  @set_method = block

  if @get_method != nil
    add_generic_update() unless @update_method
  end 
end

#add_to_page(page) ⇒ Object

Add this Control to a Page

Options

page

specifies a Parfait::Page object to add this Control to

Example

loginpage = Parfait::Page.new(
  :name => "Login Page"
)
newcontrol = Parfait::Control.new(
  :name => "User ID",
  :logtext = "user ID"
)
newcontrol.add_to_page(loginpage)


100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/parfait/control.rb', line 100

def add_to_page(page)

  if page
    case
    when page.is_a?(Parfait::Page)
      page.add_control(self)
    else
      raise "Input value must be a Page object when adding this Control to a Page"
    end
  else
    raise "Input value cannot be nil when adding this Control to a Page"
  end
  self
end

#add_to_region(region) ⇒ Object

Add this Control to a Region

Options

region

specifies a Parfait::Region object to add this Control to

Example

user = Parfait::Region.new(
  :name => "User"
)
newcontrol = Parfait::Control.new(
  :name => "Edit User",
  :logtext = "edit user link"
)
user.add_to_region(newcontrol)


133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/parfait/control.rb', line 133

def add_to_region(region)

  if region
    case
    when region.is_a?(Parfait::Region)
      region.add_control(self)
    else
      raise "Input value must be a Region object when adding this Control to a Region"
    end
  else
    raise "Input value cannot be nil when adding this Control to a Region"
  end
  self
end

#add_update(&block) ⇒ Object

Method description

Options

option

specifies something

Example

$$$ Need an example $$$


331
332
333
# File 'lib/parfait/control.rb', line 331

def add_update(&block)
  @update_method = block
end

#add_verify(&block) ⇒ Object

Method description

Options

option

specifies something

Example

$$$ Need an example $$$


359
360
361
# File 'lib/parfait/control.rb', line 359

def add_verify(&block)
  @verify_method = block
end

#confirm(value, opts = {}) ⇒ Object

Method description

Options

option

specifies something

Example

$$$ Need an example $$$


232
233
234
235
# File 'lib/parfait/control.rb', line 232

def confirm(value,opts = {})
  verify_control_presence("confirm")
  return @confirm_method.call(value,opts)
end

#get(opts = {}) ⇒ Object

Method description

Options

option

specifies something

Example

$$$ Need an example $$$


172
173
174
175
# File 'lib/parfait/control.rb', line 172

def get(opts = {})
  verify_control_presence("get")
  return @get_method.call(opts)
end

#goto(opts = {}) ⇒ Object

Method description

Options

option

specifies something

Example

$$$ Need an example $$$


262
263
264
265
# File 'lib/parfait/control.rb', line 262

def goto(opts = {})
  verify_control_presence("goto")
  return @goto_method.call(opts)
end

Method description

Options

option

specifies something

Example

$$$ Need an example $$$


277
278
279
280
# File 'lib/parfait/control.rb', line 277

def navigate(opts = {})
  verify_control_presence("navigate")
  return @navigate_method.call(opts)
end

#retrieve(opts = {}) ⇒ Object

Method description

Options

option

specifies something

Example

$$$ Need an example $$$


202
203
204
205
# File 'lib/parfait/control.rb', line 202

def retrieve(opts = {})
  verify_control_presence("retrieve")
  return @retrieve_method.call(opts)
end

#set(value, opts = {}) ⇒ Object

Set the value for this control

Options

value

specifies the value that this control will be set to

Example

$$$ Need an example $$$


187
188
189
190
# File 'lib/parfait/control.rb', line 187

def set(value,opts = {})
  verify_control_presence("set")
  @set_method.call(value,opts)
end

#update(value, opts = {}) ⇒ Object

Method description

Options

option

specifies something

Example

$$$ Need an example $$$


217
218
219
220
# File 'lib/parfait/control.rb', line 217

def update(value,opts = {})
  verify_control_presence("update")
  @update_method.call(value,opts)
end

#verify(value, opts = {}) ⇒ Object

Method description

Options

option

specifies something

Example

$$$ Need an example $$$


247
248
249
250
# File 'lib/parfait/control.rb', line 247

def verify(value,opts = {})
  verify_control_presence("verify")
  @verify_method.call(value,opts)
end

#verify_control_presence(directive_name) ⇒ Object

Method description

Options

option

specifies something

Example

$$$ Need an example $$$


158
159
160
# File 'lib/parfait/control.rb', line 158

def verify_control_presence(directive_name)
  verify_presence "Cannot call \"#{directive_name}\" directive because presence check for control \"#{@name}\" failed"
end