Class: OnlyofficeWebdriverWrapper::WebDriver

Overview

noinspection RubyTooManyMethodsInspection, RubyInstanceMethodNamingConvention, RubyParameterNamingConvention

Constant Summary collapse

TIMEOUT_WAIT_ELEMENT =
15
TIMEOUT_FILE_DOWNLOAD =
100

Constants included from WebdriverUserAgentHelper

OnlyofficeWebdriverWrapper::WebdriverUserAgentHelper::USERAGENT_ANDROID_PHONE, OnlyofficeWebdriverWrapper::WebdriverUserAgentHelper::USERAGENT_IPAD_AIR_2_SAFARI, OnlyofficeWebdriverWrapper::WebdriverUserAgentHelper::USERAGENT_IPHONE, OnlyofficeWebdriverWrapper::WebdriverUserAgentHelper::USERAGENT_NEXUS_10_CHROME

Constants included from ChromeHelper

ChromeHelper::DEFAULT_CHROME_SWITCHES

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from WebdriverBrowserLogHelper

#browser_logs

Methods included from WebdriverUserAgentHelper

#add_useragent_to_switches, #current_user_agent, #user_agent_for_device

Methods included from WebdriverTabHelper

#choose_tab, #close_popup_and_switch_to_main_tab, #close_tab, #close_window, #get_title_of_current_tab, #maximize, #new_tab, #resize_tab, #switch_to_main_tab, #switch_to_popup, #tab_count

Methods included from WebdriverStyleHelper

#get_style_attributes_of_several_elements, #get_style_parameter, #set_style_attribute, #set_style_parameter, #set_style_show_by_xpath

Methods included from WebdriverScreenshotHelper

#get_screenshot, #get_screenshot_and_upload, #screenshot_folder, #webdriver_screenshot

Methods included from WebdriverJsMethods

#document_ready?, #element_size_by_js, #execute_javascript, #get_text_by_js, #jquery_finished?, #jquery_loaded?, #object_absolute_position, #type_to_locator_by_javascript

Methods included from WebdriverHelper

#download, #system_screenshot

Methods included from WebdriverTypeHelper

#key_down, #key_up, #press_key, #send_keys, #send_keys_to_focused_elements, #type_text, #type_text_and_select_it, #type_text_by_symbol, #type_to_input, #type_to_locator

Methods included from WebdriverBrowserInfo

#browser_metadata, #browser_size

Methods included from WebdriverAttributesHelper

#attribute_exist?, #get_attribute, #get_attribute_from_displayed_element, #get_attributes_of_several_elements, #get_element_by_parameter, #get_index_of_elements_with_attribute

Methods included from WebdriverAlertHelper

#alert_confirm, #alert_exists?, #alert_text

Methods included from RubyHelper

#debug?, #os_64_bit?

Methods included from FirefoxHelper

#start_firefox_driver

Methods included from ChromeHelper

#chromedriver_path, #start_chrome_driver

Constructor Details

#initialize(browser = :firefox, remote_server = nil, device: :desktop_linux, proxy: nil) ⇒ WebDriver

Returns a new instance of WebDriver.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 55

def initialize(browser = :firefox,
               remote_server = nil,
               device: :desktop_linux,
               proxy: nil)
  raise WebdriverSystemNotSupported, 'Your OS is not 64 bit. It is not supported' unless os_64_bit?

  @device = device
  @headless = HeadlessHelper.new
  @headless.start

  @download_directory = Dir.mktmpdir('webdriver-download')
  @browser = browser
  @ip_of_remote_server = remote_server
  @proxy = proxy
  case browser
  when :firefox
    @driver = start_firefox_driver
  when :chrome
    @driver = start_chrome_driver
  else
    raise 'Unknown Browser: ' + browser.to_s
  end
end

Instance Attribute Details

#browserObject

Returns the value of attribute browser.



43
44
45
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 43

def browser
  @browser
end

#deviceSymbol

Returns device of which we try to simulate, default - :desktop_linux.

Returns:

  • (Symbol)

    device of which we try to simulate, default - :desktop_linux



