简体   繁体   English

在每个测试用例之后重新启动应用程序

[英]Restart App after every Test Case

I'm using Robotium and ActivityInstrumentationTestCase2 and I need to restart my app completely after every test case. 我正在使用Robotium和ActivityInstrumentationTestCase2,并且在每个测试用例之后都需要完全重新启动应用程序。 The app does not find the view ids in all test cases following the first one. 在第一个测试案例之后的所有测试案例中,该应用程序均未找到视图ID。

That means my view is null in TestCase2 and that doesn't make sense. 这意味着我的视图在TestCase2中为空,这没有任何意义。

The app has a loading screen. 该应用程序有一个加载屏幕。 This loading screen only appears before testCase1, and solo.getCurrentActivity() returns different Activity instances. 该加载屏幕仅出现在testCase1之前,并且solo.getCurrentActivity()返回不同的Activity实例。

public void testCase1() throws Exception {

    solo.getCurrentActivity(); //returns LoadingActivity
    solo.sleep(5000);
    solo.getCurrentActivity(); //returns MainActivity

    View view = (TextView) solo.findViewById("Button1", TIMEOUT_VIEW_SEARCH);
    solo.clickOnView(view);
}

public void testCase2() throws Exception {

    solo.getCurrentActivity(); //returns LoadingActivity
    solo.sleep(5000);
    solo.getCurrentActivity(); //returns LoadingActivity

    View view = (TextView) solo.findViewById("Button1", TIMEOUT_VIEW_SEARCH);
    solo.clickOnView(view);
}

The method findviewById(...) searches for the Id of the view based on current activity. 方法findviewById(...)根据当前活动搜索视图的ID。 This works fine. 这很好。

setUp and tearDown look like the two below. setUp和tearDown看起来像下面的两个。 I'm on the start screen when testCase2 starts, but the app didn't start completely new. 当testCase2启动时,我在启动屏幕上,但该应用程序并未完全启动。

@Override
public void setUp() throws Exception {
    super.setUp();
    solo = new MySolo(getInstrumentation(), getActivity());
}

@Override
public void tearDown() throws Exception {
    solo.finishOpenedActivities();
    super.tearDown();
}

EDIT: I've edited the question. 编辑:我已经编辑了问题。 Both methods are in different Activities. 两种方法处于不同的活动中。

使用solo.getView()而不是findViewById。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM