File tree Expand file tree Collapse file tree
tests/mytests/src/test/java Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -19,11 +19,9 @@ student:
1919# This has no effect on your grade.
2020# ----------------------------------------------------------
2121ai_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
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 55import org .openqa .selenium .*;
66import org .openqa .selenium .chrome .*;
77import org .openqa .selenium .remote .RemoteWebDriver ;
8+ import org .testng .annotations .Listeners ;
9+
810import java .net .URL ;
911import java .util .Map ;
1012import java .net .MalformedURLException ;
1113
1214import pages .LoginPage ;
1315import utils .ConfigReader ;
1416
17+ @ Listeners (utils .TestListener .class )
1518public 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}
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments