How do I click the first item in a spinner using Robotium?

ぐ巨炮叔叔 提交于 2019-12-04 05:27:27

问题


I am having problems scrolling up in a spinner to select the first item in a Robotium test case. Here is my code:

int pos = solo.getCurrentSpinners().get(0).getSelectedItemPosition();
solo.pressSpinnerItem(0, 0 - pos);

pos is 1 when I debug, but Robotium still presses the spinner on index 1 even though I order it to press on -1. What am I doing wrong?

Thanks Markus


回答1:


Seems they took those classes out now. Just ran into this myself but found a way to do this properly and generically.

// 0 is the first spinner in the layout
View view1 = solo.getView(Spinner.class, 0);
solo.clickOnView(view1);
solo.scrollToTop(); // I put this in here so that it always keeps the list at start
// select the 10th item in the spinner
solo.clickOnView(solo.getView(TextView.class, 10)); 



回答2:


The API to use here with Robotium is rather flaky, so I decided to go down the direct API route:

instrumentation.runOnMainSync(new Runnable() {
    @Override
    public void run() {
        Spinner spinner = (Spinner) solo.getView(resourceId);
        spinner.setSelection(position, true);
    }
});

This won't show you the popup of the Spinner, but it will select the desired item.




回答3:


are you able just to get the view and call the perform click on it?

solo.getCurrentSpinners().get(0).performClick()


来源:https://stackoverflow.com/questions/10921036/how-do-i-click-the-first-item-in-a-spinner-using-robotium

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