我使用robotium来测试应用程序。在我的测试类中有2个测试:
public class MainActivityTestAll extends ActivityInstrumentationTestCase2<MainActivity> {
private Solo solo;
private static TestItem item;
private boolean isTimeOut;
private ArrayList<Button> arrButton;
private static final String USER_NAME = "varick";
private static final String PASSWORD = "123456";
private static final String BTN_DISMISS = "Dismiss";
private static final String BTN_OK = "OK";
private static final String BTN_TRYAGAIN = "Try Again";
private static final String BTN_CANCEL = "Cancel";
private static final String BTN_GETSTARTED = "Get Started";
private static final String BTN_EXIT = "Exit";
private static EditText edtName, edtPass;
private View inflaterView;
private Button btnLight, btnSwitch, btnOutlet;
public MainActivityTestAll(String name) {
super(MainActivity.class);
setName(name);
}
public void setUp() throws Exception {
solo = new Solo(getInstrumentation(), getActivity());
edtName = (EditText) solo.getView(R.id.edittext_userName);
edtPass = (EditText) solo.getView(R.id.edittext_passWord);
LayoutInflater i = (LayoutInflater) getActivity().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
inflaterView = i.inflate(R.layout.consumer_welcome, null);
isTimeOut = true;
item = new TestItem();
}
@MediumTest
public void test1_DoorLock_Click() throws Exception {
Login();
View view = (View) solo.getView(R.id.fragment_holder);
solo.waitForView(view);
solo.clickOnView(view);
solo.waitForActivity(ConsumerSystemMapDetailPhone.class.getName());
btnLight = (Button) solo.getView(R.id.btnLight);
btnSwitch = (Button) solo.getView(R.id.btnSwitch);
btnOutlet = (Button) solo.getView(R.id.btnOutlet);
assertNotNull(btnLight);
assertNotNull(btnSwitch);
assertNotNull(btnOutlet);
solo.clickOnView(btnLight);
solo.clickOnView(btnSwitch);
solo.clickOnView(btnOutlet);
solo.sleep(2000);
arrButton = solo.getCurrentViews(Button.class,
solo.getView(R.id.consumerzoom_main_container));
assertTrue("DoorLock button had not active", clickonDoorlock(arrButton.get(0)));
}
@MediumTest
public void test2_Switch_Click() throws Exception{
/*
btnLight = (Button) solo.getView(R.id.btnLight);
btnOutlet = (Button) solo.getView(R.id.btnOutlet);
assertNotNull(btnLight);
assertNotNull(btnOutlet);
solo.clickOnView(btnLight);
solo.clickOnView(btnOutlet);
*/
btnSwitch = (Button) solo.getView(R.id.btnSwitch);
assertNotNull(btnSwitch);
solo.clickOnView(btnSwitch);
solo.sleep(2000);
clickonSpinner(1, 3);
solo.sleep(1000);
arrButton = solo.getCurrentViews(Button.class, solo.getView(R.id.consumerzoom_main_container));
clickonSwitch(arrButton.get(0), true, item.getbtnLevel2());
clickonSpinner(0, 1);
clickonSpinner(1, 4);
solo.sleep(1000);
arrButton = solo.getCurrentViews(Button.class, solo.getView(R.id.consumerzoom_main_container));
clickonSwitchInfortop(arrButton.get(0), false, item.getbtnParentLevel8(), item.getbtnLevel3());
exitApp();
}
我的问题是在test1完成后test2什么都不做。它仍在运行,但什么都不做。当我将test2中的代码放入test1时,运行正常。
我不确定这里有什么问题,但我想原因是因为test2不是从MainActivity开始的。
答案 0 :(得分:2)
我认为这是因为你缺少tearDown方法:
@Override
public void tearDown() throws Exception {
solo.finishOpenedActivities();
}