45
46
47
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 45

def device
  @device
end

#download_directoryObject

Returns the value of attribute download_directory.



47
48
49
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 47

def download_directory
  @download_directory
end

#driverArray, String

Returns default switches for chrome.

Returns:

  • (Array, String)

    default switches for chrome



42
43
44
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 42

def driver
  @driver
end

#headlessObject

Returns the value of attribute headless.



49
50
51
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 49

def headless
  @headless
end

#ip_of_remote_serverObject

Returns the value of attribute ip_of_remote_server.



46
47
48
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 46

def ip_of_remote_server
  @ip_of_remote_server
end

#proxyNet::HTTP::Proxy

Returns connection proxy.

Returns:

  • (Net::HTTP::Proxy)

    connection proxy



51
52
53
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 51

def proxy
  @proxy
end

#server_addressObject

Returns the value of attribute server_address.



48
49
50
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 48

def server_address
  @server_address
end

Class Method Details

.host_name_by_full_url(full_url) ⇒ Object



188
189
190
191
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 188

def self.host_name_by_full_url(full_url)
  uri = URI(full_url)
  uri.port == 80 || uri.port == 443 ? "#{uri.scheme}://#{uri.host}" : "#{uri.scheme}://#{uri.host}:#{uri.port}"
end

Instance Method Details

#action_on_locator_coordinates(xpath_name, right_by, down_by, action = :click, times = 1) ⇒ Object



351
352
353
354
355
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 351

def action_on_locator_coordinates(xpath_name, right_by, down_by, action = :click, times = 1)
  wait_until_element_visible(xpath_name)
  element = @driver.find_element(:xpath, xpath_name)
  (0...times).inject(@driver.action.move_to(element, right_by.to_i, down_by.to_i)) { |acc, _elem| acc.send(action) }.perform
end

#add_class_by_jquery(selector, class_name) ⇒ Object



206
207
208
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 206

def add_class_by_jquery(selector, class_name)
  execute_javascript("$('#{selector}').addClass('#{class_name}');")
end

#add_web_console_error(log) ⇒ Object



79
80
81
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 79

def add_web_console_error(log)
  WebDriver.web_console_error = log
end

#click(element) ⇒ Object



129
130
131
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 129

def click(element)
  element.click
end

#click_and_wait(element_to_click, element_to_wait) ⇒ Object



133
134
135
136
137
138
139
140
141
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 133

def click_and_wait(element_to_click, element_to_wait)
  element_to_click.click
  count = 0
  while !element_to_wait.visible? && count < 30
    sleep 1
    count += 1
  end
  webdriver_error("click_and_wait: Wait for element: #{element_to_click} for 30 seconds") if count == 30
end

#click_on_displayed(xpath_name) ⇒ Object



314
315
316
317
318
319
320
321
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 314

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

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

Click on locator

Parameters:

  • count (Integer) (defaults to: 1)

    count of clicks



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 271

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

  if by_javascript
    execute_javascript("document.evaluate(\"#{xpath_name}\", document, null, XPathResult.ANY_TYPE, null).iterateNext().click();")
  else
    begin
      count.times { element.click }
    rescue Selenium::WebDriver::Error::ElementNotVisibleError
      webdriver_error("Selenium::WebDriver::Error::ElementNotVisibleError: element not visible for xpath: #{xpath_name}")
    rescue Exception => e
      webdriver_error(e.class, "UnknownError #{e.message} #{xpath_name}")
    end
  end
end

#click_on_locator_by_action(xpath) ⇒ Object



323
324
325
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 323

def click_on_locator_by_action(xpath)
  @driver.action.move_to(get_element(xpath)).click.perform
end

#click_on_locator_coordinates(xpath_name, right_by, down_by) ⇒ Object



327
328
329
330
331
332
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 327

def click_on_locator_coordinates(xpath_name, right_by, down_by)
  wait_until_element_visible(xpath_name)
  element = @driver.find_element(:xpath, xpath_name)
  @driver.action.move_to(element, right_by.to_i, down_by.to_i).perform
  @driver.action.move_to(element, right_by.to_i, down_by.to_i).click.perform
