class Object
Public Instance Methods
Call another step
# File lib/anhpham/methods/core.rb, line 9 def call_step str_step put_log "\n-=> call step: #{str_step}" step %{#{str_step}} end
method to check javascript pop-up alert text
# 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
Element enabled checking param 1 : String : Expected element text param 2 : Boolean : test case [true or flase]
# 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
Method to verify partial title param 1 : String : partial title string param 2 : Boolean : test case [true or flase]
# 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
Method to verify title param 1 : String : expected title param 2 : Boolean : test case [true or flase]
# 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
# 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
# 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
Check exists
# 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
Check object property
# 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
Click on element
# 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
# 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
# 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
Get text from web element
# 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
# 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
Move mouse to element then click
# 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
Open Browser with config-able maximize browser option
# 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
Send Key
# 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
Set option state with state value True/False
# 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
Enter text to element
# 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 by Xpath
# 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/Check the object by ID and Text
# 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
# 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 data from the ‘testdata file’. if not valid, then testdataid is passed back
# 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
method to get javascript pop-up alert text
# File lib/anhpham/methods/javascript_handling_methods.rb, line 37 def get_alert_text page.driver.browser.switch_to.alert.text end
# File lib/anhpham/methods/javascript_handling_methods.rb, line 24 def get_browser_back page.evaluate_script('window.history.back()') end
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
# 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 object by XpathSelector
# 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
Method to return page title
# File lib/anhpham/methods/web_methods.rb, line 7 def get_page_title page.title end
# 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
method to assert checkbox check/uncheck param 1 : String : Locator value param 2 : Boolean : test case [true or flase]
# 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
Method to return element status - enabled? param 1 : String : Locator type (id, name, class, xpath, css) param 2 : String : Locator value
# File lib/anhpham/methods/web_methods.rb, line 212 def is_element_enabled(element) foundElement = find_object(element) expect(foundElement).to be_visible end
Method to maximize browser
# File lib/anhpham/methods/web_methods.rb, line 265 def maximize_browser page.driver.browser.manage.window.maximize end
# 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
# 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
# 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
# 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
# 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
# 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
# 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
# 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
# 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
Print script log to console
# File lib/anhpham/methods/core.rb, line 4 def put_log str puts str if $_CFWEB['Print Log'] == true end
# 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
# File lib/anhpham/methods/javascript_handling_methods.rb, line 28 def refresh_page visit page.driver.browser.current_url end
# 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
# 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
# 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
# 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
Method to resize browser
# File lib/anhpham/methods/web_methods.rb, line 284 def resize_browser(width, heigth) page.driver.browser.manage.window.resize_to(width, heigth) end
method to scroll page to top or end
# 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
# 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
Method to switch to window by title
# 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
# 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
# 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
Method to check browser type
# 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
# 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