Class: Appium::Core::Element

Inherits:
Selenium::WebDriver::Element
  • Object
show all
Includes:
Base::TakesScreenshot
Defined in:
lib/appium_lib_core/element.rb

Overview

Implement useful features for element. Patch for Selenium Webdriver.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Base::TakesScreenshot

#element_screenshot_as, #save_element_screenshot, #save_viewport_screenshot

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)


56
57
58
59
60
61
# File 'lib/appium_lib_core/element.rb', line 56

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 Attribute Details

#idString (readonly)

Retuns the element id.

Examples:

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

Returns:

  • (String)


33
34
35
# File 'lib/appium_lib_core/element.rb', line 33

def id
  @id
end

Instance Method Details

#location_rel(driver) ⇒ Struct(:x, :y)

Deprecated.

Please use ‘Element#rect` instead to get location information.

For use with location_rel.

Examples:


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

Returns:

  • (Struct(:x, :y))

    the relative x, y in a struct in string.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/appium_lib_core/element.rb', line 81

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
  point = Struct.new(:x, :y)
  point.new("#{center_x} / #{w.width.to_f}", "#{center_y} / #{w.height.to_f}")
end

#respond_to_missing?(_method_name, _include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/appium_lib_core/element.rb', line 63

def respond_to_missing?(_method_name, _include_private = false)
  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.



138
139
140
141
142
143
144
145
# File 'lib/appium_lib_core/element.rb', line 138

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



105
106
107
# File 'lib/appium_lib_core/element.rb', line 105

def screenshot
  bridge.element_screenshot @id
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



118
119
120
121
122
123
124
125
126
127
# File 'lib/appium_lib_core/element.rb', line 118

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