class Object
Constants
- WAIT
WAIT
instance for explicit wait
Public Instance Methods
method to check javascript pop-up alert text
# File lib/selenium-cucumber/methods/assertion_methods.rb, line 174 def check_alert_text(text) expect(get_alert_text).to eq text end
method to check checkbox
# File lib/selenium-cucumber/methods/input_methods.rb, line 35 def check_checkbox(access_type, access_name) checkbox = $driver.find_element(:"#{access_type}" => "#{access_name}") checkbox.click unless checkbox.selected? end
method to check attribute value param 1 : String : Locator type (id, name, class, xpath, css) param 2 : String : atrribute name param 3 : String : atrribute value param 4 : String : Locator value param 5 : Boolean : test case [true or flase]
# File lib/selenium-cucumber/methods/assertion_methods.rb, line 107 def check_element_attribute(access_type, attribute_name, attribute_value, access_name, test_case) attr_val = get_element_attribute(access_type, access_name, attribute_name) if test_case expect(attr_val).to eq(attribute_value) else expect(attr_val).to_not eq(attribute_value) end end
Element enabled checking param 1 : String : Locator type (id, name, class, xpath, css) param 2 : String : Expected element text param 4 : Boolean : test case [true or flase]
# File lib/selenium-cucumber/methods/assertion_methods.rb, line 84 def check_element_enable(access_type, access_name, test_case) result = is_element_enabled(access_type, access_name) if test_case expect(result).to be true else expect(result).to be false end end
Method to check partial element text param 1 : String : Locator type (id, name, class, xpath, css) param 2 : String : Expected element partial text param 3 : String : Locator value param 4 : Boolean : test case [true or flase]
# File lib/selenium-cucumber/methods/assertion_methods.rb, line 63 def check_element_partial_text(access_type, expected_value, access_name, test_case) element_text = get_element_text(access_type, access_name) if test_case expect(element_text).to include(expected_value) else expect(element_text).to_not include(expected_value) end end
method to check element presence param 1 : String : Locator type (id, name, class, xpath, css) param 2 : String : Locator value param 3 : Boolean : test case [true or flase]
# File lib/selenium-cucumber/methods/assertion_methods.rb, line 134 def check_element_presence(access_type, access_name, test_case) expect(is_element_displayed(access_type, access_name)).to be test_case end
Method to check element text param 1 : String : Locator type (id, name, class, xpath, css) param 2 : String : Expected element text param 3 : String : Locator value param 4 : Boolean : test case [true or flase]
# File lib/selenium-cucumber/methods/assertion_methods.rb, line 49 def check_element_text(access_type, expected_value, access_name, test_case) element_text = get_element_text(access_type, access_name) if test_case expect(element_text).to eq expected_value else expect(element_text).to_not eq expected_value end end
Method to verify partial title param 1 : String : partial title string param 2 : Boolean : test case [true or flase]
# File lib/selenium-cucumber/methods/assertion_methods.rb, line 27 def check_partial_title(partial_text_title, test_case) page_title = get_page_title if test_case expect(page_title).to include(partial_text_title) else expect(page_title).to_not include(partial_text_title) end end
Method to verify title param 1 : String : expected title param 2 : Boolean : test case [true or flase]
# File lib/selenium-cucumber/methods/assertion_methods.rb, line 14 def check_title(title, test_case) page_title = get_page_title if test_case expect(page_title).to eq title else expect(page_title).to_not eq title end end
method to clear text from textfield
# File lib/selenium-cucumber/methods/input_methods.rb, line 9 def clear_text(access_type, access_name) $driver.find_element(:"#{access_type}" => "#{access_name}").clear end
# File lib/selenium-cucumber/methods/click_elements_methods.rb, line 3 def click(access_type, access_name) $driver.find_element(:"#{access_type}" => "#{access_name}").click end
# File lib/selenium-cucumber/methods/click_elements_methods.rb, line 7 def click_by_text(access_type, access_name, text) element_found = false text_found = false text_found = $driver.page_source.include? text raise "Text '#{text}' was not found in the page source" unless text_found # enter loop if the text is found if text_found elements = $driver.find_elements(:"#{access_type}" => "#{access_name}") elements.each do |element| if element.text == text element_found = true element.click break end end end raise "Element not found having access type #{access_type}, access name #{access_name}, and text #{text}" unless element_found end
# File lib/selenium-cucumber/methods/click_elements_methods.rb, line 26 def click_forcefully(access_type, access_name) $driver.execute_script('arguments[0].click();', $driver.find_element(:"#{access_type}" => "#{access_name}")) end
# File lib/selenium-cucumber/methods/mobile_methods.rb, line 240 def close_app $driver.close_app end
method to quite webdriver instance
# File lib/selenium-cucumber/methods/navigate_methods.rb, line 18 def close_driver $driver.quit end
Method to close new window
# File lib/selenium-cucumber/methods/navigate_methods.rb, line 151 def close_new_window $driver.close end
Method to compare two images param 1 : String : Locator type (id, name, class, xpath, css, url) param 2 : String : Locator value param 3 : String : Locator type (id, name, class, xpath, css, url, image_name) param 4 : String : Locator value
# File lib/selenium-cucumber/methods/assertion_methods.rb, line 202 def compare_image(actual_img_access_type, actual_img_access_name, excp_img_access_type, excp_img_access_name) if actual_img_access_type == 'url' actual_img_url = actual_img_access_name else actual_img_url = get_element_attribute(actual_img_access_type, actual_img_access_name, 'src') end if excp_img_access_type == 'url' expected_img_url = excp_img_access_name elsif excp_img_access_type == 'image_name' expected_img_url = './features/expected_images/' + excp_img_access_name else expected_img_url = get_element_attribute(excp_img_access_type, excp_img_access_name, 'src') end # replace 'https' with 'http' from actual image url if actual_img_url.include? 'https' actual_img_url['https'] = 'http' end # replace 'https' with 'http' from expected image url if expected_img_url.include? 'https' expected_img_url['https'] = 'http' end if expected_img_url.include? '.png' image_type = 'png' else image_type = 'jpg' end # Storing actual image locally open('./features/actual_images/actual_image.' + image_type, 'wb') do |file| file << open(actual_img_url).read end actual_img_url = './features/actual_images/actual_image.' + image_type # Storing Expected image locally if excp_img_access_type != 'image_name' open('./features/expected_images/expected_image.' + image_type, 'wb') do |file| file << open(expected_img_url).read end expected_img_url = './features/expected_images/expected_image.' + image_type end # Verify image extension and call respective compare function if image_type == 'png' return compare_png_images(expected_img_url, actual_img_url) end compare_jpeg_images(expected_img_url, actual_img_url) end
Comparing jpg images
# File lib/selenium-cucumber/methods/assertion_methods.rb, line 257 def compare_jpeg_images(expected_img_url, actual_img_url) if open(expected_img_url).read == open(actual_img_url).read return true else puts 'Difference in images' return false end end
Comparing png images
# File lib/selenium-cucumber/methods/assertion_methods.rb, line 267 def compare_png_images(expected_img_url, actual_img_url) images = [ ChunkyPNG::Image.from_file(expected_img_url), ChunkyPNG::Image.from_file(actual_img_url) ] diff = [] images.first.height.times do |y| images.first.row(y).each_with_index do |pixel, x| diff << [x, y] unless pixel == images.last[x, y] end end if diff.length != 0 puts "\npixels (total): #{images.first.pixels.length}" puts "pixels changed: #{diff.length}" puts "pixels changed (%): #{(diff.length.to_f / images.first.pixels.length) * 100}%" x, y = diff.map { |xy| xy[0] }, diff.map { |xy| xy[1] } images.last.rect(x.min, y.min, x.max, y.max, ChunkyPNG::Color.rgb(0, 255, 0)) cur_time = Time.now.strftime('%Y%m%d%H%M%S%L') images.last.save("./features/image_difference/difference_#{cur_time}.png") puts "\nDifference between images saved as : difference_#{cur_time}.png\n" return false end true end
Method to find difference between images
# File lib/selenium-cucumber/methods/assertion_methods.rb, line 191 def does_images_similar?(actual_img_access_type, actual_img_access_name, excp_img_access_type, excp_img_access_name) if !compare_image(actual_img_access_type, actual_img_access_name, excp_img_access_type, excp_img_access_name) raise TestCaseFailed, 'Actual image is different from expected image' end end
# File lib/selenium-cucumber/methods/click_elements_methods.rb, line 30 def double_click(access_type, access_value) element = $driver.find_element(:"#{access_type}" => "#{access_value}") $driver.action.double_click(element).perform end
method to enter text into textfield
# File lib/selenium-cucumber/methods/input_methods.rb, line 4 def enter_text(access_type, text, access_name) $driver.find_element(:"#{access_type}" => "#{access_name}").send_keys text end
method to get javascript pop-up alert text
# File lib/selenium-cucumber/methods/assertion_methods.rb, line 169 def get_alert_text $driver.switch_to.alert.text end
Return android device name and android version using adb command
# File lib/selenium-cucumber/methods/misc_methods.rb, line 28 def get_device_info IO.popen('adb shell getprop ro.product.brand') { |f| $device = f.gets.chomp.upcase} $device += ' ' IO.popen('adb shell getprop ro.product.model') { |f| $device += f.gets.chomp.upcase} IO.popen('adb shell getprop ro.build.version.release') { |f| $os_version = f.gets.chomp.upcase} return $device, $os_version 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/selenium-cucumber/methods/assertion_methods.rb, line 97 def get_element_attribute(access_type, access_name, attribute_name) $driver.find_element(:"#{access_type}" => "#{access_name}").attribute("#{attribute_name}") end
Method to get element text param 1 : String : Locator type (id, name, class, xpath, css) param 2 : String : Locator value
# File lib/selenium-cucumber/methods/assertion_methods.rb, line 40 def get_element_text(access_type, access_name) $driver.find_element(:"#{access_type}" => "#{access_name}").text end
method to return key (control/command) by os wise
# File lib/selenium-cucumber/methods/navigate_methods.rb, line 23 def get_key os = $driver.capabilities.platform.to_s.upcase if os.to_s == 'WINDOWS' || os.to_s == 'LINUX' return 'control' elsif os.to_s == 'DARWIN' return 'command' else raise 'Invalid OS' end end
Method to return page title
# File lib/selenium-cucumber/methods/assertion_methods.rb, line 7 def get_page_title $driver.title end
# File lib/selenium-cucumber/methods/javascript_handling_methods.rb, line 3 def handle_alert(decesion) if decesion == 'accept' $driver.switch_to.alert.accept else $driver.switch_to.alert.dismiss end end
Method to hover on element
# File lib/selenium-cucumber/methods/navigate_methods.rb, line 83 def hover_over_element(access_type, access_name) element = $driver.find_element(:"#{access_type}" => "#{access_name}") $driver.action.move_to(element).perform end
method to assert checkbox check/uncheck param 1 : String : Locator type (id, name, class, xpath, css) param 2 : String : Locator value param 3 : Boolean : test case [true or flase]
# File lib/selenium-cucumber/methods/assertion_methods.rb, line 142 def is_checkbox_checked(access_type, access_name, should_be_checked = true) checkbox = $driver.find_element(:"#{access_type}" => "#{access_name}") expect(checkbox.selected?).to be should_be_checked end
method to get element status - displayed? param 1 : String : Locator type (id, name, class, xpath, css) param 2 : String : Locator value
# File lib/selenium-cucumber/methods/assertion_methods.rb, line 121 def is_element_displayed(access_type, access_name) begin $driver.find_element(:"#{access_type}" => "#{access_name}").displayed? rescue Selenium::WebDriver::Error::NoSuchElementError # elements not found return false false 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/selenium-cucumber/methods/assertion_methods.rb, line 76 def is_element_enabled(access_type, access_name) $driver.find_element(:"#{access_type}" => "#{access_name}").enabled? end
# File lib/selenium-cucumber/methods/assertion_methods.rb, line 178 def is_option_from_dropdown_selected(access_type, by, option, access_name, should_be_selected=true) dropdown = $driver.find_element(:"#{access_type}" => "#{access_name}") select_list = Selenium::WebDriver::Support::Select.new(dropdown) if by == 'text' actual_value = select_list.first_selected_option.text else actual_value = select_list.first_selected_option.attribute('value') end expect(actual_value).to eq option end
# File lib/selenium-cucumber/methods/mobile_methods.rb, line 227 def long_press_on_coordinates(x, y) action = Appium::TouchAction.new.press(x: "#{x}", y: "#{y}").wait(2000).move_to(x: "#{x}", y: "#{y}").release() action.perform end
# File lib/selenium-cucumber/methods/mobile_methods.rb, line 232 def long_press_on_coordinates_with_duration(x, y, duration) duration = duration.to_i duration = duration * 1000 puts duration action = Appium::TouchAction.new.press(x: "#{x}", y: "#{y}").wait("#{duration}").move_to(x: "#{x}", y: "#{y}").release() action.perform end
# File lib/selenium-cucumber/methods/mobile_methods.rb, line 191 def long_press_on_element_default_duration(access_type, access_name) begin ele_from = WAIT.until { $driver.find_element(:"#{access_type}" => "#{access_name}") }.location x = ele_from.x y = ele_from.y action = Appium::TouchAction.new.press(x: "#{x}", y: "#{y}").wait(2000).move_to(x: "#{x}", y: "#{y}").release() action.perform rescue Exception => e if e.to_s == 'The swipe did not complete successfully' print "" else raise e end end end
# File lib/selenium-cucumber/methods/mobile_methods.rb, line 208 def long_press_on_element_with_duration(access_type, access_name, duration) begin ele_from = WAIT.until { $driver.find_element(:"#{access_type}" => "#{access_name}") }.location x = ele_from.x y = ele_from.y duration = duration.to_i duration = duration * 1000 action = Appium::TouchAction.new.press(x: "#{x}", y: "#{y}").wait("#{duration}").move_to(x: "#{x}", y: "#{y}").release() action.perform rescue Exception => e if e.to_s == 'The swipe did not complete successfully' print "" else raise e end end end
Method to maximize browser
# File lib/selenium-cucumber/methods/navigate_methods.rb, line 78 def maximize_browser $driver.manage.window.maximize end
method to print configuration
# File lib/selenium-cucumber/methods/configuration_methods.rb, line 4 def print_congifugartion puts '' puts "Date : #{Time.now.strftime("%d-%B-%Y")}" puts "Time : #{Time.now.strftime("%I:%M:%S:%p")}" if $platform == 'android' or $platform == 'ios' print_mobile_configuration else print_desktop_configuration end end
method to print desktop configuration
# File lib/selenium-cucumber/methods/configuration_methods.rb, line 17 def print_desktop_configuration puts 'Platform : ' + $driver.capabilities.platform.to_s.upcase puts 'Browser : ' + $driver.capabilities.browser_name.to_s.upcase + " " + $driver.capabilities.version.to_s puts '' end
print error for android
# File lib/selenium-cucumber/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/selenium-cucumber/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/selenium-cucumber/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/selenium-cucumber/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/selenium-cucumber/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/selenium-cucumber/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/selenium-cucumber/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/selenium-cucumber/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
method to print mobile configuration
# File lib/selenium-cucumber/methods/configuration_methods.rb, line 24 def print_mobile_configuration puts "Platform : #{$platform.upcase}" puts "OS version : #{$os_version}" puts "Device : #{$device_name}" if $app_path.nil? puts 'Browser : ' + $driver.capabilities.browser_name.to_s.upcase + " " + $driver.capabilities.version.to_s else puts "App Tested : #{$app_path}" end puts '' end
Method to resize browser
# File lib/selenium-cucumber/methods/navigate_methods.rb, line 73 def resize_browser(width, heigth) $driver.manage.window.resize_to(width, heigth) end
method to scroll page to top or end
# File lib/selenium-cucumber/methods/navigate_methods.rb, line 95 def scroll_page(to) if to == 'end' $driver.execute_script('window.scrollTo(0,Math.max(document.documentElement.scrollHeight,document.body.scrollHeight,document.documentElement.clientHeight));') elsif to == 'top' $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
Method to scroll page to perticular element
# File lib/selenium-cucumber/methods/navigate_methods.rb, line 89 def scroll_to_element(access_type, access_name) ele_scroll = $driver.find_element(:"#{access_type}" => "#{access_name}") ele_scroll.location_once_scrolled_into_view end
method to select all option from dropdwon list
# File lib/selenium-cucumber/methods/input_methods.rb, line 21 def select_all_option_from_multiselect_dropdown(access_type, access_name) dropdown = $driver.find_element(:"#{access_type}" => "#{access_name}") select_list = Selenium::WebDriver::Support::Select.new(dropdown) select_list.select_all end
method to select option from dropdwon list
# File lib/selenium-cucumber/methods/input_methods.rb, line 14 def select_option_from_dropdown(access_type, by, option, access_name) dropdown = $driver.find_element(:"#{access_type}" => "#{access_name}") select_list = Selenium::WebDriver::Support::Select.new(dropdown) select_list.select_by(:"#{by}", "#{option}") end
# File lib/selenium-cucumber/methods/click_elements_methods.rb, line 35 def submit(access_type, access_name) $driver.find_element(:"#{access_type}" => "#{access_name}").submit end
# File lib/selenium-cucumber/methods/mobile_methods.rb, line 18 def swipe(start_x, start_y, end_x, end_y) action = Appium::TouchAction.new.press(x: "#{start_x}", y: "#{start_y}").wait(1000).move_to(x: "#{end_x}", y: "#{end_y}").release() action.perform end
# File lib/selenium-cucumber/methods/mobile_methods.rb, line 149 def swipe_coordinates_with_direction(start_x, start_y, direction) # ele_from = WAIT.until { $driver.find_element(:"#{access_type}" => "#{access_name}") }.size # height = size.height.to_i - 5 # puts "height : #{height}" # 1776 # width = size.width.to_i - 5 # puts "width :#{width}" # size = $driver.manage.window.size height = size.height.to_i - 5 puts "height : #{height}" width = size.width.to_i - 5 puts "width :#{width}" if direction == 'right' start_x = start_x start_y = start_y end_x = width end_y = start_y elsif direction == 'left' start_x = start_x start_y = start_y end_x = 10 end_y = start_y elsif direction == 'up' start_x = start_x start_y = start_y end_x = start_x end_y = 10 elsif direction == 'down' start_x = start_x start_y = start_y end_x = start_x end_y = height else raise "invalid direction" end swipe(start_x, start_y, end_x, end_y) end
# File lib/selenium-cucumber/methods/mobile_methods.rb, line 23 def swipe_direction(direction) size = $driver.manage.window.size height = size.height.to_i - 10 width = size.width.to_i - 10 if direction == 'right' start_x = (width/100) * 15 # 83 start_y = height/2 # 695 end_x = (width/100) * 90 # 900 end_y = height/2 # 630 elsif direction == 'left' start_x = (width/100) * 90 start_y = height/2 end_x = (width/100) * 15 end_y = height/2 elsif direction == 'up' start_x = width/2 start_y = (height/100) * 90 end_x = width/2 end_y = (height/100) * 15 elsif direction == 'down' start_x = width/2 start_y = (height/100) * 15 end_x = width/2 end_y = (height/100) * 90 else raise "invalid direction" end swipe(start_x, start_y, end_x, end_y) end
# File lib/selenium-cucumber/methods/mobile_methods.rb, line 55 def swipe_element_with_direction(access_type, access_name, direction) ele_from = WAIT.until { $driver.find_element(:"#{access_type}" => "#{access_name}") }.location x_start = ele_from.x y_start = ele_from.y ele_size = WAIT.until { $driver.find_element(:"#{access_type}" => "#{access_name}") } ele_height = ele_size.size.height.to_i ele_width = ele_size.size.width.to_i #puts ele_size.width x_end = x_start + ele_width y_end = y_start + ele_height puts "[#{x_start},#{y_start}],[#{x_end},#{y_end}]" puts "ele_height : #{ele_height}" # 1776 puts "ele_width :#{ele_width}" # size = $driver.manage.window.size height = size.height.to_i - 5 puts "height : #{height}" # 1776 width = size.width.to_i - 5 puts "width :#{width}" # if direction == 'right' start_x = x_start + (ele_width*0.25) start_y = (y_start) + (ele_height/2) end_x = width # 900 end_y = (y_start) + (ele_height/2) elsif direction == 'left' start_x = x_end - 5 start_y = (y_end) - (ele_height/2) end_x = 10 end_y = (y_end) - (ele_height/2) elsif direction == 'up' if ele_height > height ele_height = height end if y_end > height y_end = y_start + ele_height end puts "ele_height: #{ele_height}" puts "ele_height*).75: #{ele_height*0.75}" puts y_end start_x = x_end - (ele_width/2) start_y = (y_end) - (ele_height*0.75) if start_y < 0 y_start = 0 y_end = y_start + ele_height start_y = (y_end) - (ele_height*0.75) # puts y_end # puts "y_start: #{y_start}" # puts "ele_height: #{ele_height}" # puts "ele_height*0.75: #{ele_height*0.75}" end end_x = (x_end/2) end_y = 10 elsif direction == 'down' start_x = x_start + (ele_width/2) if ele_height > height ele_height = height end start_y = (y_start) + (ele_height*0.25) if start_y < 0 y_start = 0 if ele_height > height ele_height = height end start_y = (y_start) + (ele_height*0.25) # puts y_end # puts "y_start: #{y_start}" puts "ele_height: #{ele_height}" puts "ele_height*0.25: #{ele_height*0.25}" end end_x = x_start + (ele_width/2) if ele_height > height ele_height = height end end_y = ele_height else raise "invalid direction" end puts "start_x: #{start_x}, start_y: #{start_y}" puts "end_x: #{end_x}, end_y: #{end_y}" swipe(start_x, start_y, end_x, end_y) end
# File lib/selenium-cucumber/methods/mobile_methods.rb, line 3 def swipe_using_elements(access_type1, access_name1, access_type2, access_name2) # to get start x,y co-ordinates ele_from = WAIT.until { $driver.find_element(:"#{access_type1}" => "#{access_name1}") }.location start_x = ele_from.x start_y = ele_from.y # to get end x,y co-ordinates ele_to = WAIT.until { $driver.find_element(:"#{access_type2}" => "#{access_name2}") }.location end_x = ele_to.x end_y = ele_to.y swipe(start_x, start_y, end_x, end_y) end
method to switch frame
# File lib/selenium-cucumber/methods/navigate_methods.rb, line 156 def switch_frame frame $driver.switch_to.frame(frame) end
method to switch to main window
# File lib/selenium-cucumber/methods/navigate_methods.rb, line 161 def switch_to_main_content $driver.switch_to.default_content end
Method to switch to main window
# File lib/selenium-cucumber/methods/navigate_methods.rb, line 117 def switch_to_main_window $previous_window = $driver.window_handle $driver.switch_to.window($driver.window_handles.first) end
Method to switch to new window
# File lib/selenium-cucumber/methods/navigate_methods.rb, line 111 def switch_to_new_window $previous_window = $driver.window_handle $driver.switch_to.window($driver.window_handles.last) end
Method to switch to old window
# File lib/selenium-cucumber/methods/navigate_methods.rb, line 106 def switch_to_previous_window $driver.switch_to.window $previous_window end
Method to switch to window by title
# File lib/selenium-cucumber/methods/navigate_methods.rb, line 123 def switch_to_window_by_title window_title $previous_window = $driver.window_handle window_found = false $driver.window_handles.each{ |handle| $driver.switch_to.window handle if $driver.title == window_title window_found = true break end } raise "Window having title \"#{window_title}\" not found" if not window_found end
# File lib/selenium-cucumber/methods/navigate_methods.rb, line 136 def switch_to_window_by_url window_url $previous_window = $driver.window_handle window_found = false $driver.window_handles.each { |handle| $driver.switch_to.window handle # match absolute or relative if $driver.current_url.include?(window_url) window_found = true break end } raise "Window having url \"#{window_url}\" not found" if not window_found end
# File lib/selenium-cucumber/methods/screenshot_methods.rb, line 3 def take_screenshot cur_time = Time.now.strftime('%Y%m%d%H%M%S%L') $driver.save_screenshot('./features/screenshots/screenshot' + cur_time + '.png') end
method to select radio button
# File lib/selenium-cucumber/methods/input_methods.rb, line 50 def toggle_checkbox(access_type, access_name) $driver.find_element(:"#{access_type}" => "#{access_name}").click end
method to uncheck checkbox
# File lib/selenium-cucumber/methods/input_methods.rb, line 41 def uncheck_checkbox(access_type, access_name) checkbox = $driver.find_element(:"#{access_type}" => "#{access_name}") if checkbox.selected? checkbox.click end end
method to unselect all option from dropdwon list
# File lib/selenium-cucumber/methods/input_methods.rb, line 28 def unselect_all_option_from_multiselect_dropdown(access_type, access_name) dropdown = $driver.find_element(:"#{access_type}" => "#{access_name}") select_list = Selenium::WebDriver::Support::Select.new(dropdown) select_list.deselect_all end
method to validate locator
# File lib/selenium-cucumber/methods/misc_methods.rb, line 10 def valid_locator_type? type %w(id class css name xpath).include? type end
method to validate dropdown selector
# File lib/selenium-cucumber/methods/misc_methods.rb, line 19 def valid_option_by? option_by %w(text value index).include? option_by end
# File lib/selenium-cucumber/methods/misc_methods.rb, line 14 def validate_locator type raise "Invalid locator type - #{type}" unless valid_locator_type? type end
# File lib/selenium-cucumber/methods/misc_methods.rb, line 23 def validate_option_by option_by raise "Invalid option by - #{option_by}" unless valid_option_by? option_by end
Method to check browser type
# File lib/selenium-cucumber/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/selenium-cucumber/methods/progress_methods.rb, line 3 def wait(time) sleep time.to_i end
# File lib/selenium-cucumber/methods/progress_methods.rb, line 7 def wait_for_element_to_display(access_type, access_name, duration) wait = Selenium::WebDriver::Wait.new(:timeout => duration.to_i) # seconds wait.until { $driver.find_element(:"#{access_type}" => "#{access_name}").displayed? } end
# File lib/selenium-cucumber/methods/progress_methods.rb, line 12 def wait_for_element_to_enable(access_type, access_name, duration) wait = Selenium::WebDriver::Wait.new(:timeout => duration.to_i) # seconds wait.until { $driver.find_element(:"#{access_type}" => "#{access_name}").enabled? } end
Method to zoom in/out/reset page
# File lib/selenium-cucumber/methods/navigate_methods.rb, line 35 def zoom_in_out(operation) if $driver.capabilities.browser_name == 'chrome' actual_zoom = $driver.execute_script "return document.body.style.zoom" puts "actual_zoom:#{actual_zoom}" if actual_zoom == '' or actual_zoom == nil actual_zoom = 100 else actual_zoom = actual_zoom.delete '%' actual_zoom = actual_zoom.to_i end if operation == 'add' actual_zoom += 15 unless actual_zoom >= 500 elsif operation == 'subtract' actual_zoom -= 15 unless actual_zoom <= 25 else actual_zoom = 100 end puts "actual_zoom:#{actual_zoom}" $driver.execute_script "document.body.style.zoom='#{actual_zoom}%'" else html = $driver.find_element(:tag_name => "body") $driver.action.send_keys(html, :"#{get_key}", :"#{operation}" ).perform end end
Method to zoom in/out web page until web element displyas
# File lib/selenium-cucumber/methods/navigate_methods.rb, line 62 def zoom_in_out_till_element_display(access_type, operation, access_name) while true if WAIT.until { $driver.find_element(:"#{access_type}" => "#{access_name}") }.displayed? break else zoom_in_out(operation) end end end