Trying to take a screenshot when the test fails. What is actually happening is once intellij gets to my @AfterMethod it launches the application again and takes a screenshot of the home screen.
I have tried putting the extent.flush(); into a @AfterMethod and changing the current @AfterMethod to an @AfterTest
@AfterMethod
public synchronized void afterMethod(ITestResult result) throws IOException {
AppiumDriver<MobileElement> driver = MetricellTest.setupTests();
String screenShot = CaptureScreenShot.captureScreen(driver, CaptureScreenShot.generateFileName(result));
if (result.getStatus() == ITestResult.FAILURE) {
test.get().log(Status.FAIL, result.getName());
test.get().log(Status.FAIL, result.getThrowable());
test.get().fail("Screen Shot : " + test.get().addScreenCaptureFromPath(screenShot));
test.get().fail(result.getThrowable());
} else if (result.getStatus() == ITestResult.SKIP) {
test.get().skip("Test Case : " + result.getName() + " has been skipped");
test.get().skip(result.getThrowable());
} else
test.get().pass("Test passed");
extent.flush();
}
}
I expect it to take screenshots as it goes through and tests fail. Currently it just opens the application at the end of the test and takes a screenshot of the home screen.
In the @AfterMethod
, you are initialising the driver
again and calling the setupTests
method and your setupTests
method is doing the initialisation of the app, because of which the app is getting opened again.
So, you need to make the following changes in your code and it would work fine then:
Declare the AppiumDriver<MobileElement> driver
globally instead of
declaring it in the setupTests
method, so that it can be used
throughout the class.
Remove the line of code AppiumDriver<MobileElement> driver =
MetricellTest.setupTests();
from your @AfterMethod
because it is
initialising the driver
and the app again.
Updated Answer and adding extra explanation:
You can declare the driver
globally like:
So, lets say your class name is testClass
, you should declare the driver
just after the code for the class is starting like:
public class testClass{
AppiumDriver<MobileElement> driver;
// Add Rest of the code here
}