end

#click_on_one_of_several_by_display(xpath_several_elements) ⇒ Object



371
372
373
374
375
376
377
378
379
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 371

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) ⇒ Object



411
412
413
414
415
416
417
418
419
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 411

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

#click_on_one_of_several_by_parameter_and_text(xpath_several_elements, parameter_name, parameter_value, text_to_click) ⇒ Object



421
422
423
424
425
426
427
428
429
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 421

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

#click_on_one_of_several_by_text(xpath_several_elements, text_to_click) ⇒ Object



357
358
359
360
361
362
363
364
365
366
367
368
369
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 357

def click_on_one_of_several_by_text(xpath_several_elements, text_to_click)
  @driver.find_elements(:xpath, xpath_several_elements).each do |current_element|
    next unless text_to_click.to_s == current_element.attribute('innerHTML')

    begin
      current_element.click
    rescue Exception => e
      webdriver_error("Error in click_on_one_of_several_by_text(#{xpath_several_elements}, #{text_to_click}): #{e.message}")
    end
    return true
  end
  false
end

#click_on_one_of_several_with_display_by_number(xpath_several_elements, number) ⇒ Object



401
402
403
404
405
406
407
408
409
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 401

def click_on_one_of_several_with_display_by_number(xpath_several_elements, number)
  @driver.find_elements(:xpath, "#{xpath_several_elements}[#{number}]").each do |current_element|
    if current_element.displayed?
      current_element.click
      return true
    end
  end
  false
end

#click_on_one_of_several_with_display_by_text(xpath_several_elements, text_to_click) ⇒ Object



381
382
383
384
385
386
387
388
389
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 381

def click_on_one_of_several_with_display_by_text(xpath_several_elements, text_to_click)
  @driver.find_elements(:xpath, xpath_several_elements).each do |current_element|
    if current_element.displayed? && text_to_click == current_element.text
      current_element.click
      return true
    end
  end
  false
end

#click_on_one_of_several_xpath_by_number(xpath, number_of_element) ⇒ Object



431
432
433
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 431

def click_on_one_of_several_xpath_by_number(xpath, number_of_element)
  click_on_locator("(#{xpath})[#{number_of_element}]")
end

#context_click(xpath, x_coord, y_coord) ⇒ Object



305
306
307
308
309
310
311
312
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 305

def context_click(xpath, x_coord, y_coord)
  element = get_element(xpath)
  if browser == :firefox
    element.send_keys %i[shift f10]
  else
    @driver.action.move_to(element, x_coord.to_i, y_coord.to_i).context_click.perform
  end
end

#context_click_on_locator(xpath_name) ⇒ Object

Context click on locator

Parameters:

  • xpath_name (String)

    name of xpath to click



294
295
296
297
298
299
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 294

def context_click_on_locator(xpath_name)
  wait_until_element_visible(xpath_name)

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

#double_click(xpath_name) ⇒ Object



341
342
343
344
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 341

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) ⇒ Object



346
347
348
349
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 346

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

#drag_and_drop(xpath, x1, y1, x2, y2, mouse_release: true) ⇒ Object

Perform drag'n'drop action in one element (for example on big canvas area) for drag'n'drop one whole element use 'drag_and_drop_by'

Attributes

  • xpath - xpath of element on which drag and drop performed
  • x1 - x coordinate on element to start drag'n'drop
  • y1 - y coordinate on element to start drag'n'drop
  • x2 - shift vector x coordinate
  • y2 - shift vector y coordinate
  • mouse_release - release mouse after move


220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 220

def drag_and_drop(xpath, x1, y1, x2, y2, mouse_release: true)
  canvas = get_element(xpath)
  if mouse_release
    @driver.action.move_to(canvas, x1.to_i, y1.to_i).click_and_hold.move_by(x2, y2).release.perform
  else
    @driver.action.move_to(canvas, x1.to_i, y1.to_i).click_and_hold.move_by(x2, y2).perform
  end
rescue ArgumentError
  raise "Replace 'click_and_hold(element)' to 'click_and_hold(element = nil)' in action_builder.rb"
