Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays Ruby/Appium:如何通过文本属性从find_元素数组中选择元素_Arrays_Ruby_Find_Appium_Elements - Fatal编程技术网

Arrays Ruby/Appium:如何通过文本属性从find_元素数组中选择元素

Arrays Ruby/Appium:如何通过文本属性从find_元素数组中选择元素,arrays,ruby,find,appium,elements,Arrays,Ruby,Find,Appium,Elements,所以我使用一个通用的I press*button小黄瓜语句来按下按钮。我的问题是整个应用程序中的文本没有标准化 我要做的是使用find_元素来形成一个包含所有按钮元素的数组,以我的小黄瓜输入中的文本为例:“我按下Yes按钮”,使用.casecmp方法忽略find_元素数组中按钮文本的大小写,并比较文本属性和我的小黄瓜输入 以下是我对代码的尝试: Then (/^I press the "([^"]*)" button$/) do |button_text| #assign gherkin inp

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

我要做的是使用find_元素来形成一个包含所有按钮元素的数组,以我的小黄瓜输入中的文本为例:“我按下Yes按钮”,使用.casecmp方法忽略find_元素数组中按钮文本的大小写,并比较文本属性和我的小黄瓜输入

以下是我对代码的尝试:

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如何在该数组中存储元素的文档吗?我甚至可以将元素的文本属性与变量或其他值进行比较吗?非常感谢您能给我的任何帮助。

您使用的索引将包含web元素,而不是您期望的整数。请尝试以下操作:

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
如果您面临进一步的问题,请在评论中告诉我


希望能有帮助

谢谢!对于我的例子,我必须添加无大小写比较来解决我的大小写问题,如下所示:button_array.each do | btn | btn.click if btn.text.casecmp{button_text}==0 end否则这非常适合我的需要,非常感谢!很高兴这能帮上忙,另一个选择,如果你不在乎大小写,如果你能将两者转换成小写或大写并进行比较,下面是你如何做到这一点。。。btn.text.downcase==按钮\u text.downcase乐意帮助!!