Module: OnlyofficeWebdriverWrapper::WebdriverAttributesHelper

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

Overview

Module with methods to work with attributes

Instance Method Summary collapse

Instance Method Details

#attribute_exist?(xpath_name, attribute) ⇒ Boolean

Check if attribute of xpath is exists

Parameters:

  • xpath_name (String)

    to find object

  • attribute (String)

    name to check

Returns:

  • (Boolean)

    result of check



10
11
12
13
14
15
16
17
18
19
# File 'lib/onlyoffice_webdriver_wrapper/webdriver/webdriver_attributes_helper.rb', line 10

def attribute_exist?(xpath_name, attribute)
  element = xpath_name.is_a?(String) ? get_element(xpath_name) : xpath_name
  begin
    attribute_value = element.attribute(attribute)
    exist = !(attribute_value.empty? || attribute_value.nil?)
  rescue StandardError
    exist = false
  end
  exist
end

#get_attribute(xpath_name, attribute) ⇒ String

Get attribute of element

Parameters:

  • xpath_name (String)

    to find object

  • attribute (String)

    to get

Returns:

  • (String)

    value of attribute



25
26
27
28
29
30
31
32
33
# File 'lib/onlyoffice_webdriver_wrapper/webdriver/webdriver_attributes_helper.rb', line 25

def get_attribute(xpath_name, attribute)
  element = xpath_name.is_a?(Selenium::WebDriver::Element) ? xpath_name : get_element(xpath_name)

  if element.nil?
    webdriver_error("Webdriver.get_attribute(#{xpath_name}, #{attribute}) failed because element not found")
  else
    element.attribute(attribute)
  end
end

#get_attributes_of_several_elements(xpath_several_elements, attribute) ⇒ Array<String>

Get attributes of several elements

Parameters:

  • xpath_several_elements (String)

    to find objects

  • attribute (String)

    to get

Returns:

  • (Array<String>)

    list of attributes



39
40
41
42
43
44
45
# File 'lib/onlyoffice_webdriver_wrapper/webdriver/webdriver_attributes_helper.rb', line 39

def get_attributes_of_several_elements(xpath_several_elements, attribute)
  elements = @driver.find_elements(:xpath, xpath_several_elements)

  elements.map do |element|
    element.attribute(attribute)
  end
end

#get_index_of_elements_with_attribute(xpath, attribute, value, only_visible = true) ⇒ Integer

Get index of element from array with attribute value

Parameters:

  • xpath (String)

    to find objects

  • attribute (String)

    to check

  • value (String)

    to compare

  • only_visible (Boolean) (defaults to: true)

    ignore invisible elements unless only_visible

Returns:

  • (Integer)

    index of element or ‘0` if not found



53
54
55
56
57
58
# File 'lib/onlyoffice_webdriver_wrapper/webdriver/webdriver_attributes_helper.rb', line 53

def get_index_of_elements_with_attribute(xpath, attribute, value, only_visible = true)
  get_elements(xpath, only_visible).each_with_index do |element, index|
    return (index + 1) if get_attribute(element, attribute).include?(value)
  end
  0
end

#remove_attribute(xpath, attribute) ⇒ String

Remove attribute of element

Parameters:

  • xpath (String)

    xpath of element

  • attribute (String)

    attribute to remove

Returns:

  • (String)

    result of execution



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

def remove_attribute(xpath, attribute)
  execute_javascript("#{dom_element_by_xpath(xpath)}.removeAttribute('#{attribute}');")
end

#set_attribute(xpath, attribute, attribute_value) ⇒ String Also known as: set_parameter

Set element attribute

Parameters:

  • xpath (String)

    element to select

  • attribute (String)

    attribute to set

  • attribute_value (String)

    value of attribute

Returns:

  • (String)

    result of execution



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

def set_attribute(xpath, attribute, attribute_value)
  execute_javascript("#{dom_element_by_xpath(xpath)}.#{attribute}=\"#{attribute_value}\";")
end