Jesse Fish Jesse Fish - 1 year ago 49
Ruby Question

Check if an element is displayed and perform logic accordingly

I'm using Appium with the Cucumber Framework and utilizing the Ruby language.

My scenario is my app has multiple buttons that I want to call with a single Gherkin statement "I press the "*" button" where * is modular. I use it for Save, Back, Cancel, Expand, Yes, No, etc buttons with simple text. Here's an example:

And I press the "Create a Note" FAB button
And I am on the Create a Note Screen
Then I tap Back
>> And I press the "Yes" button


Now currently with my app on android there are multiple versions of the "Back" button and they're not all formatted the same. Some have texts that reads "BACK", some are "Back", and some are "back". I designed a series of if statements to handle this discrepancy until I can get the developers to standardize the text they use for these buttons:

Then(/^I press the "([^"]*)" button$/) do |button_text|
sleep 1
@button_text = button_text
binding.pry
if find_element(xpath: "//android.widget.Button[@text='#{@button_text}']").displayed? == true
find_element(xpath: "//android.widget.Button[@text='#{@button_text}']").click()
else
@button_text.capitalize()
if find_element(xpath: "//android.widget.Button[@text='#{@button_text}']").displayed? == true
find_element(xpath: "//android.widget.Button[@text='#{@button_text}']").click()
else
@button_text.upcase()
if find_element(xpath: "//android.widget.Button[@text='#{@button_text}']").displayed? == true
find_element(xpath: "//android.widget.Button[@text='#{@button_text}']").click()
else
@button_text.downcase()
if find_element(xpath: "//android.widget.Button[@text='#{@button_text}']").displayed? == true
find_element(xpath: "//android.widget.Button[@text='#{@button_text}']").click()
else
fail("Could not find a permutation of that button")
end
end
end
end
sleep 2
end


My thinking is that I'll save the button_text argument to a variable, @button_text, and then use the .displayed? method to check if that version of the button is available. If it is I pass the .click() method, and if it isn't I alter the text of the variable via the .capitalize, .upcase, and .downcase methods. I'm sure that using nested if statements isn't the most ideal way to do this so I would love some assistance on a better way to write that statement.

The issues I receive is I get "An element could not be located using the given search parameters" error whenever I get to this line of code. It seems like the .displayed? method doesn't return a value and triggers appium to stop the test.

I want appium to check if that version of the button is present, then adjust the variable and check the new version for upcase, downcase, and capitalize. Any and all assistance would be greatly appreciated.

Answer Source

When you're finding the elements, try setting their text to all uppercase, and then checking for one version of the word that you're searching for.

I.e. BacK or back or BACK would all become "BACK"

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download