Module: OnlyofficeWebdriverWrapper::WebdriverTypeHelper

Included in:
WebDriver
Defined in:
lib/onlyoffice_webdriver_wrapper/webdriver/webdriver_type_helper.rb

Overview

Class for helping with type stuff

Instance Method Summary collapse

Instance Method Details

#key_down(xpath, key) ⇒ void

This method returns an undefined value.

Simulate pressed down key on object

Parameters:

  • xpath (String)

    to find object

  • key (String, Symbol)

    to press



130
131
132
133
# File 'lib/onlyoffice_webdriver_wrapper/webdriver/webdriver_type_helper.rb', line 130

def key_down(xpath, key)
  @driver.action.key_down(get_element(xpath), key).perform
  sleep(1) # for some reason quick key_down select text in control
end

#key_up(xpath, key) ⇒ void

This method returns an undefined value.

Release pressed key from object

Parameters:

  • xpath (String)

    to find object

  • key (String, Symbol)

    to release



139
140
141
# File 'lib/onlyoffice_webdriver_wrapper/webdriver/webdriver_type_helper.rb', line 139

def key_up(xpath, key)
  @driver.action.key_up(get_element(xpath), key).perform
end

#press_key(key) ⇒ void

This method returns an undefined value.

Press some specific key

Parameters:

  • key (String, Symbol)

    to press



122
123
124
# File 'lib/onlyoffice_webdriver_wrapper/webdriver/webdriver_type_helper.rb', line 122

def press_key(key)
  @driver.action.send_keys(key).perform
end

#send_keys(xpath_name, text_to_send, by_action = true) ⇒ void

This method returns an undefined value.

Send keys to object

Parameters:

  • xpath_name (String)

    to find object

  • text_to_send (String)

    text to type

  • by_action (Boolean) (defaults to: true)

    type by ‘@driver.action` if true



99
100
101
102
103
104
105
106
107
# File 'lib/onlyoffice_webdriver_wrapper/webdriver/webdriver_type_helper.rb', line 99

def send_keys(xpath_name, text_to_send, by_action = true)
  element = get_element(xpath_name)
  @driver.action.click(element).perform if @browser == :firefox
  if by_action
    @driver.action.send_keys(element, text_to_send).perform
  else
    element.send_keys text_to_send
  end
end

#send_keys_to_focused_elements(keys, count_of_times = 1) ⇒ void

This method returns an undefined value.

Type text to currently focused element

Parameters:

  • keys (Array<String, Symbol>, String, Symbol)

    to send

  • count_of_times (Integer) (defaults to: 1)

    how much times to repeat



113
114
115
116
117
# File 'lib/onlyoffice_webdriver_wrapper/webdriver/webdriver_type_helper.rb', line 113

def send_keys_to_focused_elements(keys, count_of_times = 1)
  command = @driver.action.send_keys(keys)
  (count_of_times - 1).times { command = command.send_keys(keys) }
  command.perform
end

#type_text(element, text_to_send, clear_area = false) ⇒ void

This method returns an undefined value.

Type text to element

Parameters:

  • element (String)

    to find object

  • text_to_send (String)

    text to type

  • clear_area (Boolean) (defaults to: false)

    should area be cleared



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

def type_text(element, text_to_send, clear_area = false)
  element = get_element(element)
  element.clear if clear_area
  element.send_keys(text_to_send)
end

#type_text_and_select_it(element, text_to_send, clear_area = false) ⇒ void

This method returns an undefined value.

Type text to element and select it

Parameters:

  • element (String)

    to find object

  • text_to_send (String)

    text to type

  • clear_area (Boolean) (defaults to: false)

    should area be cleared



22
23
24
25
# File 'lib/onlyoffice_webdriver_wrapper/webdriver/webdriver_type_helper.rb', line 22

def type_text_and_select_it(element, text_to_send, clear_area = false)
  type_text(element, text_to_send, clear_area)
  text_to_send.length.times { element.send_keys %i[shift left] }
end

#type_to_input(xpath_name, text_to_send, clear_content = false, click_on_it = true) ⇒ void

This method returns an undefined value.

Type text to input

Parameters:

  • xpath_name (String)

    to find object

  • text_to_send (String)

    text to type

  • clear_content (Boolean) (defaults to: false)

    should content be cleared

  • click_on_it (Boolean) (defaults to: true)

    should object be clicked



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/onlyoffice_webdriver_wrapper/webdriver/webdriver_type_helper.rb', line 71

def type_to_input(xpath_name, text_to_send, clear_content = false, click_on_it = true)
  element = get_element(xpath_name)
  if element.nil?
    webdriver_error(Selenium::WebDriver::Error::NoSuchElementError,
                    "type_to_input(#{xpath_name}, #{text_to_send}, " \
                    "#{clear_content}, #{click_on_it}): element not found")
  end
  element.clear if clear_content
  sleep 0.2
  if click_on_it
    begin
      element.click
    rescue StandardError => e
      webdriver_error(e.class,
                      "type_to_input(#{xpath_name}, #{text_to_send}, " \
                      "#{clear_content}, #{click_on_it}) " \
                      "error in 'element.click' error: #{e}")
    end
    sleep 0.2
  end
  element.send_keys text_to_send
end

#type_to_locator(xpath_name, text_to_send, clear_content = true, click_on_it = false, by_action = false, by_element_send_key = false) ⇒ void

This method returns an undefined value.

Type text to object

Parameters:

  • xpath_name (String)

    to find object

  • text_to_send (String)

    text to type

  • clear_content (Boolean) (defaults to: true)

    should content be cleared

  • click_on_it (Boolean) (defaults to: false)

    should object be clicked

  • by_action (Boolean) (defaults to: false)

    type by ‘@driver.action` if true

  • by_element_send_key (Boolean) (defaults to: false)

    use ‘element.send_keys` if true



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/onlyoffice_webdriver_wrapper/webdriver/webdriver_type_helper.rb', line 35

def type_to_locator(xpath_name,
                    text_to_send,
                    clear_content = true,
                    click_on_it = false,
                    by_action = false,
                    by_element_send_key = false)
  element = get_element(xpath_name)
  if clear_content
    begin
      element.clear
    rescue StandardError => e
      webdriver_error(e.class, "Error in element.clear #{e} for " \
                               "type_to_locator(#{xpath_name}, #{text_to_send}, " \
                               "#{clear_content}, #{click_on_it}, " \
                               "#{by_action}, #{by_element_send_key})")
    end
  end

  click_on_locator(xpath_name, after_timeout: 0.2) if click_on_it

  if (@browser != :chrome && !by_action) || by_element_send_key
    element.send_keys text_to_send
  else
    send_text_by_chars(text_to_send)
  end
rescue Selenium::WebDriver::Error::JavascriptError
  webdriver_error(Selenium::WebDriver::Error::InvalidElementStateError,
                  "Invalid state of element: #{xpath_name}")
end