Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use method missing for getting attributes #116

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ All notable changes to this project will be documented in this file.
## [Unreleased]
### Enhancements
- silence warning for pointeractions [#113](/~https://github.com/appium/ruby_lib_core/pull/113)
- Use method missing to get attributes like `e.resource_id` instead of `e.attribute 'resource-id'` [#116](/~https://github.com/appium/ruby_lib_core/pull/116)

### Bug fixes

Expand Down
46 changes: 16 additions & 30 deletions lib/appium_lib_core/patch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,47 +7,33 @@ class Selenium::WebDriver::Element
# To extend Appium related SearchContext into ::Selenium::WebDriver::Element
include ::Appium::Core::Base::SearchContext

# Note: For testing .text should be used over value, and name.

# Returns the value attribute
#
# Fixes NoMethodError: undefined method `value' for Selenium::WebDriver::Element for iOS
# @return [String]
#
# @example
# Returns the value of attributes like below. Read each platform to know more details.
#
# e = @driver.find_element :accessibility_id, 'something'
# e.value
# uiautomator2: /~https://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
#
def value
attribute :value
end

# Returns the name attribute
# 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
#
# Fixes NoMethodError: undefined method `name' for Selenium::WebDriver::Element for iOS
# @return [String]
#
# @example
#
# e = @driver.find_element :accessibility_id, 'something'
# e.name
# e.value
# e.resource_id # call `e.attribute "resource-id"`
#
def name
attribute :name
def method_missing(method_name)
respond_to?(method_name) ? attribute(method_name.to_s.tr('_', '-')) : super
end

# Enable access to iOS accessibility label
# accessibility identifier is supported as 'name'
# @return [String]
#
# @example
#
# e = @driver.find_element :accessibility_id, 'something'
# e.label
#
def label
attribute :label
def respond_to_missing?(*)
true
end

# Alias for type
Expand Down
22 changes: 5 additions & 17 deletions test/functional/android/patch_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,13 @@ def teardown
save_reports(@@driver)
end

def test_value
skip "Android doesn't support"
def test_method_missing_attributes
e = @@core.wait { @@driver.find_element :accessibility_id, 'App' }

assert_equal 'App', e.value
end

def test_name
skip "Android doesn't support"
e = @@core.wait { @@driver.find_element :accessibility_id, 'App' }

assert_equal 'App', e.name
end

def test_label
skip "Android doesn't support"
e = @@core.wait { @@driver.find_element :accessibility_id, 'App' }

assert_equal 'App', e.label
assert_equal 'App', e.text
assert_equal 'App', e.enabled
assert_equal 'App', e.focused
assert_equal 'App', e.content_desc
end

def test_type
Expand Down
12 changes: 1 addition & 11 deletions test/functional/ios/patch_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,11 @@ def teardown
save_reports(@@driver)
end

def test_value
def test_method_missing_attributes
e = @@core.wait { @@driver.find_element :accessibility_id, 'Buttons' }

assert_equal 'Buttons', e.value
end

def test_name
e = @@core.wait { @@driver.find_element :accessibility_id, 'Buttons' }

assert_equal 'Buttons', e.name
end

def test_label
e = @@core.wait { @@driver.find_element :accessibility_id, 'Buttons' }

assert_equal 'Buttons', e.label
end

Expand Down
10 changes: 10 additions & 0 deletions test/unit/android/device/w3c/commands_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -817,6 +817,16 @@ def test_search_element_child_element
assert_requested(:post, "#{SESSION}/element", times: 1)
assert_requested(:post, "#{SESSION}/element/element_id_parent/element", times: 1)
end

def test_method_missing
stub_request(:get, "#{SESSION}/element/id/attribute/content-desc")
.to_return(headers: HEADER, status: 200, body: { value: '' }.to_json)

e = ::Selenium::WebDriver::Element.new(@driver.send(:bridge), 'id')
e.content_desc

assert_requested(:get, "#{SESSION}/element/id/attribute/content-desc", times: 1)
end
end # class CommandsTest
end # module W3C
end # module Device
Expand Down
10 changes: 10 additions & 0 deletions test/unit/ios/device/w3c/commands_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,16 @@ def test_get_battery_info
assert_equal :unplugged, info[:state]
assert_equal 0.5, info[:level]
end

def test_method_missing
stub_request(:get, "#{SESSION}/element/id/attribute/name")
.to_return(headers: HEADER, status: 200, body: { value: '' }.to_json)

e = ::Selenium::WebDriver::Element.new(@driver.send(:bridge), 'id')
e.name

assert_requested(:get, "#{SESSION}/element/id/attribute/name", times: 1)
end
end # class CommandsTest
end # module W3C
end # module Device
Expand Down