Skip to content

Commit f76111e

Browse files
author
Matécsa Dániel
committed
Automatically screenshot taken when a test fails
1 parent 05f44b7 commit f76111e

4 files changed

Lines changed: 85 additions & 6 deletions

File tree

points.yml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,9 @@ student:
1919
# This has no effect on your grade.
2020
# ----------------------------------------------------------
2121
ai_usage:
22-
used: false
23-
tools: "" # e.g. "Claude, GitHub Copilot"
24-
description: "" # e.g. "Used Claude to generate initial Page Object
25-
# classes, then modified them manually. Used Copilot
26-
# for autocomplete while writing test methods."
22+
used: true
23+
tools: "GitHub Copilot, Gemini" # e.g. "Claude, GitHub Copilot"
24+
description: "Copilot was used for autocomplete when writing test methods. Gemini was used to help implement advanced tasks."
2725

2826
# ----------------------------------------------------------
2927
# Quality requirements (gatekeepers)
@@ -192,7 +190,7 @@ advanced:
192190
link: "https://www.selenium.dev/selenium/docs/api/java/org/openqa/selenium/JavascriptExecutor.html"
193191

194192
screenshot_on_failure: # 6 pts | once
195-
done: false
193+
done: true
196194
description: "Automatically take a screenshot when a test fails (e.g. using a TestNG ITestListener)"
197195
link: "https://www.browserstack.com/guide/take-screenshots-in-selenium"
198196

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package tests;
2+
3+
import java.net.MalformedURLException;
4+
5+
import org.testng.annotations.*;
6+
import org.testng.*;
7+
8+
import pages.*;
9+
10+
public class ScreenshotTest extends TestBase {
11+
12+
@BeforeMethod
13+
public void Setup() throws MalformedURLException
14+
{
15+
before();
16+
}
17+
18+
@Test
19+
public void screenshotTest() {
20+
LoginPage loginPage = null;
21+
22+
loginPage = login(loginPage);
23+
24+
MainPage mainPage = loginPage.clickLogin();
25+
26+
Assert.assertTrue(mainPage.getBodyText().contains("This text will surely not be found on the page!"));
27+
28+
}
29+
30+
@AfterMethod
31+
public void close() {
32+
after();
33+
}
34+
35+
}

tests/mytests/src/test/java/tests/TestBase.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
55
import org.openqa.selenium.*;
66
import org.openqa.selenium.chrome.*;
77
import org.openqa.selenium.remote.RemoteWebDriver;
8+
import org.testng.annotations.Listeners;
9+
810
import java.net.URL;
911
import java.util.Map;
1012
import java.net.MalformedURLException;
1113

1214
import pages.LoginPage;
1315
import utils.ConfigReader;
1416

17+
@Listeners(utils.TestListener.class)
1518
public class TestBase {
1619

1720
protected WebDriver driver;
@@ -50,4 +53,7 @@ protected LoginPage login(LoginPage loginPage) {
5053
return loginPage;
5154
}
5255

56+
public WebDriver getDriver() {
57+
return this.driver;
58+
}
5359
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package utils;
2+
3+
import org.openqa.selenium.OutputType;
4+
import org.openqa.selenium.TakesScreenshot;
5+
import org.openqa.selenium.WebDriver;
6+
import org.testng.ITestResult;
7+
import org.testng.TestListenerAdapter;
8+
9+
import java.io.File;
10+
import java.io.IOException;
11+
import java.nio.file.Files;
12+
import java.nio.file.StandardCopyOption;
13+
14+
import tests.TestBase;
15+
16+
public class TestListener extends TestListenerAdapter {
17+
@Override
18+
public void onTestFailure(ITestResult tr) {
19+
String methodName = tr.getMethod().getMethodName();
20+
String fileWithPath = methodName + "_error.png";
21+
22+
Object testClass = tr.getInstance();
23+
WebDriver driver = ((TestBase) testClass).getDriver();
24+
25+
if (driver != null) {
26+
try {
27+
TakesScreenshot scrShot = ((TakesScreenshot) driver);
28+
File SrcFile = scrShot.getScreenshotAs(OutputType.FILE);
29+
File DestFile = new File(fileWithPath);
30+
31+
Files.copy(SrcFile.toPath(), DestFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
32+
System.out.println("Screenshot saved to: " + DestFile.getAbsolutePath());
33+
34+
} catch (IOException e) {
35+
System.out.println("Error: screenshot could not be saved.");
36+
e.printStackTrace();
37+
}
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)