App flash out when running multiple test classes

When running test cases below, sometimes app tends to flash out halfway through running test cases. However if the test case is run one by one, it is less likely to occur. Any idea what is the reason for it?

@RunWith(SerenityRunner.class)
@CucumberOptions(features=“features”,glue={“UserSteps”})
public class LoginTest {

private static AndroidDriver<AndroidElement> driver;
private static AppiumDriverLocalService service;

@Before
public void setUp() throws MalformedURLException,AppiumServerHasNotBeenStartedLocallyException {
    service = AppiumDriverLocalService.buildDefaultService();
    service.start();

    DesiredCapabilities dc = new DesiredCapabilities();

    dc.setCapability(MobileCapabilityType.DEVICE_NAME, CommonInfo.devName);
    dc.setCapability("platformName", CommonInfo.platfName);
    dc.setCapability("platformVersion", CommonInfo.platfVer);

    dc.setCapability("appPackage", CommonInfo.AppPackage);
    dc.setCapability("appActivity", CommonInfo.AppActivity);
    dc.setCapability("newCommandTimeout", 500);

    AndroidDriver<AndroidElement> driver = new AndroidDriver<AndroidElement>(new URL(CommonInfo.URL), dc);
}

@Steps
UserSteps userSteps = new UserSteps();

@Test
public void LoginwithValidOTP() throws MalformedURLException, InterruptedException {
    String Mobile = "97584761";
    String validOTP = "1234";
    User UserLogin = new User()
            .setMobile(Mobile)
            .setOTP(validOTP);
    userSteps.EnterMobileNo(UserLogin);
    userSteps.EnterOTP(UserLogin);
    userSteps.AllowLocation();
}


@Test
public void LoginwithInvalidOTP() throws MalformedURLException, InterruptedException{
    String Mobile = "84594455";
    String invalidOTP = "4321";
    User UserLogin = new User()
            .setMobile(Mobile)
            .setOTP(invalidOTP);
    userSteps.EnterMobileNo(UserLogin);
    userSteps.InvalidOTP(UserLogin);
    userSteps.VerifyInvalidOTP();
}

@Test
public void LoginwithInvalidMobile() throws MalformedURLException, InterruptedException {
    String Mobile = "77778888";
    String invalidOTP = "1234";
    User UserLogin = new User()
            .setMobile(Mobile)
            .setOTP(invalidOTP);
    userSteps.EnterMobileNo(UserLogin);
}

@AfterClass
public static void afterClass() {
    if (driver != null) {
        driver.quit();
    }
    if (service != null) {
        service.stop();
    }
}

}

this should be done once before all tests.

this should be done after each test NOT after all test completed.

in general logic:

  • start appium servers
  • run tests with driver start and quit in EACH test
  • stop appium servers

It works, thank you so much!!