rescue TypeError => e
  webdriver_error("drag_and_drop(#{xpath}, #{x1}, #{y1}, #{x2}, #{y2}) TypeError: #{e.message}")
end

#drag_and_drop_by(source, right_by, down_by = 0) ⇒ Object

Perform drag'n'drop one whole element for drag'n'drop inside one element (f.e. canvas) use drag_and_drop

Attributes

  • source - xpath of element on which drag and drop performed
  • right_by - shift vector x coordinate
  • down_by - shift vector y coordinate


240
241
242
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 240

def drag_and_drop_by(source, right_by, down_by = 0)
  @driver.action.drag_and_drop_by(get_element(source), right_by, down_by).perform
end

#element_present?(xpath_name) ⇒ Boolean

Returns:

  • (Boolean)


461
462
463
464
465
466
467
468
469
470
471
472
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 461

def element_present?(xpath_name)
  if xpath_name.is_a?(PageObject::Elements::Element)
    xpath_name.visible?
  elsif xpath_name.is_a?(Selenium::WebDriver::Element)
    xpath_name.displayed?
  else
    @driver.find_element(:xpath, xpath_name)
    true
  end
rescue Exception
  false
end

#element_visible?(xpath_name) ⇒ Boolean

Returns:

  • (Boolean)


547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 547

def element_visible?(xpath_name)
  if xpath_name.is_a?(PageObject::Elements::Element) # PageObject always visible
    true
  elsif element_present?(xpath_name)
    element = get_element(xpath_name)
    return false if element.nil?

    begin
      visible = element.displayed?
    rescue Exception
      visible = false
    end
    visible
  else
    false
  end
end

#get_all_combo_box_values(xpath_name) ⇒ Object



166
167
168
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 166

def get_all_combo_box_values(xpath_name)
  @driver.find_element(:xpath, xpath_name).find_elements(tag_name: 'option').map { |el| el.attribute('value') }
end

#get_element(object_identification) ⇒ Object



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

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



518
519
520
521
522
523
524
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 518

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
  webdriver_error("get_element_by_display(#{xpath_name}): invalid selector: Unable to locate an element with the xpath expression")
end

#get_element_count(xpath_name, only_visible = true) ⇒ Object



526
527
528
529
530
531
532
533
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 526

def get_element_count(xpath_name, only_visible = true)
  if element_present?(xpath_name)
    elements = @driver.find_elements(:xpath, xpath_name)
    only_visible ? elements.delete_if { |element| @browser == :firefox && !element.displayed? }.length : elements.length
  else
    0
  end
end

#get_element_index(title, list_elements) ⇒ Object



159
160
161
162
163
164
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 159

def get_element_index(title, list_elements)
  list_elements.each_with_index do |current, i|
    return i if get_text(current) == title
  end
  nil
end

#get_element_number_by_text(xpath_list, element_text) ⇒ Object



669
670
671
672
673
674
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 669

def get_element_number_by_text(xpath_list, element_text)
  @driver.find_elements(:xpath, xpath_list).each_with_index do |current_element, index|
    return index if element_text == current_element.attribute('innerHTML')
  end
  nil
end

#get_elements(objects_identification, only_visible = true) ⇒ Object



535
536
537
538
539
540
541
542
543
544
545
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 535

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

#get_elements_from_array_after_some(xpath_several_elements, xpath_for_some) ⇒ Object



506
507
508
509
510
511
512
513
514
515
516
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 506

def get_elements_from_array_after_some(xpath_several_elements, xpath_for_some)
  elements = get_elements(xpath_several_elements)
  some_element = get_element(xpath_for_some)
  return elements if some_element.nil?

  elements.each do |current|
    elements.delete(current)
    break if current == some_element
  end
  elements
end

#get_elements_from_array_before_some(xpath_several_elements, xpath_for_some) ⇒ Object



492
493
494
495
496
497
498
499
500
501
502
503
504
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 492

