3

I'm having a problem with searchText() function used with Robotium.

I'm searching for this string:

<string name="provisioning_wizard_title_2">Service Activation Wizard (Step 2)</string>

I defined it in string xml, and I'm searching it this way:

Activity act=solo.getCurrentActivity(); 
String string = solo.getString(act.getResources().getIdentifier("provisioning_wizard_title_2", "string", act.getPackageName()));

It fails when I call

assertTrue(solo.searchText(string));

and it fails also if i call:

assertTrue(solo.searchText("Service Activation Wizard (Step 2)"));

even if that is the string I'm seeing in the focused screen activity.

The strange thing is that it works if I use the same string without last character:

assertTrue(solo.searchText("Service Activation Wizard (Step 2"));

and it works if I use

assertTrue(solo.searchText(string.substring(0, string.length()-1)));

I hope someone could help me.

Sorry for my english!

Thank you.

PROBLEM SOLVED I solved problem thanks to Renas Reda (Founder and maintainer of Robotium). The problem is due to regex support in searchText(); some characters will trigger it. Use Pattern.quote(string) Example: Pattern.qoute("provisioning_wizard_title_2") and eveything works good!

Flavio Capaccio
  • 706
  • 4
  • 15

2 Answers2

2

I usually use Solo#waitForText(...) so I make sure I'm not losing some race condition.

Try this:

assertTrue(solo.waitForText( solo.getString(R.string. provisioning_wizard_title_2) );

Make sure you are importing the correct R.java file from your production project (not the test project).

James McCracken
  • 15,488
  • 5
  • 54
  • 62
  • I'm making a black box text so I don't have R.java but I know from documentation the id of string declared in strings.xml so I can reference to them with code I use above. I solved the problem using Pattern.quote(string). Thanks for help! – Flavio Capaccio Jul 19 '13 at 23:27
0

I'm not sure, what string you are fetching. Try to log it:

Log.d("Debug", "String is: " + string);

you should rather call, here may be another issue related to package as activity may be in another package than main package of application:

String string = solo.getString(act.getResources().getIdentifier(act.getPackageName()+":string/provisioning_wizard_title_2"), null, null));  

You can also get all visible texts, to make sure, what is on the screen:

for(TextView textView : solo.getCurrentViews(TextView.class)) {
    Log.d("DEBUG", "Text on the screen: " + textView.getText().toString());
}
maszter
  • 3,680
  • 6
  • 37
  • 53
  • I used Log class to check if the string I fetch was correct and it was; then I discovered problem is due to regex support in searchText() . I use Pattern.quote(string) example: Pattern.qoute("provisioning_wizard_title_2") and everything works good! – Flavio Capaccio Jul 19 '13 at 23:25