简体   繁体   English

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

[英]Take a ScreenShot on test failure via TestNG before @AfterMethod (Selenium)

I have ApplicationTest class with tests methods where I have tests and @BeforeMethod and @AfterMethod , that contains initialization and cleanup respectively. 我有带有测试方法的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() {

}

}

It exstends of BaseTest class where I wrote logic of taking screenshot of failed tests: 它扩展了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);
        }
    }
}

Here is implementation of this method: 这是此方法的实现:

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

It works ok in other test classes where there is no @AfterMethod , but in ApplicationTest class it takes screenshot after @AfterMethod of ApplicationTest class and it is wrong screenshot, because it is not a screenshot after @Test method. 它在没有@AfterMethod其他测试类中也可以@AfterMethod ,但是在ApplicationTest类中,它需要在ApplicationTest类的@AfterMethod之后@AfterMethod屏幕快照,并且它是错误的截屏,因为它不是@Test方法之后的截屏。

How can I take a correct screenshot for failed test after @Test method in ApplicationTest class, but not after @AfterMethod of ApplicationTest class. 如何在ApplicationTest类的@Test方法之后而不是ApplicationTest类的@AfterMethod之后为失败的测试拍摄正确的屏幕截图。

So it have to be in next order (if we start count with @Test): 因此它必须处于下一个顺序(如果我们从@Test开始计数):

  1. @Test @测试
  2. @AfterMethod of BaseTest class @AfterMethod BaseTest
  3. @AfterMethod of ApplicationTest class ApplicationTest类的@AfterMethod

why dont you try 你为什么不尝试

 @Override
    public void onTestFailure(ITestResult result) {
        System.out.println("***** Error "+result.getName()+" test has failed *****");
        String methodName=result.getName().toString().trim();
        takeScreenShot(methodName);

in your application class itself. 在您的应用程序类本身中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM