Class: Selenium::WebDriver::Element

Inherits:
Object
  • Object
show all
Includes:
Appium::Core::Base::SearchContext, Appium::Core::Base::TakesScreenshot
Defined in:
lib/appium_lib_core/patch.rb

Overview

Implement useful features for element. Patch for Selenium Webdriver.

Constant Summary

Constants included from Appium::Core::Base::SearchContext

Appium::Core::Base::SearchContext::FINDERS

Instance Method Summary collapse

Methods included from Appium::Core::Base::TakesScreenshot

#element_screenshot_as, #save_element_screenshot, #save_viewport_screenshot

Methods included from Appium::Core::Base::SearchContext

#find_element, #find_elements

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ String

Returns the value of attributes like below. Read each platform to know more details.

uiautomator2: github.com/appium/appium-uiautomator2-server/blob/203cc7e57ce477f3cff5d95b135d1b3450a6033a/app/src/main/java/io/appium/uiautomator2/utils/Attribute.java#L19

checkable, checked, class, clickable, content-desc, enabled, focusable, focused
long-clickable, package, password, resource-id, scrollable, selection-start, selection-end
selected, text, bounds, index

XCUITest automation name supports below attributes.

UID, accessibilityContainer, accessible, enabled, frame,
label, name, rect, type, value, visible, wdAccessibilityContainer,
wdAccessible, wdEnabled, wdFrame, wdLabel, wdName, wdRect, wdType,
wdUID, wdValue, wdVisible

Examples:


e = @driver.find_element :accessibility_id, 'something'
e.value
e.resource_id # call 'e.attribute "resource-id"'

Returns:

  • (String)

48
49
50
51
52
53
# File 'lib/appium_lib_core/patch.rb', line 48

def method_missing(method_name, *args, &block)
  ignore_list = [:to_hash]
  return if ignore_list.include? method_name

  respond_to?(method_name) ? attribute(method_name.to_s.tr('_', '-')) : super
end

Instance Method Details

#immediate_value(*value) ⇒ Object

Set the value to element directly

Examples:


@driver.immediate_value 'hello'

68
69
70
# File 'lib/appium_lib_core/patch.rb', line 68

def immediate_value(*value)
  @bridge.set_immediate_value(self, *value)
end

#location_rel(driver) ⇒ ::Selenium::WebDriver::Point

For use with location_rel.

Examples:


e = @driver.find_element :accessibility_id, 'something'
e.location_rel @driver

Returns:

  • (::Selenium::WebDriver::Point)

    the relative x, y in a struct. ex: { x: 0.50, y: 0.20 }


91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/appium_lib_core/patch.rb', line 91

def location_rel(driver)
  rect = self.rect
  location_x = rect.x.to_f
  location_y = rect.y.to_f

  size_width  = rect.width.to_f
  size_height = rect.height.to_f

  center_x = location_x + (size_width / 2.0)
  center_y = location_y + (size_height / 2.0)

  w = driver.window_size
  ::Selenium::WebDriver::Point.new "#{center_x} / #{w.width.to_f}", "#{center_y} / #{w.height.to_f}"
end

#replace_value(*value) ⇒ Object

Replace the value to element directly

Examples:


@driver.replace_value 'hello'

78
79
80
# File 'lib/appium_lib_core/patch.rb', line 78

def replace_value(*value)
  @bridge.replace_value(self, *value)
end

#respond_to_missing?Boolean

Returns:

  • (Boolean)

55
56
57
# File 'lib/appium_lib_core/patch.rb', line 55

def respond_to_missing?(*)
  true
end

#save_screenshot(png_path) ⇒ File

Save an element screenshot to the given path

Examples:


element.save_screenshot("fine_name.png")

Parameters:

  • png_path (String)

    A path to save the screenshot

Returns:

  • (File)

    Path to the element screenshot.


147
148
149
150
151
152
153
154
# File 'lib/appium_lib_core/patch.rb', line 147

def save_screenshot(png_path)
  extension = File.extname(png_path).downcase
  if extension != '.png'
    ::Appium::Logger.warn 'name used for saved screenshot does not match file type. '\
                          'It should end with .png extension'
  end
  File.open(png_path, 'wb') { |f| f << screenshot_as(:png) }
end

#screenshotObject

Return an element screenshot as base64

Examples:


element.screenshot #=> "iVBORw0KGgoAAAANSUhEUgAABDgAAAB+CAIAAABOPDa6AAAAAX"

Returns:

  • String Base 64 encoded string


114
115
116
# File 'lib/appium_lib_core/patch.rb', line 114

def screenshot
  bridge.take_element_screenshot(self)
end

#screenshot_as(format) ⇒ Object

Return an element screenshot in the given format

Examples:


element.screenshot_as :base64 #=> "iVBORw0KGgoAAAANSUhEUgAABDgAAAB+CAIAAABOPDa6AAAAAX"

Parameters:

  • format (:base64, :png)

Returns:

  • String screenshot


127
128
129
130
131
132
133
134
135
136
# File 'lib/appium_lib_core/patch.rb', line 127

def screenshot_as(format)
  case format
  when :base64
    bridge.take_element_screenshot(self)
  when :png
    bridge.take_element_screenshot(self).unpack('m')[0]
  else
    raise Core::Error::UnsupportedOperationError, "unsupported format: #{format.inspect}"
  end
end