def get_elements_from_array_before_some(xpath_several_elements, xpath_for_some)
  elements = get_elements(xpath_several_elements)
  result = []
  some_element = get_element(xpath_for_some)
  return result if some_element.nil?

  elements.each do |current|
    break if current == some_element

    result << current
  end
  result
end

#get_host_nameObject



193
194
195
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 193

def get_host_name
  WebDriver.host_name_by_full_url(get_url)
end

#get_page_sourceObject



676
677
678
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 676

def get_page_source
  @driver.execute_script('return document.documentElement.innerHTML;')
end

#get_text(xpath_name, wait_until_visible = true) ⇒ Object

Get text of current element

Parameters:

  • xpath_name (String)

    name of xpath

  • wait_until_visible (true, false) (defaults to: true)

    wait until element visible [@default = true]



627
628
629
630
631
632
633
634
635
636
637
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 627

def get_text(xpath_name, wait_until_visible = true)
  wait_until_element_visible(xpath_name) if wait_until_visible

  element = get_element(xpath_name)
  webdriver_error("get_text(#{xpath_name}, #{wait_until_visible}) not found element by xpath") if element.nil?
  if element.tag_name == 'input' || element.tag_name == 'textarea'
    element.attribute('value')
  else
    element.text
  end
end

#get_text_array(array_elements) ⇒ Object



125
126
127
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 125

def get_text_array(array_elements)
  get_elements(array_elements).map { |current_element| get_text(current_element) }
end

#get_text_of_several_elements(xpath_several_elements) ⇒ Object



639
640
641
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 639

def get_text_of_several_elements(xpath_several_elements)
  @driver.find_elements(:xpath, xpath_several_elements).map { |element| element.text unless element.text == '' }.compact
end

#get_urlString

it is a root frame

Returns:

  • (String)

    url of current frame, or browser url if



172
173
174
175
176
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 172

def get_url
  execute_javascript('return window.location.href')
rescue Selenium::WebDriver::Error::NoSuchDriverError
  raise 'Browser is crushed or hangup'
end

#go_backObject



183
184
185
186
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 183

def go_back
  @driver.navigate.back
  OnlyofficeLoggerHelper.log('Go back to previous page')
end

#left_mouse_click(xpath, x_coord, y_coord) ⇒ Object



288
289
290
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 288

def left_mouse_click(xpath, x_coord, y_coord)
  @driver.action.move_to(get_element(xpath), x_coord.to_i, y_coord.to_i).click.perform
end

#mouse_over(xpath_name, x_coordinate = 0, y_coordinate = 0) ⇒ Object



456
457
458
459
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 456

def mouse_over(xpath_name, x_coordinate = 0, y_coordinate = 0)
  wait_until_element_present(xpath_name)
  @driver.action.move_to(@driver.find_element(:xpath, xpath_name), x_coordinate.to_i, y_coordinate.to_i).perform
end

#move_to_element(element) ⇒ Object



435
436
437
438
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 435

def move_to_element(element)
  element = get_element(element) if element.is_a?(String)
  @driver.action.move_to(element).perform
end

#move_to_element_by_locator(xpath_name) ⇒ Object



440
441
442
443
444
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 440

def move_to_element_by_locator(xpath_name)
  element = get_element(xpath_name)
  @driver.action.move_to(element).perform
  OnlyofficeLoggerHelper.log("Moved mouse to element: #{xpath_name}")
end

#move_to_one_of_several_displayed_element(xpath_several_elements) ⇒ Object



446
447
448
449
450
451
452
453
454
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 446

def move_to_one_of_several_displayed_element(xpath_several_elements)
  get_elements(xpath_several_elements).each do |current_element|
    if current_element.displayed?
      move_to_element(current_element)
      return true
    end
  end
  false
end

#one_of_several_elements_displayed?(xpath_several_elements) ⇒ Boolean

Returns:

  • (Boolean)


577
578
579
580
581
582
583
584
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 577

def one_of_several_elements_displayed?(xpath_several_elements)
  @driver.find_elements(:xpath, xpath_several_elements).each do |current_element|
    return true if current_element.displayed?
  end
  false
rescue Exception => e
  webdriver_error("Raise unkwnown exception: #{e}")
