Class: OnlyofficeWebdriverWrapper::WebDriver
- Inherits:
-
Object
- Object
- OnlyofficeWebdriverWrapper::WebDriver
show all
- Includes:
- ChromeHelper, FirefoxHelper, RubyHelper, WebdriverAlertHelper, WebdriverAttributesHelper, WebdriverBrowserInfo, WebdriverBrowserLogHelper, WebdriverHelper, WebdriverJsMethods, WebdriverScreenshotHelper, WebdriverStyleHelper, WebdriverTabHelper, WebdriverTypeHelper, WebdriverUserAgentHelper
- Defined in:
- lib/onlyoffice_webdriver_wrapper/webdriver.rb
Overview
noinspection RubyTooManyMethodsInspection, RubyInstanceMethodNamingConvention, RubyParameterNamingConvention
Constant Summary
collapse
- TIMEOUT_WAIT_ELEMENT =
15
- TIMEOUT_FILE_DOWNLOAD =
100
OnlyofficeWebdriverWrapper::WebdriverUserAgentHelper::USERAGENT_ANDROID_PHONE, OnlyofficeWebdriverWrapper::WebdriverUserAgentHelper::USERAGENT_IPAD_AIR_2_SAFARI, OnlyofficeWebdriverWrapper::WebdriverUserAgentHelper::USERAGENT_IPHONE, OnlyofficeWebdriverWrapper::WebdriverUserAgentHelper::USERAGENT_NEXUS_10_CHROME
ChromeHelper::DEFAULT_CHROME_SWITCHES
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
-
#action_on_locator_coordinates(xpath_name, right_by, down_by, action = :click, times = 1) ⇒ Object
-
#add_class_by_jquery(selector, class_name) ⇒ Object
-
#add_web_console_error(log) ⇒ Object
-
#click(element) ⇒ Object
-
#click_and_wait(element_to_click, element_to_wait) ⇒ Object
-
#click_on_displayed(xpath_name) ⇒ Object
-
#click_on_locator(xpath_name, by_javascript = false, count: 1) ⇒ Object
-
#click_on_locator_by_action(xpath) ⇒ Object
-
#click_on_locator_coordinates(xpath_name, right_by, down_by) ⇒ Object
-
#click_on_one_of_several_by_display(xpath_several_elements) ⇒ Object
-
#click_on_one_of_several_by_parameter(xpath_several_elements, parameter_name, parameter_value) ⇒ Object
-
#click_on_one_of_several_by_parameter_and_text(xpath_several_elements, parameter_name, parameter_value, text_to_click) ⇒ Object
-
#click_on_one_of_several_by_text(xpath_several_elements, text_to_click) ⇒ Object
-
#click_on_one_of_several_with_display_by_number(xpath_several_elements, number) ⇒ Object
-
#click_on_one_of_several_with_display_by_text(xpath_several_elements, text_to_click) ⇒ Object
-
#click_on_one_of_several_xpath_by_number(xpath, number_of_element) ⇒ Object
-
#context_click(xpath, x_coord, y_coord) ⇒ Object
-
#context_click_on_locator(xpath_name) ⇒ Object
Context click on locator.
-
#double_click(xpath_name) ⇒ Object
-
#double_click_on_locator_coordinates(xpath_name, right_by, down_by) ⇒ Object
-
#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.
-
#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.
-
#element_present?(xpath_name) ⇒ Boolean
-
#element_visible?(xpath_name) ⇒ Boolean
-
#get_all_combo_box_values(xpath_name) ⇒ Object
-
#get_element(object_identification) ⇒ Object
-
#get_element_by_display(xpath_name) ⇒ Object
-
#get_element_count(xpath_name, only_visible = true) ⇒ Object
-
#get_element_index(title, list_elements) ⇒ Object
-
#get_element_number_by_text(xpath_list, element_text) ⇒ Object
-
#get_elements(objects_identification, only_visible = true) ⇒ Object
-
#get_elements_from_array_after_some(xpath_several_elements, xpath_for_some) ⇒ Object
-
#get_elements_from_array_before_some(xpath_several_elements, xpath_for_some) ⇒ Object
-
#get_host_name ⇒ Object
-
#get_page_source ⇒ Object
-
#get_text(xpath_name, wait_until_visible = true) ⇒ Object
Get text of current element.
-
#get_text_array(array_elements) ⇒ Object
-
#get_text_of_several_elements(xpath_several_elements) ⇒ Object
-
#get_url ⇒ String
-
#go_back ⇒ Object
-
#initialize(browser = :firefox, remote_server = nil, device: :desktop_linux, proxy: nil) ⇒ WebDriver
constructor
A new instance of WebDriver.
-
#left_mouse_click(xpath, x_coord, y_coord) ⇒ Object
-
#mouse_over(xpath_name, x_coordinate = 0, y_coordinate = 0) ⇒ Object
-
#move_to_element(element) ⇒ Object
-
#move_to_element_by_locator(xpath_name) ⇒ Object
-
#move_to_one_of_several_displayed_element(xpath_several_elements) ⇒ Object
-
#one_of_several_elements_displayed?(xpath_several_elements) ⇒ Boolean
-
#open(url) ⇒ Object
-
#open_dropdown_selector(xpath_name, horizontal_shift = 30, vertical_shift = 0) ⇒ Object
Open dropdown selector, like 'Color Selector', which has no element id.
-
#quit ⇒ Object
-
#refresh ⇒ Object
-
#remove_attribute(xpath, attribute) ⇒ Object
-
#remove_class_by_jquery(selector, class_name) ⇒ Object
-
#remove_element(xpath) ⇒ Object
-
#remove_event(event_name) ⇒ Object
-
#right_click(xpath_name) ⇒ Object
-
#right_click_on_locator_coordinates(xpath_name, right_by = nil, down_by = nil) ⇒ Object
-
#right_click_on_one_of_several_by_text(xpath_several_elements, text_to_click) ⇒ Object
-
#scroll_list_by_pixels(list_xpath, pixels) ⇒ Object
-
#scroll_list_to_element(list_xpath, element_xpath) ⇒ Object
-
#select_combo_box(xpath_name, select_value, select_by = :value) ⇒ Object
-
#select_frame(xpath_name = '//iframe', count_of_frames = 1) ⇒ Object
-
#select_from_list(xpath_value, value) ⇒ Object
-
#select_from_list_elements(value, elements_value) ⇒ Object
-
#select_text_from_page(xpath_name) ⇒ Object
-
#select_top_frame ⇒ Object
Select top frame of browser (even if several subframes exists).
-
#service_unavailable? ⇒ Boolean
-
#set_parameter(xpath, attribute, attribute_value) ⇒ Object
-
#set_text_to_iframe(element, text) ⇒ Object
-
#wait_element(xpath_name, period_of_wait = 1, critical_time = 3) ⇒ Object
-
#wait_file_for_download(file_name, timeout = TIMEOUT_FILE_DOWNLOAD) ⇒ Object
-
#wait_until(timeout = ::PageObject.default_page_wait, message = nil, wait_js: true, &block) ⇒ Object
-
#wait_until_element_disappear(xpath_name) ⇒ Object
-
#wait_until_element_present(xpath_name) ⇒ Object
-
#wait_until_element_visible(xpath_name, timeout = 15) ⇒ Object
-
#webdriver_error(exception, error_message = nil) ⇒ Object
#browser_logs
#add_useragent_to_switches, #current_user_agent, #user_agent_for_device
#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
#get_style_attributes_of_several_elements, #get_style_parameter, #set_style_attribute, #set_style_parameter, #set_style_show_by_xpath
#amazon_s3_wrapper, #get_screenshot, #get_screenshot_and_upload, #screenshot_folder, #webdriver_screenshot
#computed_style, #document_ready?, #element_size_by_js, #execute_javascript, #get_text_by_js, #jquery_finished?, #jquery_loaded?, #jquery_selector_by_xpath, #object_absolute_position, #type_to_locator_by_javascript
#download, #system_screenshot
#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
#browser_metadata, #browser_size
#attribute_exist?, #get_attribute, #get_attribute_from_displayed_element, #get_attributes_of_several_elements, #get_index_of_elements_with_attribute
#alert_confirm, #alert_exists?, #alert_text
Methods included from RubyHelper
#debug?, #os_64_bit?
#firefox_service, #start_firefox_driver
#chrome_service, #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.
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 58
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
@browser_running = true
end
|
Instance Attribute Details
#browser ⇒ Object
Returns the value of attribute browser.
44
45
46
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 44
def browser
@browser
end
|
#browser_running ⇒ True, False
Returns is browser currently running.
46
47
48
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 46
def browser_running
@browser_running
end
|
#device ⇒ Symbol
Returns device of which we try to simulate, default - :desktop_linux.
48
49
50
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 48
def device
@device
end
|
#download_directory ⇒ Object
Returns the value of attribute download_directory.
50
51
52
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 50
def download_directory
@download_directory
end
|
#driver ⇒ Array, String
Returns default switches for chrome.
43
44
45
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 43
def driver
@driver
end
|
#headless ⇒ Object
Returns the value of attribute headless.
52
53
54
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 52
def headless
@headless
end
|
#ip_of_remote_server ⇒ Object
Returns the value of attribute ip_of_remote_server.
49
50
51
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 49
def ip_of_remote_server
@ip_of_remote_server
end
|
#proxy ⇒ Net::HTTP::Proxy
Returns connection proxy.
54
55
56
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 54
def proxy
@proxy
end
|
#server_address ⇒ Object
Returns the value of attribute server_address.
51
52
53
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 51
def server_address
@server_address
end
|
Class Method Details
.host_name_by_full_url(full_url) ⇒ Object
195
196
197
198
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 195
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
356
357
358
359
360
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 356
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
212
213
214
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 212
def add_class_by_jquery(selector, class_name)
execute_javascript("#{jquery_selector_by_xpath(selector)}.addClass('#{class_name}');")
end
|
#add_web_console_error(log) ⇒ Object
83
84
85
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 83
def add_web_console_error(log)
WebDriver.web_console_error = log
end
|
#click(element) ⇒ Object
136
137
138
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 136
def click(element)
element.click
end
|
#click_and_wait(element_to_click, element_to_wait) ⇒ Object
140
141
142
143
144
145
146
147
148
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 140
def click_and_wait(element_to_click, element_to_wait)
element_to_click.click
count = 0
while !element_to_wait.present? && 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
319
320
321
322
323
324
325
326
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 319
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 276
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
328
329
330
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 328
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
332
333
334
335
336
337
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 332
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
376
377
378
379
380
381
382
383
384
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 376
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
416
417
418
419
420
421
422
423
424
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 416
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
426
427
428
429
430
431
432
433
434
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 426
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
362
363
364
365
366
367
368
369
370
371
372
373
374
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 362
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
406
407
408
409
410
411
412
413
414
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 406
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
386
387
388
389
390
391
392
393
394
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 386
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
436
437
438
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 436
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
310
311
312
313
314
315
316
317
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 310
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
299
300
301
302
303
304
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 299
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
346
347
348
349
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 346
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
351
352
353
354
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 351
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
226
227
228
229
230
231
232
233
234
235
236
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 226
def drag_and_drop(xpath, x1, y1, x2, y2, mouse_release: true)
canvas = get_element(xpath)
move_action = @driver.action
.move_to(canvas, x1.to_i, y1.to_i)
.click_and_hold
.move_by(x2, y2)
move_action = move_action.release if mouse_release
move_action.perform
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
245
246
247
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 245
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
466
467
468
469
470
471
472
473
474
475
476
477
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 466
def element_present?(xpath_name)
if xpath_name.is_a?(PageObject::Elements::Element)
xpath_name.present?
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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 552
def element_visible?(xpath_name)
if xpath_name.is_a?(PageObject::Elements::Element) 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
173
174
175
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 173
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
119
120
121
122
123
124
125
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 119
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
523
524
525
526
527
528
529
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 523
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
531
532
533
534
535
536
537
538
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 531
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
166
167
168
169
170
171
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 166
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
674
675
676
677
678
679
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 674
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
540
541
542
543
544
545
546
547
548
549
550
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 540
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
511
512
513
514
515
516
517
518
519
520
521
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 511
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
497
498
499
500
501
502
503
504
505
506
507
508
509
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 497
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_page_source ⇒ Object
681
682
683
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 681
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
632
633
634
635
636
637
638
639
640
641
642
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 632
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
132
133
134
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 132
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
644
645
646
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 644
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_url ⇒ String
179
180
181
182
183
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 179
def get_url
execute_javascript('return window.location.href')
rescue Selenium::WebDriver::Error::NoSuchDriverError
raise 'Browser is crushed or hangup'
end
|
#go_back ⇒ Object
190
191
192
193
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 190
def go_back
@driver.navigate.back
OnlyofficeLoggerHelper.log('Go back to previous page')
end
|
#left_mouse_click(xpath, x_coord, y_coord) ⇒ Object
293
294
295
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 293
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
461
462
463
464
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 461
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
440
441
442
443
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 440
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
445
446
447
448
449
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 445
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
451
452
453
454
455
456
457
458
459
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 451
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
582
583
584
585
586
587
588
589
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 582
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
87
88
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 87
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
261
262
263
264
265
266
267
268
269
270
271
272
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 261
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
|
#quit ⇒ Object
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 100
def quit
return unless browser_running
begin
@driver.execute_script('window.onbeforeunload = null') 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)
@browser_running = false
end
|
#refresh ⇒ Object
185
186
187
188
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 185
def refresh
@driver.navigate.refresh
OnlyofficeLoggerHelper.log('Refresh page')
end
|
#remove_attribute(xpath, attribute) ⇒ Object
653
654
655
656
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 653
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
208
209
210
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 208
def remove_class_by_jquery(selector, class_name)
execute_javascript("#{jquery_selector_by_xpath(selector)}.removeClass('#{class_name}');")
end
|
#remove_element(xpath) ⇒ Object
601
602
603
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 601
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
204
205
206
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 204
def remove_event(event_name)
execute_javascript("jQuery(document).unbind('#{event_name}');")
end
|
#right_click(xpath_name) ⇒ Object
306
307
308
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 306
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
339
340
341
342
343
344
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 339
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
396
397
398
399
400
401
402
403
404
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 396
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
|
255
256
257
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 255
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
|
249
250
251
252
253
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 249
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
664
665
666
667
668
669
670
671
672
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 664
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
607
608
609
610
611
612
613
614
615
616
617
618
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 607
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
150
151
152
153
154
155
156
157
158
159
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 150
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
161
162
163
164
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 161
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
658
659
660
661
662
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 658
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_frame ⇒ Object
Select top frame of browser (even if several subframes exists)
621
622
623
624
625
626
627
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 621
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
724
725
726
727
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 724
def service_unavailable?
source = get_page_source
source.include?('Error 503')
end
|
#set_parameter(xpath, attribute, attribute_value) ⇒ Object
648
649
650
651
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 648
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
127
128
129
130
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 127
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
591
592
593
594
595
596
597
598
599
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 591
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
711
712
713
714
715
716
717
718
719
720
721
722
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 711
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
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 695
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
488
489
490
491
492
493
494
495
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 488
def wait_until_element_disappear(xpath_name)
wait = Selenium::WebDriver::Wait.new(timeout: TIMEOUT_WAIT_ELEMENT) 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
479
480
481
482
483
484
485
486
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 479
def wait_until_element_present(xpath_name)
wait = Selenium::WebDriver::Wait.new(timeout: TIMEOUT_WAIT_ELEMENT) 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
570
571
572
573
574
575
576
577
578
579
580
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 570
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
685
686
687
688
689
690
691
692
693
|
# File 'lib/onlyoffice_webdriver_wrapper/webdriver.rb', line 685
def webdriver_error(exception, error_message = nil)
if exception.is_a?(String) 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
|