Question

I am stuck at this problem wherein i require to click on a specific button adjacent to some text in my app.

For eg: Let this be the one of the screens of my app with the following text.

    xxxx-xxxx-xxxx  Button1 Button2
    yyyy-yyyy-yyyy  Button3 Button4

Now i want to click on Button1 if the text i am passing as input matches "xxxx-xxxx-xxxx". And, Button3 if the input text matches "yyyy-yyyy-yyyy".

https://www.dropbox.com/s/1fgr4uz4mm2mfm6/Untitled.png refer to this link to see how it looks like.

Thanks in advance!

Was it helpful?

Solution

I have made an assumption that they are in the same Viewgroup. If this is the case then it is not so bad.

What you need to do is break this down in to a set of problems

1) Find the view, this is pretty easy solo.getText(String text) will return the text view that has that text or getEditText(String text) will work if it is an edit text.

2) find the parent view in common between the view you found and the button to click. you can do this by calling getParent() on the view returned above and casting it to a (ViewGroup)

3) find the text view you want from that view parent, the simplest if they all have the same structure is to use getChildAt(i) on the view group above, where i will probably be 2 from the strucutre you mention. You will need to cast this to a button.

putting this all together it will look soemthing like:

View view = solo.getEditText("xxxx-xxxx-xxxx"); //or solo.getText("xxxx-xxxx-xxxx")
ViewGroup viewGroupContaingTextAndButton = (ViewGroup) view.getParent();
Button button = viewGroupContaingTextAndButton.getChildAt(2);

You should be able to refactor that into a nice easy re-usable method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top