end

#open(url) ⇒ Object



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

def open(url)
  url = 'http://' + url unless url.include?('http') || url.include?('file://')
  loop do
    begin
      @driver.navigate.to url
      break
    rescue Timeout::Error
      @driver.navigate.refresh
    end
  end
  OnlyofficeLoggerHelper.log("Opened page: #{url}")
end

#open_dropdown_selector(xpath_name, horizontal_shift = 30, vertical_shift = 0) ⇒ Object

Open dropdown selector, like 'Color Selector', which has no element id

Parameters:

  • xpath_name (String)

    name of dropdown list



256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 256

def open_dropdown_selector(xpath_name, horizontal_shift = 30, vertical_shift = 0)
  element = get_element(xpath_name)
  if @browser == :firefox || @browser == :safari
    set_style_attribute(xpath_name + '/button', 'display', 'none')
    set_style_attribute(xpath_name, 'display', 'inline')
    element.click
    set_style_attribute(xpath_name + '/button', 'display', 'inline-block')
    set_style_attribute(xpath_name, 'display', 'block')
  else
    @driver.action.move_to(element, horizontal_shift, vertical_shift).click.perform
  end
end

#quitObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 96

def quit
  begin
    @driver.execute_script('window.onbeforeunload = null') # off popup window
  rescue StandardError
    Exception
  end
  begin
    @driver.quit
  rescue Exception => e
    OnlyofficeLoggerHelper.log("Some error happened on webdriver.quit #{e.backtrace}")
  end
  alert_confirm
  @headless.stop
  FileUtils.remove_dir(@download_directory) if Dir.exist?(@download_directory)
end

#refreshObject



178
179
180
181
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 178

def refresh
  @driver.navigate.refresh
  OnlyofficeLoggerHelper.log('Refresh page')
end

#remove_attribute(xpath, attribute) ⇒ Object



648
649
650
651
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 648

def remove_attribute(xpath, attribute)
  execute_javascript("document.evaluate(\"#{xpath}\",document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null)." \
                         "singleNodeValue.removeAttribute('#{attribute}');")
end

#remove_class_by_jquery(selector, class_name) ⇒ Object



201
202
203
204
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 201

def remove_class_by_jquery(selector, class_name)
  execute_javascript('cd(window.frames[0])')
  execute_javascript("$('#{selector}').removeClass('#{class_name}');")
end

#remove_element(xpath) ⇒ Object



596
597
598
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 596

def remove_element(xpath)
  execute_javascript("element = document.evaluate(\"#{xpath}\", document, null, XPathResult.ANY_TYPE, null).iterateNext();if (element !== null) {element.parentNode.removeChild(element);};")
end

#remove_event(event_name) ⇒ Object



197
198
199
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 197

def remove_event(event_name)
  execute_javascript("jQuery(document).unbind('#{event_name}');")
end

#right_click(xpath_name) ⇒ Object



301
302
303
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 301

def right_click(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) ⇒ Object



334
335
336
337
338
339
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 334

def right_click_on_locator_coordinates(xpath_name, right_by = nil, down_by = nil)
  wait_until_element_visible(xpath_name)
  element = @driver.find_element(:xpath, xpath_name)
  @driver.action.move_to(element, right_by.to_i, down_by.to_i).perform
  @driver.action.move_to(element, right_by.to_i, down_by.to_i).context_click.perform
end

#right_click_on_one_of_several_by_text(xpath_several_elements, text_to_click) ⇒ Object



391
392
393
394
395
396
397
398
399
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 391

def right_click_on_one_of_several_by_text(xpath_several_elements, text_to_click)
  @driver.find_elements(:xpath, xpath_several_elements).each do |current_element|
    if text_to_click == current_element.text
      @driver.action.context_click(current_element).perform
      return true
    end
  end
  false
end

#scroll_list_by_pixels(list_xpath, pixels) ⇒ Object



250
251
252
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 250

