Module: OnlyofficeWebdriverWrapper::WebdriverJsMethods

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

Overview

Methods for webdriver for calling Javascript

Instance Method Summary collapse

Instance Method Details

#computed_style(xpath, pseudo_element = 'null', property = nil) ⇒ String

Get ComputedStyle property value

Parameters:

  • xpath (String)

    xpath of element

  • pseudo_element (String) (defaults to: 'null')

    pseudo element to compute

  • property (String) (defaults to: nil)

    property to get

Returns:

  • (String)

    value of property



86
87
88
89
90
91
92
# File 'lib/onlyoffice_webdriver_wrapper/webdriver/webdriver_js_methods.rb', line 86

def computed_style(xpath, pseudo_element = 'null', property = nil)
  element_by_xpath = dom_element_by_xpath(xpath)
  style = "window.getComputedStyle(#{element_by_xpath}, '#{pseudo_element}')"
  full_command = "#{style}.getPropertyValue('#{property}')"
  result = execute_javascript("return #{full_command}")
  result.delete('"')
end

#document_ready?Boolean

Returns Is document is ready, finished to be loading.

Returns:

  • (Boolean)

    Is document is ready, finished to be loading



77
78
79
# File 'lib/onlyoffice_webdriver_wrapper/webdriver/webdriver_js_methods.rb', line 77

def document_ready?
  execute_javascript('return document.readyState;') == 'complete'
end

#dom_element_by_xpath(xpath) ⇒ String

Returns string to select dom by xpath.

Parameters:

  • xpath (String)

    element to select

Returns:

  • (String)

    string to select dom by xpath



27
28
29
30
# File 'lib/onlyoffice_webdriver_wrapper/webdriver/webdriver_js_methods.rb', line 27

def dom_element_by_xpath(xpath)
  escaped_xpath = xpath.tr('"', "'")
  "document.evaluate(\"#{escaped_xpath}\", document, null, XPathResult.ANY_TYPE, null).iterateNext()"
end

#element_size_by_js(xpath) ⇒ Dimensions

Calculate object size using Javascript

Parameters:

  • xpath (Sting)

    xpath of object

Returns:



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

def element_size_by_js(xpath)
  width = execute_javascript("return #{dom_element_by_xpath(xpath)}.offsetWidth")
  height = execute_javascript("return #{dom_element_by_xpath(xpath)}.offsetHeight")
  Dimensions.new(width, height)
end

#execute_javascript(script, wait_timeout: 0) ⇒ Object

Execute javascript

Parameters:

  • script (String)

    code to execute

  • wait_timeout (Integer) (defaults to: 0)

    wait after JS is executed. Some code require some time to execute



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/onlyoffice_webdriver_wrapper/webdriver/webdriver_js_methods.rb', line 10

def execute_javascript(script, wait_timeout: 0)
  result = @driver.execute_script(script)
  OnlyofficeLoggerHelper.log("Executed js: `#{script}` with result: `#{result}`")
  sleep(wait_timeout)
  result
rescue Timeout::Error => e
  # Usually this mean browser hang up or some modular
  # window is blocking browser for execution of any code
  # in that case performing `webdriver_error` only cause forever loop
  # since webdriver_error trying to current_url or make screenshots
  raise(e.class, "Timeout Error #{e} happened while executing #{script}")
rescue StandardError => e
  webdriver_error(e, "Exception #{e} in execute_javascript: #{script}")
end

#jquery_finished?Boolean

Checks for jQuery finished its job or not present

Returns:

  • (Boolean)


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

def jquery_finished?
  return true unless jquery_loaded?

  execute_javascript('return window.jQuery.active;').zero?
end

#jquery_loaded?True, False

Returns is page have jquery loaded.

Returns:

  • (True, False)

    is page have jquery loaded



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

def jquery_loaded?
  loaded = execute_javascript('return !!window.jQuery')
  OnlyofficeLoggerHelper.log("jquery_loaded? # #{loaded}")
  loaded
end

#object_absolute_position(xpath) ⇒ CursorPoint

Get object absolute position from top left edge of screen

Parameters:

  • xpath (Sting)

    xpath of object

Returns:

  • (CursorPoint)

    position of element



55
56
57
58
59
60
# File 'lib/onlyoffice_webdriver_wrapper/webdriver/webdriver_js_methods.rb', line 55

def object_absolute_position(xpath)
  bounding = "#{dom_element_by_xpath(xpath)}.getBoundingClientRect()"
  left = execute_javascript("return #{bounding}.left")
  top = execute_javascript("return #{bounding}.top")
  Dimensions.new(left, top)
end

#remove_element(xpath) ⇒ String

Remove element by its xpath

Parameters:

  • xpath (String)

    of element to remove

Returns:

  • (String)

    result of javascript execution



97
98
99
100
101
102
# File 'lib/onlyoffice_webdriver_wrapper/webdriver/webdriver_js_methods.rb', line 97

def remove_element(xpath)
  script = "element = #{dom_element_by_xpath(xpath)};" \
           'if (element !== null) ' \
           '{element.parentNode.removeChild(element);};'
  execute_javascript(script)
end

#type_to_locator_by_javascript(xpath, text) ⇒ void

This method returns an undefined value.

Type to locator by javascript

Parameters:

  • xpath (String)

    to find object where to type

  • text (String)

    to type



36
37
38
39
40
41
# File 'lib/onlyoffice_webdriver_wrapper/webdriver/webdriver_js_methods.rb', line 36

def type_to_locator_by_javascript(xpath, text)
  escaped_text = text.gsub('\\', '\\\\\\\\')
                     .gsub('"', '\\"')
                     .gsub("\n", '\\n')
  execute_javascript("#{dom_element_by_xpath(xpath)}.value=\"#{escaped_text}\";")
end