Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

android - Iterating through a List and clicking on list items in Robotium

I'm trying to run some automated tests in Robotium by iterating through a list and clicking on each list element to start another activity. I have the code below in my test method:

Code:

      solo.assertCurrentActivity("Wrong activity", MainActivity.class);
      //Clicks on the action bar tab
      solo.clickOnText("Charts"); 


      ArrayList<ListView> list = solo.getCurrentListViews();

      for(int i = 0; i < list.size(); i++) {


         //Clicks on the list item assert that the new activity is started
         solo.clickInList(chartPosition);
         solo.assertCurrentActivity("Json Class", JsonActivity.class);
         //Go back to the list  
         solo.goBack();         


     }

The code above does not click on any list items and the JUnit Test results show that all tests are passed which is very confusing.

Does anyone how to successfully iterate through a list in Robotium?

I've seen another question similar to this but the answer suggests looking at jMock instead which doesn't help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I see a couple of problems:

  1. The code is iterating over all list views in the activity, but it is not doing anything with the current list view.
  2. As the code does not show the value of chartPosition, there is no guarantee, the current list has that many items.
  3. I believe you need to wait for the new activity to load.

Try this:

  for(int i = 0; i < list.size(); i++) {
      assertTrue("There are no listviews in this activity.", list.size() > 0);
      chartPosition = 0;  // just to be safe, point at the first item in the list.
      for(int i = 0; i < list.size(); i++) {
         solo.clickInList(chartPosition, i);  // Note that "i" identifies the ListView

         solo.waitForActivity("name.of.the.expected.activity");

      }
     ...
  }

Disclaimer - This suggestion is based purely on code inspections and Robotium Solo Javadoc: http://www.jarvana.com/jarvana/view/com/jayway/android/robotium/robotium-solo/1.4.0/robotium-solo-1.4.0-javadoc.jar!/com/jayway/android/robotium/solo/Solo.html#clickInList(int)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...