def scroll_list_by_pixels(list_xpath, pixels)
  execute_javascript("$(document.evaluate(\"#{list_xpath.tr('"', "'")}\", document, null, XPathResult.ANY_TYPE, null).iterateNext()).scrollTop(#{pixels})")
end

#scroll_list_to_element(list_xpath, element_xpath) ⇒ Object



244
245
246
247
248
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 244

def scroll_list_to_element(list_xpath, element_xpath)
  execute_javascript("$(document.evaluate(\"#{list_xpath}\", document, null, XPathResult.ANY_TYPE, null).
      iterateNext()).jScrollPane().data('jsp').scrollToElement(document.evaluate(\"#{element_xpath}\",
      document, null, XPathResult.ANY_TYPE, null).iterateNext());")
end

#select_combo_box(xpath_name, select_value, select_by = :value) ⇒ Object



659
660
661
662
663
664
665
666
667
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 659

def select_combo_box(xpath_name, select_value, select_by = :value)
  wait_until_element_visible(xpath_name)
  option = Selenium::WebDriver::Support::Select.new(get_element(xpath_name))
  begin
    option.select_by(select_by, select_value)
  rescue StandardError
    option.select_by(:text, select_value)
  end
end

#select_frame(xpath_name = '//iframe', count_of_frames = 1) ⇒ Object

Select frame as current

Parameters:

  • xpath_name (String) (defaults to: '//iframe')

    name of current xpath



602
603
604
605
606
607
608
609
610
611
612
613
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 602

def select_frame(xpath_name = '//iframe', count_of_frames = 1)
  (0...count_of_frames).each do
    begin
      frame = @driver.find_element(:xpath, xpath_name)
      @driver.switch_to.frame frame
    rescue Selenium::WebDriver::Error::NoSuchElementError
      OnlyofficeLoggerHelper.log('Raise NoSuchElementError in the select_frame method')
    rescue Exception => e
      webdriver_error("Raise unkwnown exception: #{e}")
    end
  end
end

#select_from_list(xpath_value, value) ⇒ Object



143
144
145
146
147
148
149
150
151
152
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 143

def select_from_list(xpath_value, value)
  @driver.find_element(:xpath, xpath_value).find_elements(tag_name: 'li').each do |element|
    next unless element.text == value.to_s

    element.click
    return true
  end

  webdriver_error("select_from_list: Option #{value} in list #{xpath_value} not found")
end

#select_from_list_elements(value, elements_value) ⇒ Object



154
155
156
157
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 154

def select_from_list_elements(value, elements_value)
  index = get_element_index(value, elements_value)
  elements_value[index].click
end

#select_text_from_page(xpath_name) ⇒ Object



653
654
655
656
657
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 653

def select_text_from_page(xpath_name)
  wait_until_element_visible(xpath_name)
  elem = get_element xpath_name
  @driver.action.key_down(:control).click(elem).send_keys('a').key_up(:control).perform
end

#select_top_frameObject

Select top frame of browser (even if several subframes exists)



616
617
618
619
620
621
622
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 616

def select_top_frame
  @driver.switch_to.default_content
rescue Timeout::Error
  OnlyofficeLoggerHelper.log('Raise TimeoutError in the select_top_frame method')
rescue Exception => e
  raise "Browser is crushed or hangup with error: #{e}"
end

#service_unavailable?Boolean

Returns:

  • (Boolean)


719
720
721
722
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 719

def service_unavailable?
  source = get_page_source
  source.include?('Error 503')
end

#set_parameter(xpath, attribute, attribute_value) ⇒ Object



643
644
645
646
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 643

def set_parameter(xpath, attribute, attribute_value)
  execute_javascript("document.evaluate(\"#{xpath.tr('"', "'")}\",document, null, XPathResult.ANY_TYPE, null ).iterateNext()." \
                         "#{attribute}=\"#{attribute_value}\";")
end

#set_text_to_iframe(element, text) ⇒ Object



120
121
122
123
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 120

def set_text_to_iframe(element, text)
  element.click
  @driver.action.send_keys(text).perform
end

#wait_element(xpath_name, period_of_wait = 1, critical_time = 3) ⇒ Object



586
587
588
589
590
591
592
593
594
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 586

