Issue
I want to convert string type into int. I am doing automation using espresso.
As per espresso automation syntax for clicking on object, onView(withid(R.id.btn_0).perform(click())
I have json file, where I have stored the object,
"btn_0": {
"ANDROID": "withId=R.id.btn_0"
}
Now, when I read btn_0, i get string value for the btn_0 object and withId() takes int.
Note: Integer.parseInt() will not work.
Can anyone help me with this?
Solution
You can parse a resource name to an integer using the Resources.getIdentifier() method, I have prepared an example for you:
@Test
public void parseResourceIdTest() {
final Context context = InstrumentationRegistry.getTargetContext();
assertEquals(R.id.btn_0, parseResourceId(context, "btn_0"));
}
public static int parseResourceId(@NonNull Context context, @NonNull String string) {
return context.getResources().getIdentifier(string, "id", context.getPackageName());
}
Answered By - Jarosław Wiśniewski
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.