![](/img/trans.png)
[英]TestNG @Test and @AfterMethod are not executing and also not take the screenshot
[英]Take a ScreenShot on test failure via TestNG before @AfterMethod (Selenium)
我有帶有測試方法的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
其他測試類中也可以@AfterMethod
,但是在ApplicationTest類中,它需要在ApplicationTest類的@AfterMethod
之后@AfterMethod
屏幕快照,並且它是錯誤的截屏,因為它不是@Test
方法之后的截屏。
如何在ApplicationTest類的@Test
方法之后而不是ApplicationTest
類的@AfterMethod
之后為失敗的測試拍攝正確的屏幕截圖。
因此它必須處於下一個順序(如果我們從@Test開始計數):
- @測試
- 的@AfterMethod
BaseTest
類ApplicationTest
類的@AfterMethod
你為什么不嘗試
@Override
public void onTestFailure(ITestResult result) {
System.out.println("***** Error "+result.getName()+" test has failed *****");
String methodName=result.getName().toString().trim();
takeScreenShot(methodName);
在您的應用程序類本身中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.