Robotium for Android - solo.searchText () not working

一曲冷凌霜 提交于 2019-12-10 20:59:04

问题


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!


回答1:


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).




回答2:


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());
}


来源:https://stackoverflow.com/questions/17741680/robotium-for-android-solo-searchtext-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!