Module: OnlyofficeWebdriverWrapper::ClickMethods

Included in:
WebDriver
Defined in:
lib/onlyoffice_webdriver_wrapper/webdriver/click_methods.rb,
lib/onlyoffice_webdriver_wrapper/webdriver/element_getters.rb

Overview

Methods to get elements

Instance Method Summary collapse

Instance Method Details

#click(element) ⇒ nil

Click on specified element

Parameters:

  • element (Selenium::WebDriver::Element)

    element to click

Returns:

  • (nil)

    nothing



9
10
11
# File 'lib/onlyoffice_webdriver_wrapper/webdriver/click_methods.rb', line 9

def click(element)
  element.click
end

#click_on_displayed(xpath_name) ⇒ nil

Click on one of several which displayed

Parameters:

  • xpath_name (String)

    xpath to find element

Returns:

  • (nil)


45
46
47
48
49
50
51
52
# File 'lib/onlyoffice_webdriver_wrapper/webdriver/click_methods.rb', line 45

def click_on_displayed(xpath_name)
  element = get_element_by_display(xpath_name)
  begin
    element.is_a?(Array) ? element.first.click : element.click
  rescue StandardError => e
    webdriver_error(e.class, "Exception #{e} in click_on_displayed(#{xpath_name})")
  end
end

#click_on_locator(xpath_name, by_javascript = false, count: 1, after_timeout: 0) ⇒ Object

Click on locator

Parameters:

  • xpath_name (String)

    xpath to click

  • by_javascript (True, False) (defaults to: false)

    should be clicked by javascript or by webdriver action

  • count (Integer) (defaults to: 1)

    count of clicks

  • after_timeout (Integer) (defaults to: 0)

    timeout after click



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/onlyoffice_webdriver_wrapper/webdriver/click_methods.rb', line 18

def click_on_locator(xpath_name, by_javascript = false, count: 1, after_timeout: 0)
  element = get_element(xpath_name)
  return webdriver_error("Element with xpath: #{xpath_name} not found") if element.nil?

  click_function = if by_javascript
                     -> { execute_javascript("#{dom_element_by_xpath(xpath_name)}.click();") }
                   else
                     -> { element.click }
                   end

  begin
    count.times do
      click_function.call
      sleep(after_timeout)
    end
  rescue Selenium::WebDriver::Error::ElementNotInteractableError => e
    webdriver_error(e.class,
                    'Selenium::WebDriver::Error::ElementNotInteractableError: ' \
                    "element not visible for xpath: #{xpath_name}")
  rescue StandardError => e
    webdriver_error(e.class, "UnknownError #{e.message} #{xpath_name}")
  end
end

#click_on_locator_coordinates(xpath_name, right_by, down_by) ⇒ nil

Click on locator by coordinates

Parameters:

  • xpath_name (String)

    xpath to click

  • right_by (Integer)

    shift to right

  • down_by (Integer)

    shift to bottom

Returns:

  • (nil)


59
60
61
62
63
# File 'lib/onlyoffice_webdriver_wrapper/webdriver/click_methods.rb', line 59

def click_on_locator_coordinates(xpath_name, right_by, down_by)
  wait_until_element_visible(xpath_name)
  move_to_driver_action(xpath_name, right_by, down_by).perform
  move_to_driver_action(xpath_name, right_by, down_by).click.perform
end

#click_on_one_of_several_by_display(xpath_several_elements) ⇒ True, False

Click on one of several which displayed

Parameters:

  • xpath_several_elements (String)

    xpath to find element

Returns:

  • (True, False)

    true if click successful, false if not found



68
69
70
71
72
73
74
75
76
# File 'lib/onlyoffice_webdriver_wrapper/webdriver/click_methods.rb', line 68

def click_on_one_of_several_by_display(xpath_several_elements)
  @driver.find_elements(:xpath, xpath_several_elements).each do |current_element|
    if current_element.displayed?
      current_element.click
      return true
    end
  end
  false
end

#click_on_one_of_several_by_parameter(xpath_several_elements, parameter_name, parameter_value) ⇒ True, False

Click on one of several xpath filtered by parameter and value

Parameters:

  • xpath_several_elements (String)

    xpath to select

  • parameter_name (String)

    parameter name

  • parameter_value (String)

    parameter value

Returns:

  • (True, False)

    true if click successful, false if not found



83
84
85
86
87
88
89
90
91
# File 'lib/onlyoffice_webdriver_wrapper/webdriver/click_methods.rb', line 83

