2

I am using calabash-android to test my app. In its pre-defined steps it has a bunch of "wait" steps, but it only has "Then I wait for view with id to appear".

I need to wait for a string R.string.final_result to appear.

I am wondering, how can I wait for a string with id to appear? I need to wait for the id of string resource ,because the string could be localized in my app.

If it is impossible to do with pre-defined steps, how can I create a step to do it?

Leem.fin
  • 40,781
  • 83
  • 202
  • 354
  • did you try `Then I wait for "final_result" to appear`? – Blackbelt Aug 31 '16 at 07:45
  • @Blackbelt, Like I said in my question, the string I am waiting for can be localized to other language, the `Then I wait for "final_result" to appear` is waiting for the text "final_result" to appear, I need to wait for the string id to appear, not the string text. – Leem.fin Aug 31 '16 at 08:02
  • I recommend you to try this library: https://github.com/mauriciotogneri/green-coffee It's based on Espresso, so tests run faster than using Calabash. Also, you will not have synchronization issues. – Mauricio Togneri Feb 04 '17 at 09:38

2 Answers2

1

fin You can wait for the element first using wait_poll method. and once it appears you can get its text value. As that string would be as one the attribute value of the element. check out the code of .feature file

Then I should wait for the text Final Result

and code behind in your .rb file,

Then(/^I should wait for the text Final Result$/) do
#this wait_poll method will wait untill element with specified id exists.
wait_poll(:until_exists => "* marked:'#{id}'", :timeout => 20)do
#code, you can run while waiting
end
end
nimesh
  • 83
  • 1
  • 8
0

Example of the step. You can substitute the * with whatever class you're using for the label.

Then(/^I wait for text with id "(.*?)" to appear$/) do |id|
    wait_for_element_exists(“* id:’#{key}’”)
end

And then just use it in step definition:

Then I wait for text with id "final_result" to appear

Also, if you do not like the idea of id's, you can create a method that will return the string itself, based on the language, and key you pass to it, and you can deal with localization that way. For example a rough skeleton with xml strings file

def get_string_from_strings_file(key, language)
    file_path = "#{PATH_TO_STRINGS_DIR}/#{language}"
    xml_string = File.open(file_path)
    s = string_xml.xpath("//string[@name='#{key}']/text()")
    raise 'No string found' if s == nil
    s
end

And the check for text in the step.

Krumons
  • 61
  • 7
  • Thanks, I will have a try. By the way, where can i find all the available functions like `wait_for_element_exists(...)` for creating custom steps? – Leem.fin Aug 31 '16 at 14:59
  • I tried your solution, I get this error : `undefined local variable or method `key' for # (NameError)` – Leem.fin Aug 31 '16 at 17:51
  • Yeah, sorry, my mistake. You should use the same variable in the step definition. Also, your strings file might be in a different format, so in `get_string_from_strings_file`, you can use whatever you like to get the info you need. This is just an example – Krumons Sep 01 '16 at 12:18