Class: OnlyofficeWebdriverWrapper::WebDriver

Overview

Class for working with webdriver, main class of project

Constant Summary collapse

TIMEOUT_FILE_DOWNLOAD =

Returns Default timeout for waiting to file to download.

Returns:

  • (Integer)

    Default timeout for waiting to file to download

100
SUPPORTED_BROWSERS =

Returns list of supported browsers.

Returns:

  • (Array<Symbol>)

    list of supported browsers

%i[firefox chrome].freeze

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 WebdriverTabHelper

OnlyofficeWebdriverWrapper::WebdriverTabHelper::TIMEOUT_WAIT_ELEMENT

Constants included from WebdriverScreenshotHelper

OnlyofficeWebdriverWrapper::WebdriverScreenshotHelper::SCREENSHOT_CONTENT_TYPE

Constants included from WebdriverAlertHelper

OnlyofficeWebdriverWrapper::WebdriverAlertHelper::NO_ALERT_EXCEPTIONS

Constants included from WaitUntilMethods

OnlyofficeWebdriverWrapper::WaitUntilMethods::TIMEOUT_WAIT_ELEMENT

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, #new_tab, #resize_tab, #switch_to_main_tab, #switch_to_popup, #tab_count, #title_of_current_tab

Methods included from WebdriverStyleHelper

#get_style_parameter, #set_style_attribute, #set_style_show_by_xpath

Methods included from WebdriverScreenshotHelper

#amazon_s3_wrapper, #get_screenshot, #get_screenshot_and_upload, #screenshot_folder, #webdriver_screenshot

Methods included from WebdriverNavigationMethods

#current_url, #go_back, #open, #quit, #refresh

Methods included from WebdriverMoveCursorMethods

#drag_and_drop, #drag_and_drop_by, #mouse_over, #move_to_element_by_locator

Methods included from WebdriverJsMethods

#computed_style, #document_ready?, #dom_element_by_xpath, #element_size_by_js, #execute_javascript, #jquery_finished?, #jquery_loaded?, #object_absolute_position, #remove_element, #type_to_locator_by_javascript

Methods included from WebdriverHelper

#cleanup_download_folder, #download, #system_screenshot

Methods included from WebdriverFrameMethods

#select_frame, #select_top_frame

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_to_input, #type_to_locator

Methods included from WebdriverBrowserInfo

#browser_metadata, #browser_size

Methods included from WebdriverAttributesHelper

#attribute_exist?, #get_attribute, #get_attributes_of_several_elements, #get_index_of_elements_with_attribute, #remove_attribute, #set_attribute

Methods included from WebdriverAlertHelper

#alert_confirm, #alert_exists?, #alert_text

Methods included from WaitUntilMethods

#wait_until, #wait_until_element_disappear, #wait_until_element_present, #wait_until_element_visible

Methods included from RubyHelper

#debug?, #os_64_bit?

Methods included from FirefoxHelper

#firefox_service, #start_firefox_driver

Methods included from SelectListMethods

#get_all_combo_box_values, #get_element_index, #select_combo_box, #select_from_list_elements

Methods included from ScrollMethods

#current_scroll_position, #scroll_list_by_pixels

Methods included from GetTextMethods

#get_text, #get_text_array, #get_text_by_js, #get_text_of_several_elements

Methods included from ClickMethods

#click, #click_on_displayed, #click_on_locator, #click_on_locator_coordinates, #click_on_one_of_several_by_display, #click_on_one_of_several_by_parameter, #double_click, #double_click_on_locator_coordinates, #get_element, #get_element_by_display, #get_elements, #right_click, #right_click_on_locator_coordinates

Methods included from ChromeHelper

#chrome_service, #start_chrome_driver

Methods included from ChromeVersionHelper

#chrome_version, #chromedriver_path, #unknown_chrome_version

Constructor Details

#initialize(browser = :firefox, params = {}) ⇒ WebDriver

Returns a new instance of WebDriver.



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 77

def initialize(browser = :firefox, params = {})
  raise WebdriverSystemNotSupported, 'Your OS is not 64 bit. It is not supported' unless os_64_bit?

  ensure_supported_browser(browser)
  @device = params.fetch(:device, :desktop_linux)
  @record_video = params.fetch(:record_video, true)
  @headless = HeadlessHelper.new(record_video: record_video)
  @headless.start

  @download_directory = params.fetch(:download_directory, Dir.mktmpdir('webdriver-download'))
  @browser = params.fetch(:browser, browser)
  return if params[:do_not_start_browser]

  case browser
  when :firefox
    @driver = start_firefox_driver
  when :chrome
    @driver = start_chrome_driver
  end
  @browser_running = true
end

Instance Attribute Details

#browserSymbol

Returns browser to use.

Returns:

  • (Symbol)

    browser to use



65
66
67
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 65

def browser
  @browser
end

#browser_runningTrue, False (readonly)

Returns is browser currently running.

Returns:

  • (True, False)

    is browser currently running



67
68
69
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 67

def browser_running
  @browser_running
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



69
70
71
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 69

def device
  @device
end

#download_directoryString

Returns directory to which file is downloaded.

Returns:

  • (String)

    directory to which file is downloaded



71
72
73
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 71

def download_directory
  @download_directory
end

#driverArray, String

Returns default switches for chrome.

Returns:

  • (Array, String)

    default switches for chrome



63
64
65
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 63

def driver
  @driver