def click_on_one_of_several_by_parameter(xpath_several_elements, parameter_name, parameter_value)
  @driver.find_elements(:xpath, xpath_several_elements).each do |current_element|
    if current_element.attribute(parameter_name).include? parameter_value
      current_element.click
      return true
    end
  end
  false
end

#double_click(xpath_name) ⇒ nil

Perform double_click on element

Parameters:

  • xpath_name (String)

    xpath to click

Returns:

  • (nil)


116
117
118
119
# File 'lib/onlyoffice_webdriver_wrapper/webdriver/click_methods.rb', line 116

def double_click(xpath_name)
  wait_until_element_visible(xpath_name)
  @driver.action.move_to(@driver.find_element(:xpath, xpath_name)).double_click.perform
end

#double_click_on_locator_coordinates(xpath_name, right_by, down_by) ⇒ nil

Perform double_click on specified coordinates

Parameters:

  • xpath_name (String)

    xpath to click

  • right_by (Integer)

    shift to right

  • down_by (Integer)

    shift to bottom

Returns:

  • (nil)


126
127
128
129
# File 'lib/onlyoffice_webdriver_wrapper/webdriver/click_methods.rb', line 126

def double_click_on_locator_coordinates(xpath_name, right_by, down_by)
  wait_until_element_visible(xpath_name)
  move_to_driver_action(xpath_name, right_by, down_by).double_click.perform
end

#get_element(object_identification) ⇒ Object?

Get element by it’s xpath

Parameters:

  • object_identification (String)

    xpath of object to find

Returns:

  • (Object, nil)

    nil if nothing found



9
10
11
12
13
14
15
# File 'lib/onlyoffice_webdriver_wrapper/webdriver/element_getters.rb', line 9

def get_element(object_identification)
  return object_identification unless object_identification.is_a?(String)

  @driver.find_element(:xpath, object_identification)
rescue StandardError
  nil
end

#get_element_by_display(xpath_name) ⇒ Object

Get first visible element from several

Parameters:

  • xpath_name (String)

    to find several objects

Returns:

  • (Object)

    first visible element

Raises:

  • (Selenium::WebDriver::Error::InvalidSelectorError)

    if selector is invalid



21
22
23
24
25
26
27
28
# File 'lib/onlyoffice_webdriver_wrapper/webdriver/element_getters.rb', line 21

def get_element_by_display(xpath_name)
  @driver.find_elements(:xpath, xpath_name).each do |element|
    return element if element.displayed?
  end
rescue Selenium::WebDriver::Error::InvalidSelectorError, Selenium::WebDriver::Error::JavascriptError
  webdriver_error(Selenium::WebDriver::Error::InvalidSelectorError,
                  "Invalid Selector: get_element_by_display('#{xpath_name}')")
end

#get_elements(objects_identification, only_visible = true) ⇒ Array, Object

Get array of webdriver object by xpath

Parameters:

  • objects_identification (String)

    object to find

  • only_visible (Boolean) (defaults to: true)

    return invisible if true

Returns:

  • (Array, Object)

    list of objects



34
35
36
37
38
39
40
41
42
43
44
# File 'lib/onlyoffice_webdriver_wrapper/webdriver/element_getters.rb', line 34

def get_elements(objects_identification, only_visible = true)
  return objects_identification if objects_identification.is_a?(Array)

  elements = @driver.find_elements(:xpath, objects_identification)
  if only_visible
    elements.each do |current|
      elements.delete(current) unless @browser == :firefox || current.displayed?
    end
  end
  elements
end

#right_click(xpath_name) ⇒ nil

Perform right click on xpath

Parameters:

  • xpath_name (String)

    xpath to click

Returns:

  • (nil)


96
97
98
99
100
# File 'lib/onlyoffice_webdriver_wrapper/webdriver/click_methods.rb', line 96

def right_click(xpath_name)
  wait_until_element_visible(xpath_name)

  @driver.action.context_click(@driver.find_element(:xpath, xpath_name)).perform
end

#right_click_on_locator_coordinates(xpath_name, right_by = nil, down_by = nil) ⇒ nil

Perform right click on locator with specified coordinates

Parameters:

  • xpath_name (String)

    xpath to click

  • right_by (Integer) (defaults to: nil)

    shift to right

  • down_by (Integer) (defaults to: nil)

    shift to bottom

Returns:

  • (nil)


107
108
109
110
111
# File 'lib/onlyoffice_webdriver_wrapper/webdriver/click_methods.rb', line 107

def right_click_on_locator_coordinates(xpath_name, right_by = nil, down_by = nil)
  wait_until_element_visible(xpath_name)
  move_to_driver_action(xpath_name, right_by, down_by).perform
  move_to_driver_action(xpath_name, right_by, down_by).context_click.perform
end