Top Level Namespace

Includes:
RbConfig, Test::Unit::Assertions

Defined Under Namespace

Modules: Anhpham Classes: DB_Utils, DatabaseMethods, IFD_Assertion, IFD_Connections, IFD_Email, IFD_WS, TestCaseFailed, Util, WebServiceMethods

Instance Method Summary collapse

Instance Method Details

#call_step(str_step) ⇒ Object

Call another step



9
10
11
12
# File 'lib/anhpham/methods/core.rb', line 9

def call_step str_step
  put_log "\n-=> call step: #{str_step}"
  step %{#{str_step}}
end

#check_alert_text(text) ⇒ Object

method to check javascript pop-up alert text



42
43
44
45
46
# File 'lib/anhpham/methods/javascript_handling_methods.rb', line 42

def check_alert_text(text)
  if get_alert_text != text
    raise TestCaseFailed,  "Text on alert pop up not matched, Actual Text : #{get_alert_text}, Expected Text : #{text}"
  end
end

#check_element_enable(element, test_case) ⇒ Object

Element enabled checking param 1 : String : Expected element text param 2 : Boolean : test case [true or flase]



220
221
222
223
224
225
226
227
228
# File 'lib/anhpham/methods/web_methods.rb', line 220

def check_element_enable(element, test_case)
  result = is_element_enabled(element)

  if test_case
    raise TestCaseFailed, 'Element Not Enabled' unless result
  else
    raise TestCaseFailed, 'Element Enabled' unless !result
  end
end

#check_partial_title(partial_text_title, test_case) ⇒ Object

Method to verify partial title param 1 : String : partial title string param 2 : Boolean : test case [true or flase]



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/anhpham/methods/web_methods.rb', line 30

def check_partial_title(partial_text_title, test_case)
  page_title = get_page_title
  if test_case
    if not page_title.include? "#{partial_text_title}"
      raise TestCaseFailed, 'Partial Page Title Not Present'
    end
  else
    if page_title.include? "#{partial_text_title}"
      raise TestCaseFailed, 'Page Title Matched'
    end
  end
end

#check_title(title, test_case) ⇒ Object

Method to verify title param 1 : String : expected title param 2 : Boolean : test case [true or flase]



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/anhpham/methods/web_methods.rb', line 14

def check_title(title, test_case)
  page_title = get_page_title
  if test_case
    if page_title != "#{title}"
      raise TestCaseFailed, "Page Title Not Matched, Actual Page Title : #{page_title}, Expected Page Title : #{title}"
    end
  else
    if page_title == "#{title}"
      raise TestCaseFailed, "Page Title Matched, Actual Page Title: #{page_title}"
    end
  end
end

#close_windowsObject



19
20
21
22
# File 'lib/anhpham/methods/javascript_handling_methods.rb', line 19

def close_windows
  page.driver.browser.close
  page.driver.browser.switch_to.window(page.driver.browser.window_handles[0])
end

#double_click(element) ⇒ Object



259
260
261
262
# File 'lib/anhpham/methods/web_methods.rb', line 259

def double_click(element)
  foundElement = find_object(element)
  page.driver.browser.mouse.double_click(foundElement.native)
end

#execute_checkexists(negate, element, timeout) ⇒ Object

Check exists



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/anhpham/methods/web_methods.rb', line 156

def execute_checkexists negate, element, timeout
  origin_timeout = $_CFWEB['Wait Time']
  set_time_out(timeout)
  foundElement = find_object(element)
  set_time_out(origin_timeout)
  #put_log "\nexecute_checkexists : #{foundElement}"
  check = false
  if foundElement != nil
    check = true
  end

  negate = negate.strip if negate != nil
  #puts "---------->#{negate}"
  if negate == "not"
    # check.should_not eq true
    expect(check).not_to eql true
  elsif check.should eq true
    expect(check).to eq true
  end
end

#execute_checkproperty(element, property, negate, value, isSpecialChar = false) ⇒ Object

Check object property



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
# File 'lib/anhpham/methods/core.rb', line 196

def execute_checkproperty element, property, negate, value, isSpecialChar=false
  Capybara.configure do |config|
    config.ignore_hidden_elements = false
  end
  foundElement = find_object(element)

  check = false
  if foundElement == nil and value == ""
    check = true
    # check.should eq true
    expect(check).to eq true
  else
    put_log "\n\n\t>>> execute_checkproperty: finish to found element"
    if foundElement != nil
      if property.upcase == 'VALUE'
        actual_value = foundElement.value()

      elsif property.upcase == 'TEXT'
        actual_value = foundElement.text()

      elsif property.upcase == 'SPECIAL CHAR'
        actual_value = foundElement.text()
        isSpecialChar = true

      elsif property.upcase == 'TAGNAME'
        actual_value = foundElement.tag_name()

      elsif property.upcase == 'STYLE'
        actual_value = foundElement[:style]

      elsif property.upcase == 'DISABLED'
        actual_value = foundElement[:disabled]

      elsif property.upcase == 'WIDTH'
        actual_value = foundElement[:width]

      elsif property.upcase == 'HEIGHT'
        actual_value = foundElement[:height]

      elsif property.upcase == 'ID'
        actual_value = foundElement[:id]

      elsif property.upcase == 'NAME'
        actual_value = foundElement[:name]

      elsif property.upcase == 'CLASS'
        actual_value = foundElement[:class]

      elsif property.upcase == 'HREF'
        actual_value = foundElement[:href]

      elsif property.upcase == 'TITLE'
        actual_value = foundElement[:title]

      elsif property.upcase == 'TYPE'
        actual_value = foundElement[:type]

      elsif property.upcase == 'CHECKED'
        actual_value = "true" if foundElement.checked? == true
        actual_value = "false" if foundElement.checked? == false
      else
        actual_value = foundElement["#{property}"]
      end

      if actual_value == nil
        actual_value = ''
      end
    else
      put_log "\nError >> Not found object: #{element}"
    end

    Capybara.configure do |config|
      config.ignore_hidden_elements = true
    end

    if IFD_Assertion.reg_compare(actual_value, value, isSpecialChar)
      check = true
    end

    put_log "\n#{property} :: Actual result is '#{actual_value}' -- Expected result is '#{value}'"

    if negate == " not"
      # check.should_not eq true
      expect(check).not_to eql true
    elsif
      # check.should eq true
    expect(check).to eq true
    end
  end
end

#execute_click(element) ⇒ Object

Click on element



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/anhpham/methods/web_methods.rb', line 62

def execute_click element
  foundElement = find_object(element)
  if foundElement != nil
    page.driver.execute_script('window.focus();')
    startTime = Time.new.to_i
    begin
      sleep(0.5)
      currentTime = Time.new.to_i
    end while (foundElement.native.enabled? == false and (currentTime - startTime) < $_CFWEB['Wait Time'])
    page.driver.browser.execute_script("arguments[0].scrollIntoView(true);", foundElement.native)
    page.driver.browser.mouse.click(foundElement.native)
  else
    put_log "\nError >> Not found object: #{element}"
  end
end

#execute_click_to_upload(object, file_name) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/anhpham/methods/web_methods.rb', line 142

def execute_click_to_upload object, file_name
  upload_file = ($test_data_dir + file_name).gsub!(/\//, "\\")
  foundElement = find_object(object)
  if foundElement != nil
    call_step("I click on \"#{object}\"")
    window = RAutomation::Window.new(:title => /File Upload/)
    window.text_field(:class => "Edit").set(upload_file)
    window.button(:value => "&Open").click
  else
    raise "Error >> Object #{object} not found.!"
  end
end

#execute_drag_to_new_object(from_element, element) ⇒ Object



124
125
126
127
128
129
130
# File 'lib/anhpham/methods/web_methods.rb', line 124

def execute_drag_to_new_object from_element, element
  found_from_element = find_object(from_element)
  foundElement = find_object(element)
  if foundElement != nil and found_from_element != nil
    found_from_element.drag_to(foundElement)
  end
end

#execute_gettext(element) ⇒ Object

Get text from web element



288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/anhpham/methods/core.rb', line 288

def execute_gettext element
  foundElement = find_object(element)
  if foundElement != nil
    tagname = foundElement.tag_name()

    if tagname.upcase == 'SELECT'
      #@text = page.evaluate_script("$(\"##{foundElement[:id]}\").text()")
    else
      actual_text = foundElement.text()
      if (actual_text == nil or actual_text.length == 0) and (tagname.upcase == 'INPUT' or tagname.upcase == 'TEXTAREA')
        actual_text = foundElement.value()
      end
    end

    put_log "\nText is '" + actual_text + "' of object '" + element + "'"
    $context_value = actual_text
  else
    put_log "\nError >> Not found object: #{element}"
    exit
  end
end

#execute_hover_mouse_on(element) ⇒ Object



132
133
134
135
136
137
138
139
140
# File 'lib/anhpham/methods/web_methods.rb', line 132

def execute_hover_mouse_on element
  foundElement = find_object(element)
  if foundElement != nil
    page.driver.browser.mouse.move_to(foundElement.native, element)
    sleep 1
  else
    put_log "\nError >> Not found object: #{element}"
  end
end

#execute_mousehoverandclick(element) ⇒ Object

Move mouse to element then click



113
114
115
116
117
118
119
120
121
122
# File 'lib/anhpham/methods/web_methods.rb', line 113

def execute_mousehoverandclick element
  foundElement = find_object(element)
  if foundElement != nil
    page.driver.browser.mouse.move_to(foundElement.native, element)
    page.driver.browser.mouse.click(foundElement.native)
    sleep(2)
  else
    put_log "\nError >> Not found object: #{element}"
  end
end

#execute_openbrowser(url_site) ⇒ Object

Open Browser with config-able maximize browser option



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/anhpham/methods/web_methods.rb', line 45

def execute_openbrowser url_site #, redirect
  begin
    if $_CFWEB['Maximize Browser'] == true
      page.driver.browser.manage.window.maximize
    end
  rescue StandardError => myStandardError
    put_log "\n>>> Error: #{myStandardError}"
  end

  if url_site == ""
    visit("")
  else
    visit(url_site)
  end
end

#execute_sendkeys(element, text) ⇒ Object

Send Key



178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/anhpham/methods/web_methods.rb', line 178

def execute_sendkeys element, text
  foundElement = find_object(element)
  if foundElement != nil
    if foundElement[:disabled] == ''
      if text == ":down"
        foundElement.native.send_keys([:down])
      elsif text == ":enter"
        foundElement.native.send_keys([:enter])
      elsif text == ":tab"
        foundElement.native.send_keys([:tab])
      else
        foundElement.native.send_keys(text)
      end
    end
  else
    put_log "\nError >> Not found object: #{element}"
    exit
  end
end

#execute_setstate(element, state) ⇒ Object

Set option state with state value True/False



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/anhpham/methods/web_methods.rb', line 79

def execute_setstate element, state
  foundElement = find_object(element)
  if foundElement != nil
    if state.upcase == "TRUE"
      foundElement.set(true)
    else
      foundElement.set(false)
    end
  else
    put_log "\nError >> Not found object: #{element}"
    exit
  end
end

#execute_settext(element, text) ⇒ Object

Enter text to element



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/anhpham/methods/web_methods.rb', line 94

def execute_settext element, text
  foundElement = find_object(element)
  if foundElement != nil
    begin
      foundElement.set(text)
        #page.driver.browser.mouse.click(foundElement.native)
        #foundElement.native.send_keys(text)
        # put_log "\nfoundElement.value: #{foundElement.value}"
        #foundElement.native.send_keys([:tab])
    rescue StandardError => myStandardError
      put_log "\n>>> Error: #{myStandardError}"
    end
  else
    put_log "\nError >> Not found object: #{element}"
    exit
  end
end

#find_object(string_object) ⇒ Object

Find object by Xpath



15
16
17
18
19
20
21
22
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/anhpham/methods/core.rb', line 15

def find_object string_object
  startTime = Time.new.to_i
  string_object = string_object.gsub(/"/, "'")
  # puts "String_object: #{string_object}"
  locator_matching = /(.*?)(\{.*?\})/.match(string_object)
  # puts "locator_matching : #{locator_matching}"
  dyn_pros = {}
  if locator_matching != nil
    string_object = locator_matching[1]
    eval(locator_matching[2].gsub(/['][\s,\t]*?:[\s,\t]*?[']?/, "'=>'")).each { |k, v|
      dyn_pros[k.to_s.upcase] = v
    }
  end

  # put_log "\n---dyn_pros: #{dyn_pros}"
  hash_object = $_RP_OBJECT[string_object]
  if hash_object == nil
    put_log ">>> OBJECT NAME MAYBE NOT FOUND!!!"
    # true.should eq false
    expect(true).to eq(false)
  end
  upcase_attrb = {}
  if hash_object != nil
    hash_object.each { |k, v|
      k = k.to_s.upcase
      # put_log "\n#{k} =~ /ph_/i and dyn_pros[#{k}]: #{k =~ /ph_/i and dyn_pros[k] != nil}"
      if k =~ /ph_/i
        if dyn_pros[k] != nil
          # Assign place holder value to place holder property, also remove prefix ph_ from property key,
          # also remove this pl from dyn_pros <= should be consider to continue transfer into inner object in relation
          if v =~ /<ph_value>/i
            upcase_attrb[k[3..k.size-1]] = v.gsub(/<ph_value>/i, dyn_pros[k])
          else
            upcase_attrb[k[3..k.size-1]] = dyn_pros[k]
          end

          dyn_pros.delete(k)
        end
      else
        upcase_attrb[k.to_s.upcase] = v
      end

    }
  end
  if upcase_attrb.size > 0
    strId = ""
    strClass = ""
    strName = ""
    strTagname = ""
    strXpathSelector = ""
    strText = ""

    index = nil
    upcase_attrb.each { |key, value|
      upcase_key = key.to_s.upcase
      if upcase_key == "XPATH_SELECTOR"
        strXpathSelector = value
      end
    }
    continue_run = true;

    if continue_run == true
      begin
        if strXpathSelector != nil and strXpathSelector.length > 0
          foundElements = get_objects_by_XpathSelector(strXpathSelector)
        else
          #Generate Selector
          strGenerateXpathSel = ""
          strGenerateXpathSel = generate_xpath_selector(strId, strClass, strName, strTagname)
          if strGenerateXpathSel.length > 0
            foundElements = get_objects_by_XpathSelector(strGenerateXpathSel)
          end
        end

        if foundElements == nil or foundElements.length == 0
          currentTime = Time.new.to_i
        else
          # put_log "\nBREAK!!!"
          break
        end
        test = currentTime - startTime
        # put_log "\n#TIMEOUNT:#{test} < #{$_CFWEB['Wait Time']}"
        sleep(0.5)
      end while (currentTime - startTime) < $_CFWEB['Wait Time']

      if foundElements != nil or foundElements.length != 0
        if index != nil and index.to_i >= 0
          matched_index = 0;
          return_element = nil
          foundElements.each { |cur_element|
            passCheck = find_object_check_object(cur_element, strId, strText)
            if passCheck
              if matched_index == index.to_i
                return_element = cur_element
                break
              else
                matched_index = matched_index + 1
              end
            end
          }
          return return_element
        else
          return_element = nil
          foundElements.each { |cur_element|
            passCheck = find_object_check_object(cur_element, strId, strText)
            if passCheck
              return_element = cur_element
              break
            end
          }
          return return_element
        end # if index != nil and index.to_i >= 0
      end #if foundElements != nil or foundElements.length != 0
    end #if continue = true
  end

  return nil
end

#find_object_check_object(cur_element, strId, strText) ⇒ Object

Find/Check the object by ID and Text



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/anhpham/methods/core.rb', line 172

def find_object_check_object cur_element, strId, strText
  passCheck = true
  if cur_element != nil

    # Check ID
    if strId != nil and strId.length > 0
      if strId =~ /^#/
        strId = strId[1..-1]
      end
    end

    # Check Text
    if passCheck and strText != nil and strText.length > 0
      if (strText =~ /^#/)
        strText = strText[1..-1]
      end
    end
    return passCheck
  end

  return false
end

#generate_xpath_selector(strId, strClass, strName, strTagname) ⇒ Object

Generate Xpath Selector



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/anhpham/methods/core.rb', line 135

def generate_xpath_selector(strId, strClass, strName, strTagname)
  strGenerateXpathSel = ""
  if strId != nil and strId.length > 0 and (strId =~ /^#/) == nil
    strGenerateXpathSel = "[@id='#{strId}']"
  end
  if strClass != nil and strClass.length > 0 and (strClass =~ /^#/) == nil
    strGenerateXpathSel = "#{strGenerateXpathSel}[@class='#{strClass}']"
  end
  if strName != nil and strName.length > 0 and (strName =~ /^#/) == nil
    strGenerateXpathSel = "#{strGenerateXpathSel}[@name='#{strName}']"
  end

  if strGenerateXpathSel.length > 0
    if strTagname != nil and strTagname.length > 0
      strGenerateXpathSel = "//#{strTagname}#{strGenerateXpathSel}"
    else
      strGenerateXpathSel = "//*#{strGenerateXpathSel}"
    end
  end

  return strGenerateXpathSel
end

#get_alert_textObject

method to get javascript pop-up alert text



37
38
39
# File 'lib/anhpham/methods/javascript_handling_methods.rb', line 37

def get_alert_text
  page.driver.browser.switch_to.alert.text
end

#get_browser_backObject



24
25
26
# File 'lib/anhpham/methods/javascript_handling_methods.rb', line 24

def get_browser_back
  page.evaluate_script('window.history.back()')
end

#get_element_attribute(access_type, access_name, attribute_name) ⇒ Object

method to get attribute value param 1 : String : Locator type (id, name, class, xpath, css) param 2 : String : Expected element text param 3 : String : atrribute name



328
329
330
# File 'lib/anhpham/methods/core.rb', line 328

def get_element_attribute(access_type, access_name, attribute_name)
  $driver.find_element(:"#{access_type}" => "#{access_name}").attribute("#{attribute_name}")
end

#get_objects_by_XpathSelector(strXpathSelector) ⇒ Object

Get object by XpathSelector



159
160
161
162
163
164
165
166
167
168
169
# File 'lib/anhpham/methods/core.rb', line 159

def get_objects_by_XpathSelector(strXpathSelector)
  # put_log "\nstrXpathSelector: #{strXpathSelector}"
  foundElements = nil
  begin
    foundElements = page.all(:xpath, strXpathSelector)
  rescue StandardError => myStandardError
    put_log "\n>>> Error: #{myStandardError}"
  end

  return foundElements
end

#get_page_titleObject

Method to return page title



7
8
9
# File 'lib/anhpham/methods/web_methods.rb', line 7

def get_page_title
  page.title
end

#getTestData(testdataid) ⇒ Object

Get data from the ‘testdata file’. if not valid, then testdataid is passed back



333
334
335
336
337
338
339
340
341
342
# File 'lib/anhpham/methods/core.rb', line 333

def getTestData(testdataid)
  begin
    sTestDataidArr = testdataid.downcase.split('.')
    testdata = $testdata[sTestDataidArr.first][sTestDataidArr.last]
    puts (testdata)
    return testdata
  rescue Exception
    return testdataid
  end
end

#handle_alert(decision) ⇒ Object



3
4
5
6
7
8
9
# File 'lib/anhpham/methods/javascript_handling_methods.rb', line 3

def handle_alert(decision)
  if decision == 'accept'
    page.driver.browser.switch_to.alert.accept
  else
   	page.driver.browser.switch_to.alert.dismiss
  end
end

#is_checkbox_checked(element, should_be_checked = true) ⇒ Object

method to assert checkbox check/uncheck param 1 : String : Locator value param 2 : Boolean : test case [true or flase]



234
235
236
237
238
239
240
241
242
243
# File 'lib/anhpham/methods/web_methods.rb', line 234

def is_checkbox_checked(element, should_be_checked = true)
  foundElement = find_object(element)
  checkbox = expect(foundElement).to be_checked

  if !checkbox && should_be_checked
    raise TestCaseFailed, 'Checkbox is not checked'
  elsif checkbox && !should_be_checked
    raise TestCaseFailed, 'Checkbox is checked'
  end
end

#is_element_enabled(element) ⇒ Object

Method to return element status - enabled? param 1 : String : Locator type (id, name, class, xpath, css) param 2 : String : Locator value



212
213
214
215
# File 'lib/anhpham/methods/web_methods.rb', line 212

def is_element_enabled(element)
  foundElement = find_object(element)
  expect(foundElement).to be_visible
end

#is_radio_button_selected(element, should_be_selected = true) ⇒ Object

method to assert radio button selected/unselected param 1 : String : Locator value param 2 : Boolean : test case [true or flase]



248
249
250
251
252
253
254
255
256
257
# File 'lib/anhpham/methods/web_methods.rb', line 248

def is_radio_button_selected(element, should_be_selected = true)
  foundElement = find_object(element)
  radio = expect(foundElement).to be_checked

  if !radio && should_be_selected
    raise TestCaseFailed, 'Radio Button not selected'
  elsif radio && !should_be_selected
    raise TestCaseFailed, 'Radio Button is selected'
  end
end

#maximize_browserObject

Method to maximize browser



265
266
267
# File 'lib/anhpham/methods/web_methods.rb', line 265

def maximize_browser
  page.driver.browser.manage.window.maximize
end

#open_new_windows_browserObject



32
33
34
# File 'lib/anhpham/methods/javascript_handling_methods.rb', line 32

def open_new_windows_browser
  page.driver.execute_script("window.open()")
end

print error for android



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/anhpham/methods/error_handling_methods.rb', line 29

def print_error_android(browser_type, app_path)
  if browser_type=='ff' and app_path==nil
    puts "\nOops... not mentioned \"Browser\" or \"App path\""
    print_error_android_app
    print_error_android_web
    Process.exit(0) 
  elsif browser_type!='ff' and !%w(native chrome).include? browser_type
    puts "\nOops... not supported browser"
    print_error_android_web
    Process.exit(0) 
  end
end

print error for android app



43
44
45
46
# File 'lib/anhpham/methods/error_handling_methods.rb', line 43

def print_error_android_app
  puts "\nTo run test on android app"
  puts "\n  Usage : cucumber PLATFORM=android APP_PATH=path/to/app"
end

print error for android web



49
50
51
52
53
# File 'lib/anhpham/methods/error_handling_methods.rb', line 49

def print_error_android_web
  puts "\nTo run test on android mobile web"
  puts "\n  Usage : cucumber PLATFORM=android BROWSER=browser_name"
  puts "\n  Supported browsers are \"chrome\" and \"native\""
end

print error for desktop



20
21
22
23
24
25
26
# File 'lib/anhpham/methods/error_handling_methods.rb', line 20

def print_error_desktop
  puts "\nInappropraite desktop browser : \"#{ENV['BROWSER']}\""
  puts "\nUsage : cucumber BROWSER=browser_name"
  puts "\nBrowser Supported  :\n"
  puts "\n1.ie\n2.chrome\n3.ff\n4.safari\n5.opera"
  Process.exit(0) 
end

print error for ios



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/anhpham/methods/error_handling_methods.rb', line 56

def print_error_ios
  if browser_type=='ff' and app_path==nil
    puts "\nOops... not mentioned \"Browser\" or \"App path\""
    print_error_ios_app
    print_error_ios_web
    Process.exit(0) 
  elsif browser_type!='safari'
    puts "\nOops... not supported browser"
    print_error_ios_app_web
    Process.exit(0) 
  end
end

print error for ios app



70
71
72
73
# File 'lib/anhpham/methods/error_handling_methods.rb', line 70

def print_error_ios_app
  puts "\nTo run test on iOS App"
  puts "\n  Usage : cucumber PLATFORM=iOS APP_PATH=path/to/app"
end

print error for ios web



76
77
78
79
# File 'lib/anhpham/methods/error_handling_methods.rb', line 76

def print_error_ios_web
  puts "\nTo run test on iOS mobile web"
  puts "\n  Usage : cucumber PLATFORM=iOS BROWSER=safari"
end

print error if invalid platform



82
83
84
85
86
87
# File 'lib/anhpham/methods/error_handling_methods.rb', line 82

def print_invalid_platform
  puts "\nOops... Invalid Platform"
  puts "\nSupported platform are \"android\" and \"iOS\"."
  puts "\nTo run on Desktop no need to mention platform."
  Process.exit(0) 
end

#put_log(str) ⇒ Object

Print script log to console



4
5
6
# File 'lib/anhpham/methods/core.rb', line 4

def put_log str
  puts str if $_CFWEB['Print Log'] == true
end

#read_file(filename) ⇒ Object



310
311
312
313
314
315
316
317
# File 'lib/anhpham/methods/core.rb', line 310

def read_file(filename)
  data = ''
  f = File.open(filename, "r")
  f.each_line do |line|
    data += line
  end
  return data.strip;
end

#refresh_pageObject



28
29
30
# File 'lib/anhpham/methods/javascript_handling_methods.rb', line 28

def refresh_page
  visit page.driver.browser.current_url
end

#replace_day(str) ⇒ Object



37
38
39
40
41
42
43
44
45
46
# File 'lib/anhpham/methods/lib_var.rb', line 37

def replace_day str
  t = Time.now()
  _day = t.day
  if _day < 10
    return str.gsub("{day}", "0#{_day.to_s}")
  else
    return str.gsub("{day}", "#{_day.to_s}")
  end
  return str
end

#replace_month(str) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/anhpham/methods/lib_var.rb', line 26

def replace_month str
  t = Time.now()
  _month = t.month
  if _month < 10
    return str.gsub("{month}", "0#{_month.to_s}")
  else
    return str.gsub("{month}", "#{_month.to_s}")
  end
  return str
end

#replace_pipe(str_pipe) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/anhpham/methods/lib_var.rb', line 48

def replace_pipe str_pipe
  put_log ">>>> #{str_pipe}"
  if str_pipe =~ /^.*{pipe}.*$/
    return str_pipe.gsub("{pipe}", "|")
  end
  return str_pipe
end

#replace_year(str) ⇒ Object



21
22
23
24
# File 'lib/anhpham/methods/lib_var.rb', line 21

def replace_year str
  t = Time.now()
  return str.gsub("{year}", t.year.to_s)
end

#resize_browser(width, heigth) ⇒ Object

Method to resize browser



284
285
286
# File 'lib/anhpham/methods/web_methods.rb', line 284

def resize_browser(width, heigth)
  page.driver.browser.manage.window.resize_to(width, heigth)
end

#scroll_page(to) ⇒ Object

method to scroll page to top or end



199
200
201
202
203
204
205
206
207
# File 'lib/anhpham/methods/web_methods.rb', line 199

def scroll_page(to)
  if to == 'end'
    page.driver.execute_script('window.scrollTo(0,Math.max(document.documentElement.scrollHeight,document.body.scrollHeight,document.documentElement.clientHeight));')
  elsif to == 'top'
    page.driver.execute_script('window.scrollTo(Math.max(document.documentElement.scrollHeight,document.body.scrollHeight,document.documentElement.clientHeight),0);')
  else
    raise "Exception : Invalid Direction (only scroll \"top\" or \"end\")"
  end
end

#set_time_out(timeout) ⇒ Object



319
320
321
322
# File 'lib/anhpham/methods/core.rb', line 319

def set_time_out(timeout)
  $_CFWEB['Wait Time'] = timeout
  Capybara.default_wait_time = $_CFWEB['Wait Time']
end

#switch_to_window_by_title(window_title) ⇒ Object

Method to switch to window by title



270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/anhpham/methods/web_methods.rb', line 270

def switch_to_window_by_title window_title
  $previous_window = page.driver.browser.window_handle
  window_found = false
  page.driver.browser.window_handles.each{ |handle|
    page.driver.browser.switch_to.window handle
    if page.title == window_title
      window_found = true
      break
    end
  }
  raise "Window having title \"#{window_title}\" not found" if not window_found
end

#switch_to_windows(decision) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/anhpham/methods/javascript_handling_methods.rb', line 11

def switch_to_windows(decision)
  if decision == 'last'
    page.driver.browser.switch_to.window(page.driver.browser.window_handles.last)
  else
    page.driver.browser.switch_to.window(page.driver.browser.window_handles.first)
  end
end

#take_screenshotObject



288
289
290
291
# File 'lib/anhpham/methods/web_methods.rb', line 288

def take_screenshot
  cur_time = Time.now.strftime('%Y%m%d%H%M%S%L')
  page.driver.browser.save_screenshot($test_data_dir + '/screenshots/screenshot' + cur_time + '.png')
end

#validate_parameters(platform, browser_type, app_path) ⇒ Object

Method to check browser type



4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/anhpham/methods/error_handling_methods.rb', line 4

def validate_parameters(platform, browser_type, app_path)
  if platform == 'desktop'
    if !%w(ff ie chrome safari opera).include? browser_type
      print_error_desktop
    end
  elsif platform == 'android'
      print_error_android browser_type, app_path
  elsif platform == 'iOS'
    puts "Not Implemented..."
    # print_error_ios browser_type, app_path
  else
    print_invalid_platform
  end
end

#var_collect(string_var) ⇒ Object



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/anhpham/methods/lib_var.rb', line 1

def var_collect string_var
  if string_var =~ /^.*{year}.*$/
    string_var = replace_year(string_var)
  end

  if string_var =~ /^.*{month}.*$/
    string_var = replace_month(string_var)
  end

  if string_var =~ /^.*{day}.*$/
    string_var = replace_day(string_var)
  end

  if string_var =~ /^.*{store_value}.*$/
    string_var = $context_value
  end

  return string_var
end