1717import solutions .bellatrix .core .configuration .ConfigurationService ;
1818import solutions .bellatrix .core .plugins .Plugin ;
1919import solutions .bellatrix .core .plugins .TestResult ;
20+ import solutions .bellatrix .core .plugins .TimeRecord ;
2021import solutions .bellatrix .core .utilities .DebugInformation ;
2122import solutions .bellatrix .core .utilities .PathNormalizer ;
2223import solutions .bellatrix .web .configuration .WebSettings ;
@@ -45,8 +46,8 @@ public class AppLifecyclePlugin extends Plugin {
4546 public void preBeforeClass (Class type ) {
4647 if (ConfigurationService .get (WebSettings .class ).getExecutionType () == "regular" ) {
4748 CURRENT_APP_CONFIGURATION .set (getExecutionAppClassLevel (type ));
48- if (shouldRestartApp ()) {
49- restartApp ();
49+ if (shouldReinstallApp ()) {
50+ reinstallApp ();
5051 // TODO: maybe we can simplify and remove this parameter.
5152 IS_APP_STARTED_DURING_PRE_BEFORE_CLASS .set (true );
5253 } else {
@@ -72,33 +73,42 @@ public void preBeforeTest(TestResult testResult, Method memberInfo) {
7273 CURRENT_APP_CONFIGURATION .get ().setTestName (testFullName );
7374
7475 if (!IS_APP_STARTED_DURING_PRE_BEFORE_CLASS .get ()) {
75- if (shouldRestartApp ()) {
76- restartApp ();
76+ if (shouldReinstallApp ()) {
77+ reinstallApp ();
7778 }
7879 }
7980
8081 IS_APP_STARTED_DURING_PRE_BEFORE_CLASS .set (false );
8182 }
8283
8384 @ Override
84- public void postAfterTest (TestResult testResult , Method memberInfo , Throwable failedTestException ) {
85+ public void postAfterTest (TestResult testResult , TimeRecord timeRecord , Method memberInfo , Throwable failedTestException ) {
8586 if (CURRENT_APP_CONFIGURATION .get ().getLifecycle () == Lifecycle .REUSE_IF_STARTED ) {
8687 return ;
8788 }
8889
89- if (CURRENT_APP_CONFIGURATION .get ().getLifecycle () == Lifecycle .RESTART_ON_FAIL && testResult != TestResult .FAILURE ) {
90+ if (CURRENT_APP_CONFIGURATION .get ().getLifecycle () == Lifecycle .RESTART_ON_FAIL && testResult == TestResult .SUCCESS ) {
91+ DriverService .getWrappedAndroidDriver ().terminateApp (CURRENT_APP_CONFIGURATION .get ().getAppPackage ());
92+ DriverService .getWrappedAndroidDriver ().activateApp (CURRENT_APP_CONFIGURATION .get ().getAppPackage ());
9093 return ;
9194 }
9295
9396 shutdownApp ();
9497 }
9598
99+ @ Override
100+ public void beforeTestFailed (Exception ex ) {
101+ if (CURRENT_APP_CONFIGURATION .get ().getLifecycle () == Lifecycle .RESTART_ON_FAIL ) {
102+ shutdownApp ();
103+ }
104+ }
105+
96106 private void shutdownApp () {
97107 DriverService .close ();
98108 PREVIOUS_APP_CONFIGURATION .set (null );
99109 }
100110
101- private void restartApp () {
111+ private void reinstallApp () {
102112 shutdownApp ();
103113 try {
104114 DriverService .start (CURRENT_APP_CONFIGURATION .get ());
@@ -111,7 +121,7 @@ private void restartApp() {
111121 PREVIOUS_APP_CONFIGURATION .set (CURRENT_APP_CONFIGURATION .get ());
112122 }
113123
114- private boolean shouldRestartApp () {
124+ private boolean shouldReinstallApp () {
115125 // TODO: IsAppStartedCorrectly getter?
116126 var previousConfiguration = PREVIOUS_APP_CONFIGURATION .get ();
117127 var currentConfiguration = CURRENT_APP_CONFIGURATION .get ();
0 commit comments