Module: Applitools::Selenium::DomCapture

Extended by:
DomCapture
Included in:
DomCapture
Defined in:
lib/applitools/selenium/dom_capture/dom_capture.rb,
lib/applitools/selenium/dom_capture/dom_capture_script.rb

Constant Summary collapse

CSS_DOWNLOAD_TIMEOUT =

2 seconds

2
DOM_CAPTURE_TIMEOUT =

10 seconds

10
CSS_CAPTURE_SCRIPT =
"  function extractCssResources() {\n    return Array.from(document.querySelectorAll('link[rel=\"stylesheet\"],style')).map(el => {\n      if (el.tagName.toUpperCase() === 'LINK') {\n          return {\"href\": el.getAttribute('href')};\n      } else {\n          return {\"text\": el.textContent};\n      }\n    });\n  }\n  return extractCssResources();\n"
DOM_CAPTURE_SCRIPT =
"  function captureFrame({ styleProps, attributeProps, rectProps, ignoredTagNames }) {\n    const NODE_TYPES = {\n      ELEMENT: 1,\n      TEXT: 3,\n    };\n    function filter(x) {\n      return !!x;\n    }\n    function notEmptyObj(obj) {\n      return Object.keys(obj).length ? obj : undefined;\n    }\n    function iframeToJSON(el) {\n      const obj = elementToJSON(el);\n      try {\n        if (el.contentDocument) {\n          obj.childNodes = [captureNode(el.contentDocument.documentElement)];\n        }\n      } catch (ex) {\n      } finally {\n        return obj;\n      }\n    }\n    function elementToJSON(el) {\n      const tagName = el.tagName.toUpperCase();\n      if (ignoredTagNames.indexOf(tagName) > -1) return null;\n      const computedStyle = window.getComputedStyle(el);\n      const boundingClientRect = el.getBoundingClientRect();\n      const style = {};\n      for (const p of styleProps) style[p] = computedStyle.getPropertyValue(p);\n      const rect = {};\n      for (const p of rectProps) rect[p] = boundingClientRect[p];\n      const attributes = {};\n      if (!attributeProps) {\n        if (el.hasAttributes()) {\n          var attrs = el.attributes;\n          for (const p of attrs) {\n            attributes[p.name] = p.value;\n          }\n        }\n      }\n      else {\n        if (attributeProps.all) {\n          for (const p of attributeProps.all) {\n            if (el.hasAttribute(p)) attributes[p] = el.getAttribute(p);\n          }\n        }\n        if (attributeProps[tagName]) {\n          for (const p of attributeProps[tagName]) {\n            if (el.hasAttribute(p)) attributes[p] = el.getAttribute(p);\n          }\n        }\n      }\n      return {\n        tagName,\n        style: notEmptyObj(style),\n        rect: notEmptyObj(rect),\n        attributes: notEmptyObj(attributes),\n        childNodes: Array.prototype.map.call(el.childNodes, captureNode).filter(filter),\n      };\n    }\n    function captureTextNode(node) {\n      return {\n        tagName: '#text',\n        text: node.textContent,\n      };\n    }\n    function captureNode(node) {\n      switch (node.nodeType) {\n        case NODE_TYPES.TEXT:\n          return captureTextNode(node);\n        case NODE_TYPES.ELEMENT:\n          if (node.tagName.toUpperCase() === 'IFRAME') {\n            return iframeToJSON(node);\n          } else {\n            return elementToJSON(node);\n          }\n        default:\n          return null;\n      }\n    }\n    return captureNode(document.documentElement);\n  }\n  return captureFrame(arguments[0]);\n"

Instance Method Summary collapse

Instance Method Details

#get_window_dom(driver, logger) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/applitools/selenium/dom_capture/dom_capture.rb', line 15

def get_window_dom(driver, logger)
  args_obj = {
    'styleProps' => %w(
      background-color background-image background-size color border-width
      border-color border-style padding margin
    ),
    'attributeProps' => nil,
    'rectProps' => %w(width height top left),
    'ignoredTagNames' => %w(HEAD SCRIPT)
  }
  dom_tree = ''
  if Timeout.respond_to?(:timeout)
    Timeout.timeout(DOM_CAPTURE_TIMEOUT) do
      dom_tree = driver.execute_script(Applitools::Selenium::DomCapture::DOM_CAPTURE_SCRIPT, args_obj)
      get_frame_dom(driver, { 'childNodes' => [dom_tree], 'tagName' => 'OUTER_HTML' }, logger)
    end
  else
    timeout(DOM_CAPTURE_TIMEOUT) do
      dom_tree = driver.execute_script(Applitools::Selenium::DomCapture::DOM_CAPTURE_SCRIPT, args_obj)
      get_frame_dom(driver, { 'childNodes' => [dom_tree], 'tagName' => 'OUTER_HTML' }, logger)
    end
  end
  dom_tree
end