end

#headlessHeadlessHelper

Returns headless wrapper.

Returns:



73
74
75
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 73

def headless
  @headless
end

#record_videoTrue, False (readonly)

Returns should video be recorded.

Returns:

  • (True, False)

    should video be recorded



75
76
77
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 75

def record_video
  @record_video
end

Class Method Details

.clean_up(forced = false) ⇒ Object

Perform cleanup if something went wrong during tests

Parameters:

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

    should cleanup run in all cases



241
242
243
244
245
246
247
248
249
250
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 241

def self.clean_up(forced = false)
  return unless OnlyofficeFileHelper::LinuxHelper.user_name.include?('nct-at') ||
                OnlyofficeFileHelper::LinuxHelper.user_name.include?('ubuntu') ||
                forced

  OnlyofficeFileHelper::LinuxHelper.kill_all('chromedriver')
  OnlyofficeFileHelper::LinuxHelper.kill_all('geckodriver')
  OnlyofficeFileHelper::LinuxHelper.kill_all('Xvfb')
  OnlyofficeFileHelper::LinuxHelper.kill_all_browsers
end

Instance Method Details

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

This method returns an undefined value.

Perform an action on coordinate

Parameters:

  • xpath_name (String)

    to find element

  • right_by (Integer)

    x coordinate

  • down_by (Integer)

    y coordinate

  • action (Symbol) (defaults to: :click)

    to perform

  • times (Integer) (defaults to: 1)

    how much times to repeat



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

def action_on_locator_coordinates(xpath_name, right_by, down_by, action = :click, times = 1)
  wait_until_element_visible(xpath_name)
  (0...times).inject(move_to_driver_action(xpath_name, right_by, down_by)) { |acc, _elem| acc.send(action) }.perform
end

#element_present?(xpath_name) ⇒ Boolean

Check if element present on page It may be visible or invisible, but should be present in DOM tree

Parameters:

  • xpath_name (String)

    to find element

Returns:

  • (Boolean)

    result of check



134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 134

def element_present?(xpath_name)
  case xpath_name
  when PageObject::Elements::Element
    xpath_name.present?
  when Selenium::WebDriver::Element
    xpath_name.displayed?
  else
    @driver.find_element(:xpath, xpath_name)
    true
  end
rescue StandardError
  false
end

#element_visible?(xpath_name) ⇒ Boolean

Check if element visible on page It should be part of DOM and should be visible on current visible part of page

Parameters:

  • xpath_name (String)

    element to find

Returns:

  • (Boolean)

    result of check



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 165

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 StandardError => e
      OnlyofficeLoggerHelper.log("Element #{xpath_name} is not visible because of: #{e.message}")
      visible = false
    end
    visible
  else
    false
  end
end

#ensure_supported_browser(browser) ⇒ void

This method returns an undefined value.

Ensure that browser is supported

Parameters:

  • browser (Symbol)

    to check

Raises:

  • (RuntimeError)

    if browser is not supported



103
104
105
106
107
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 103

def ensure_supported_browser(browser)
  return if SUPPORTED_BROWSERS.include?(browser)

  raise("Unknown Browser: #{browser}")
end

#get_element_count(xpath_name, only_visible = true) ⇒ Integer

Return count of elements (visible and not visible)

Parameters:

  • xpath_name (String)

    xpath to find

  • only_visible (True, False) (defaults to: true)

    count only visible elements?

Returns:

  • (Integer)

    element count



152
153
154
155
156
157
158
159
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 152

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| !element.displayed? }.length : elements.length
  else
    0
  end
end

#one_of_several_elements_displayed?(xpath_several_elements) ⇒ True, False

Check if any element of xpath is displayed

Parameters:

  • xpath_several_elements (String)

    xpath to check

Returns:

  • (True, False)

    result of visibility



187
188
189
190
191
192
193
194
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 187

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 StandardError => e
  webdriver_error("Raise unknown exception: #{e}")
end

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

This method returns an undefined value.

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

Parameters:

  • xpath_name (String)

    name of dropdown list

  • horizontal_shift (Integer) (defaults to: 30)

    x value

  • vertical_shift (Integer) (defaults to: 0)

    y value



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

def open_dropdown_selector(xpath_name, horizontal_shift = 30, vertical_shift = 0)
  move_to_driver_action(xpath_name, horizontal_shift, vertical_shift).click.perform
end

#page_sourceString Also known as: get_page_source

Get page source

Returns:

  • (String)

    all page source



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

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

#wait_file_for_download(file_name, timeout = TIMEOUT_FILE_DOWNLOAD) ⇒ String

Wait for file to be downloaded

Parameters:

  • file_name (String)

    to wait for download

  • timeout (Integer) (defaults to: TIMEOUT_FILE_DOWNLOAD)

    to wait for file to download

Returns:

  • (String)

    full file name of downloaded file

Raises:

  • (StandardError)

    error if something happened and file not downloaded



226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 226

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

#webdriver_error(exception, error_message = nil) ⇒ void

This method returns an undefined value.

Raise an error, making a screenshot before it

Parameters:

  • exception (String, Object)

    class to raise

  • error_message (String) (defaults to: nil)

    to raise

Raises:

  • (Object)

    specified exception



212
213
214
215
216
217
218
219
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 212

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
  raise exception, "#{error_message}\n\nPage address: #{current_url}\n\nError #{webdriver_screenshot}"
end