def wait_element(xpath_name, period_of_wait = 1, critical_time = 3)
  wait_until_element_present(xpath_name)
  time = 0
  until element_visible?(xpath_name)
    sleep(period_of_wait)
    time += 1
    return if time == critical_time
  end
end

#wait_file_for_download(file_name, timeout = TIMEOUT_FILE_DOWNLOAD) ⇒ Object



706
707
708
709
710
711
712
713
714
715
716
717
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 706

def wait_file_for_download(file_name, timeout = TIMEOUT_FILE_DOWNLOAD)
  full_file_name = "#{@download_directory}/#{file_name}"
  full_file_name = file_name if file_name[0] == '/'
  counter = 0
  while !File.exist?(full_file_name) && counter < timeout
    OnlyofficeLoggerHelper.log("Waiting for download file #{full_file_name} for #{counter} of #{timeout}")
    sleep 1
    counter += 1
  end
  webdriver_error("File #{full_file_name} not download for #{timeout} seconds") if counter >= timeout
  full_file_name
end

#wait_until(timeout = ::PageObject.default_page_wait, message = nil, wait_js: true, &block) ⇒ Object



690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 690

def wait_until(timeout = ::PageObject.default_page_wait, message = nil, wait_js: true, &block)
  tries ||= 3
  wait = Object::Selenium::WebDriver::Wait.new(timeout: timeout, message: message)
  wait.until(&block)
  if wait_js
    wait.until { document_ready? }
    wait.until { jquery_finished? }
  end
rescue Selenium::WebDriver::Error::TimeOutError
  webdriver_error("Wait until timeout: #{timeout} seconds in")
rescue Selenium::WebDriver::Error::StaleElementReferenceError
  OnlyofficeLoggerHelper.log("Wait until: rescuing from Stale Element error, #{tries} attempts remaining")
  retry unless (tries -= 1).zero?
  webdriver_error('Wait until: rescuing from Stale Element error failed after 3 tries')
end

#wait_until_element_disappear(xpath_name) ⇒ Object



483
484
485
486
487
488
489
490
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 483

def wait_until_element_disappear(xpath_name)
  wait = Selenium::WebDriver::Wait.new(timeout: TIMEOUT_WAIT_ELEMENT) # seconds
  begin
    wait.until { get_element(xpath_name) ? false : true }
  rescue Selenium::WebDriver::Error::TimeOutError
    webdriver_error("wait_until_element_present(#{xpath_name}) Selenium::WebDriver::Error::TimeOutError: timed out after 15 seconds")
  end
end

#wait_until_element_present(xpath_name) ⇒ Object



474
475
476
477
478
479
480
481
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 474

def wait_until_element_present(xpath_name)
  wait = Selenium::WebDriver::Wait.new(timeout: TIMEOUT_WAIT_ELEMENT) # seconds
  begin
    wait.until { get_element(xpath_name) }
  rescue Selenium::WebDriver::Error::TimeOutError
    webdriver_error("wait_until_element_present(#{xpath_name}) Selenium::WebDriver::Error::TimeOutError: timed out after 15 seconds")
  end
end

#wait_until_element_visible(xpath_name, timeout = 15) ⇒ Object



565
566
567
568
569
570
571
572
573
574
575
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 565

def wait_until_element_visible(xpath_name, timeout = 15)
  wait_until_element_present(xpath_name)
  time = 0
  while !element_visible?(xpath_name) && time < timeout
    sleep(1)
    time += 1
  end
  return unless time >= timeout

  webdriver_error("Element #{xpath_name} not visible for #{timeout} seconds")
end

#webdriver_error(exception, error_message = nil) ⇒ Object

Raises:

  • (exception)


680
681
682
683
684
685
686
687
688
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 680

def webdriver_error(exception, error_message = nil)
  if exception.is_a?(String) # If there is no error_message
    error_message = exception
    exception = RuntimeError
  end
  select_top_frame
  current_url = get_url
  raise exception, "#{error_message}\n\nPage address: #{current_url}\n\nError #{webdriver_screenshot}"
end