Ruby/Appium:如何通过文本属性 find_elements 数组中的 select 元素

Ruby/Appium: How to select element from find_elements array via text attribute

所以我使用通用的 "I press "*"button" Gherkin 语句来按下按钮。我的问题是整个应用程序中的文本没有标准化。

我想做的是用find_elements组成所有按钮元素的数组,从我的Gherkin输入中获取文本(例如:'I press the "Yes" button'),利用.casecmp方法来忽略 find_elements 数组中我的按钮文本的大写,并比较文本属性和我的 Gherkin 输入。

这是我对代码的尝试:

Then (/^I press the "([^"]*)" button$/) do |button_text|
#assign gherkin input to variable
@button_text = button_text
#create find_elements array for all Buttons
button_array = find_elements(xpath: "//android.widget.Button")
#create for loop that will compare each element's text with @button_text
  button_array.each do |index|
   #Attempting to reference text attribute of array at index and compare @button_text with case insensitive comparison

   matching_button = button_array[index].text.casecmp("#{@button_text}")
     if matching_button = 0 #this means it's a match
      button_array[index].click()
     else
     end 
  end
end

目前我收到以下错误:

And I press the "YES" button                  # features/step_definitions_android/common_steps.rb:107
      no implicit conversion of Selenium::WebDriver::Element into Integer (TypeError)
      ./features/step_definitions_android/common_steps.rb:113:in `[]'
      ./features/step_definitions_android/common_steps.rb:113:in `block (2 levels) in <top (required)>'
      ./features/step_definitions_android/common_steps.rb:111:in `each'
      ./features/step_definitions_android/common_steps.rb:111:in `/^I press the "([^"]*)" button$/'
      features/FAB.feature:18:in `And I press the "YES" button'

我不完全确定这些错误对我来说意味着什么,但我正在继续我的研究。如果有人可以分享我做错事的见解,我将不胜感激。

还有关于 appium 如何在该数组中存储元素的文档吗?我什至可以将元素的文本属性与变量或其他值进行比较吗?非常感谢你能给我的任何帮助。

您获取的索引将包含网络元素,而不是您期望的整数。尝试以下操作:

Then (/^I press the "([^"]*)" button$/) do |button_text|
  button_array = find_elements(xpath: "//android.widget.Button")
  button_array.each do |btn|
     btn.click if btn.text == button_text
  end
end

如果您遇到进一步的问题,请在评论中告诉我。

希望对您有所帮助!!