class Object

Constants

WAIT

WAIT instance for explicit wait

Public Instance Methods

check_alert_text(text) click to toggle source

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
check_checkbox(access_type, access_name) click to toggle source

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
check_element_attribute(access_type, attribute_name, attribute_value, access_name, test_case) click to toggle source

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
check_element_enable(access_type, access_name, test_case) click to toggle source

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
check_element_partial_text(access_type, expected_value, access_name, test_case) click to toggle source

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
check_element_presence(access_type, access_name, test_case) click to toggle source

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
check_element_text(access_type, expected_value, access_name, test_case) click to toggle source

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
check_partial_title(partial_text_title, test_case) click to toggle source

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
check_title(title, test_case) click to toggle source

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
clear_text(access_type, access_name) click to toggle source

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
click(access_type, access_name) click to toggle source
# 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
click_by_text(access_type, access_name, text) click to toggle source
# 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
click_forcefully(access_type, access_name) click to toggle source
# 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
close_app() click to toggle source
# File lib/selenium-cucumber/methods/mobile_methods.rb, line 240
def close_app
  $driver.close_app
end
close_driver() click to toggle source

method to quite webdriver instance

# File lib/selenium-cucumber/methods/navigate_methods.rb, line 18
def close_driver
  $driver.quit
end
close_new_window() click to toggle source

Method to close new window

# File lib/selenium-cucumber/methods/navigate_methods.rb, line 151
def close_new_window
  $driver.close
end
compare_image(actual_img_access_type, actual_img_access_name, excp_img_access_type, excp_img_access_name) click to toggle source

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
compare_jpeg_images(expected_img_url, actual_img_url) click to toggle source

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
compare_png_images(expected_img_url, actual_img_url) click to toggle source

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
does_images_similar?(actual_img_access_type, actual_img_access_name, excp_img_access_type, excp_img_access_name) click to toggle source

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
double_click(access_type, access_value) click to toggle source
# 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
enter_text(access_type, text, access_name) click to toggle source

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
get_alert_text() click to toggle source

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
get_device_info() click to toggle source

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
get_element_attribute(access_type, access_name, attribute_name) click to toggle source

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
get_element_text(access_type, access_name) click to toggle source

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
get_key() click to toggle source

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
get_page_title() click to toggle source

Method to return page title

# File lib/selenium-cucumber/methods/assertion_methods.rb, line 7
def get_page_title
  $driver.title
end
handle_alert(decesion) click to toggle source
# 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
hover_over_element(access_type, access_name) click to toggle source

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
is_checkbox_checked(access_type, access_name, should_be_checked = true) click to toggle source

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
is_element_displayed(access_type, access_name) click to toggle source

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
is_element_enabled(access_type, access_name) click to toggle source

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
is_option_from_dropdown_selected(access_type, by, option, access_name, should_be_selected=true) click to toggle source
# 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
is_option_from_radio_button_group_selected(access_type, by, option, access_name, should_be_selected = true) click to toggle source

method to assert option from radio button group is selected/unselected

# File lib/selenium-cucumber/methods/assertion_methods.rb, line 158
def is_option_from_radio_button_group_selected(access_type, by, option, access_name, should_be_selected = true)
  radio_button_group = $driver.find_elements(:"#{access_type}" => "#{access_name}")

  getter = ->(rb, by) { by == 'value' ? rb.attribute('value') : rb.text }

  ele = radio_button_group.find { |rb| getter.call(rb, by) == option }

  expect(ele.selected?).to be should_be_selected
end
is_radio_button_selected(access_type, access_name, should_be_selected = true) click to toggle source

method to assert radio button selected/unselected 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 152
def is_radio_button_selected(access_type, access_name, should_be_selected = true)
  radio_button = $driver.find_element(:"#{access_type}" => "#{access_name}")
  expect(radio_button.selected?).to be should_be_selected
end
long_press_on_coordinates(x, y) click to toggle source
# 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
long_press_on_coordinates_with_duration(x, y, duration) click to toggle source
# 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
long_press_on_element_default_duration(access_type, access_name) click to toggle source
# 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
long_press_on_element_with_duration(access_type, access_name, duration) click to toggle source
# 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
maximize_browser() click to toggle source

