From 7db8eacb5b888f68c766dc78f24096ce9b930364 Mon Sep 17 00:00:00 2001 From: Kazuaki MATSUO Date: Sat, 28 Jul 2018 13:28:43 +0900 Subject: [PATCH 1/3] use method missing for getting attributes --- lib/appium_lib_core/patch.rb | 46 +++++++------------ test/functional/android/patch_test.rb | 22 ++------- test/functional/ios/patch_test.rb | 12 +---- test/unit/android/device/w3c/commands_test.rb | 10 ++++ test/unit/ios/device/w3c/commands_test.rb | 10 ++++ 5 files changed, 42 insertions(+), 58 deletions(-) diff --git a/lib/appium_lib_core/patch.rb b/lib/appium_lib_core/patch.rb index e27072ff..211058ee 100644 --- a/lib/appium_lib_core/patch.rb +++ b/lib/appium_lib_core/patch.rb @@ -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 # - # 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 support 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 diff --git a/test/functional/android/patch_test.rb b/test/functional/android/patch_test.rb index 38e02a95..12eb919d 100644 --- a/test/functional/android/patch_test.rb +++ b/test/functional/android/patch_test.rb @@ -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 diff --git a/test/functional/ios/patch_test.rb b/test/functional/ios/patch_test.rb index 2345a4d5..1fa64d0c 100644 --- a/test/functional/ios/patch_test.rb +++ b/test/functional/ios/patch_test.rb @@ -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 diff --git a/test/unit/android/device/w3c/commands_test.rb b/test/unit/android/device/w3c/commands_test.rb index 9a854493..3cbdf969 100644 --- a/test/unit/android/device/w3c/commands_test.rb +++ b/test/unit/android/device/w3c/commands_test.rb @@ -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 diff --git a/test/unit/ios/device/w3c/commands_test.rb b/test/unit/ios/device/w3c/commands_test.rb index a74d9d19..1fe40f6c 100644 --- a/test/unit/ios/device/w3c/commands_test.rb +++ b/test/unit/ios/device/w3c/commands_test.rb @@ -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 From 6fa0b948a1adec67b28854165e3245620a9f66f3 Mon Sep 17 00:00:00 2001 From: Kazuaki MATSUO Date: Sat, 28 Jul 2018 13:49:03 +0900 Subject: [PATCH 2/3] update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ca1d44fb..a25eb900 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 From caa35354f5d9e63979f665545d4bbfeedaddb0aa Mon Sep 17 00:00:00 2001 From: Kazuaki MATSUO Date: Sat, 28 Jul 2018 13:54:11 +0900 Subject: [PATCH 3/3] tweak --- lib/appium_lib_core/patch.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/appium_lib_core/patch.rb b/lib/appium_lib_core/patch.rb index 211058ee..26255320 100644 --- a/lib/appium_lib_core/patch.rb +++ b/lib/appium_lib_core/patch.rb @@ -7,14 +7,14 @@ class Selenium::WebDriver::Element # To extend Appium related SearchContext into ::Selenium::WebDriver::Element include ::Appium::Core::Base::SearchContext - # Returns the value of attributes + # Returns the value of attributes like below. Read each platform to know more details. # # 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 # - # XCUITest automation name support below attributes + # 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,