在@AfterMethod(Selenium)之前通过TestNG进行ScreenShot测试失败

时间:2016-08-29 15:06:33

标签: java selenium testng screenshot

我有ApplicationTest类和测试方法,我有测试,@BeforeMethod@AfterMethod,分别包含初始化和清理。

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() {

}

}

它介绍了BaseTest类,我编写了关于失败测试截图的逻辑:

    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);
        }
    }
}

以下是此方法的实现:

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 + " ]");
}

在没有@AfterMethod的其他测试类中它可以正常工作,但是在ApplicationTest类中,它在ApplicationTest类的@AfterMethod之后截取屏幕截图并且它是错误的屏幕截图,因为它不是{}之后的屏幕截图{1}}方法。

如何在ApplicationTest类中的@Test方法之后对失败的测试进行正确的屏幕截图,而不是在@Test类的@AfterMethod之后。

  

所以必须按照下一个顺序(如果我们开始使用@Test计算):

     
      
  1. @Test
  2.   {li> ApplicationTest类的@AfterMethod   {li> BaseTest类的@AfterMethod   

1 个答案:

答案 0 :(得分:0)

为什么不试试

{{1}}

在您的应用程序类本身。