2017-05-23 76 views
0

所以我使用一般的“I press”*“button”Gherkin语句来按下按钮。我的问题是整个应用程序中的文本不是标准化的。Ruby/Appium:如何通过文本属性从find_elements数组中选择元素

我想要做的是使用find_elements来形成一个所有按钮元素的数组,从我的小黄瓜输入中获取文本(例如:'我按“是”按钮'),利用.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' 

我不能完全确定什么这些错误在我的案件的意思,但我继续我的研究。如果任何人都可以分享见解我做错了,我将不胜感激。

也有任何有关如何在该阵列中存储元素的文档?我甚至可以将元素的文本属性与变量或其他值进行比较?非常感谢您给我提供的任何帮助。

回答

1

您所采用的索引将具有web元素,而不是您所期望的Integer。请尝试以下操作:

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 

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

希望它有帮助!

+0

谢谢!对于我的情况,我不得不添加无案例的比较来解决我的套管问题,如下所示: button_array.each do | btn | btn.click if btn.text.casecmp(“#{button_text}”)== 0 end 否则这对于我所需要的非常完美,谢谢! –

+0

很高兴这可以帮助,另一种选择,如果你不打扰案件,如果你可以转换成较低或大写,并比较,这里是你是如何做到这一点... btn.text.downcase == button_text.downcase 乐于帮助!! –