title: Take a ScreenShot on test failure via TestNG before @AfterMethod (Selenium)
body: <p>I have ApplicationTest class with tests methods where I have tests and <code>@BeforeMethod</code> and <code>@AfterMethod</code>, that contains initialization and cleanup respectively. </p>
  
  <pre><code>public class ApplicationTest extends BaseTest {
  
  ....
  
  @BeforeMethod(alwaysRun = true)
  public void openHomePage() {
  
      ......
  
      headerPage.openMyApplicationsPage();
      myAppPage.openAddNewAppPage();
      newAppPage.createApp(appWithoutImages);
  
      headerPage.openMyApplicationsPage();
      myAppPage.openAddNewAppPage();
      newAppPage.createApp(appWithImages);
  
  
  }
  
  @AfterMethod(alwaysRun = true)
  public void cleanUp() {
      headerPage.openMyApplicationsPage();
      if (myAppPage.isApplicationPresent(appWithoutImages.getTitle())){
          myAppPage.openApp(appWithoutImages.getTitle());
          appPage.deleteApp();
      }
      headerPage.openMyApplicationsPage();
      if (myAppPage.isApplicationPresent(appWithImages.getTitle())){
          myAppPage.openApp(appWithImages.getTitle());
          appPage.deleteApp();
      }
      driver.manage().deleteAllCookies();
  }
  
  @Test
  public void correctInformationAboutApplicationTest() {
  .....
  }
  
  @Test
  public void testAppCreationWithoutImages() {
  ....
  }
  
  @Test
  public void testAppEditing() {
  
  }
  
  }
  </code></pre>
  
  <p>It exstends of BaseTest class where I wrote logic of taking screenshot of failed tests:</p>
  
  <pre><code>    public class BaseTest {
      private static Settings settings = new Settings();
  
      @BeforeSuite(alwaysRun = true)
      public static void beforeSuite() {
          driver = settings.getDriver();
  
          BasePage.settings = settings;
  
          driver.get(settings.getBaseUrl());
  
          if (!settings.getBrowser().equals(BrowserType.HTMLUNIT))
              driver.manage().window().maximize();
      }
  
      @AfterSuite(alwaysRun = true)
      public static void afterClass() {
          driver.close();
      }
  
      @AfterMethod
      public void takeScreenshotWhenFailure(ITestResult result) {
          String testDate = getCurrentDateAndTimeInSting();
          if (ITestResult.FAILURE == result.getStatus()) {
              captureScreenshot(result.getName() + " - " + testDate);
          }
      }
  }
  </code></pre>
  
  <p>Here is implementation of this method:</p>
  
  <pre><code>public static void captureScreenshot(String screenshotName) {
  
      String pathToScreenshotDirectory = PATH_TO_SCREENSHOTS + " - " + TEST_DATE_FOR_PACKAGE;
  
      try {
          createDirectory(pathToScreenshotDirectory);
          TakesScreenshot ts = (TakesScreenshot) driver;
          File screenshot = ts.getScreenshotAs(OutputType.FILE);
          FileUtils.copyFile(screenshot, new File(pathToScreenshotDirectory + "\\" + screenshotName + ".png"));
  
      } catch (IOException e) {
          e.printStackTrace();
      }
      log.info("Screenshot taken: [ " + screenshotName + " ]");
  }
  </code></pre>
  
  <p>It works ok in other test classes where there is no <code>@AfterMethod</code>, but in ApplicationTest class it takes screenshot after <code>@AfterMethod</code> of ApplicationTest class and it is wrong screenshot, because it is not a screenshot  after <code>@Test</code> method.</p>
  
  <p>How can I take a correct screenshot for failed test after <code>@Test</code> method in ApplicationTest class, but not after <code>@AfterMethod</code> of <code>ApplicationTest</code> class.</p>
  
  <blockquote>
    <p>So it have to be in next order (if we start count with @Test):</p>
    
    <ol>
    <li><strong>@Test</strong></li>
    <li><strong>@AfterMethod</strong> of <code>BaseTest</code> class</li>
    <li><strong>@AfterMethod</strong> of <code>ApplicationTest</code> class</li>
    </ol>
  </blockquote>