Method to maximize browser

# File lib/selenium-cucumber/methods/navigate_methods.rb, line 78
def maximize_browser
  $driver.manage.window.maximize
end
navigate(direction) click to toggle source

method to navigate back & forword

navigate_to(link) click to toggle source

method to open link

print_congifugartion() click to toggle source

method to print configuration

print_desktop_configuration() click to toggle source

method to print desktop configuration

print_error_android(browser_type, app_path) click to toggle source

print error for android

print_error_android_app() click to toggle source

print error for android app

print_error_android_web() click to toggle source

print error for android web

print_error_desktop() click to toggle source

print error for desktop

print_error_ios() click to toggle source

print error for ios

print_error_ios_app() click to toggle source

print error for ios app

print_error_ios_web() click to toggle source

print error for ios web

print_invalid_platform() click to toggle source

print error if invalid platform

print_mobile_configuration() click to toggle source

method to print mobile configuration

resize_browser(width, heigth) click to toggle source

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
scroll_page(to) click to toggle source

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
scroll_to_element(access_type, access_name) click to toggle source

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
select_all_option_from_multiselect_dropdown(access_type, access_name) click to toggle source

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
select_option_from_dropdown(access_type, by, option, access_name) click to toggle source

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
select_option_from_radio_button_group(access_type, by, option, access_name) click to toggle source

method to select option from radio button group

# File lib/selenium-cucumber/methods/input_methods.rb, line 61
def select_option_from_radio_button_group(access_type, by, option, access_name)
  radio_button_group = $driver.find_elements(:"#{access_type}" => "#{access_name}")
  
  getter = ->(rb, by) { by == 'value' ? rb.attribute('value') : rb.text }
  ele = radio_button_group.find { |rb| getter.call(rb, by) == option }
  ele.click unless ele.selected?
end
select_radio_button(access_type, access_name) click to toggle source

method to select radio button

# File lib/selenium-cucumber/methods/input_methods.rb, line 55
def select_radio_button(access_type, access_name)
  radio_button = $driver.find_element(:"#{access_type}" => "#{access_name}")
  radio_button.click unless radio_button.selected?
end
submit(access_type, access_name) click to toggle source
# 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
swipe(start_x, start_y, end_x, end_y) click to toggle source
# 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
swipe_coordinates_with_direction(start_x, start_y, direction) click to toggle source
# 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
swipe_direction(direction) click to toggle source
# 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
swipe_element_with_direction(access_type, access_name, direction) click to toggle source
# 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
swipe_using_elements(access_type1, access_name1, access_type2, access_name2) click to toggle source
# 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
switch_frame(frame) click to toggle source

method to switch frame

# File lib/selenium-cucumber/methods/navigate_methods.rb, line 156
def switch_frame frame
  $driver.switch_to.frame(frame)
end
switch_to_main_content() click to toggle source

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
switch_to_main_window() click to toggle source

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
switch_to_new_window() click to toggle source

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
switch_to_previous_window() click to toggle source

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
switch_to_window_by_title(window_title) click to toggle source

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
switch_to_window_by_url(window_url) click to toggle source
# 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
take_screenshot() click to toggle source
# 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
toggle_checkbox(access_type, access_name) click to toggle source

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
uncheck_checkbox(access_type, access_name) click to toggle source

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
unselect_all_option_from_multiselect_dropdown(access_type, access_name) click to toggle source

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
valid_locator_type?(type) click to toggle source

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
valid_option_by?(option_by) click to toggle source

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
validate_locator(type) click to toggle source
# 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
validate_option_by(option_by) click to toggle source
# 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
validate_parameters(platform, browser_type, app_path) click to toggle source

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
wait(time) click to toggle source
# File lib/selenium-cucumber/methods/progress_methods.rb, line 3
def wait(time)
  sleep time.to_i
end
wait_for_element_to_display(access_type, access_name, duration) click to toggle source
# 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
wait_for_element_to_enable(access_type, access_name, duration) click to toggle source
# 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
zoom_in_out(operation) click to toggle source

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
zoom_in_out_till_element_display(access_type, operation, access_name) click to toggle source

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