Skip to content

Commit c4cdc9f

Browse files
committed
added code to take screenshot after navigating to registration page
1 parent 245bd77 commit c4cdc9f

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

src/test/java/io/github/mfaisalkhatri/pages/RegistrationPage.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
package io.github.mfaisalkhatri.pages;
22

3-
import java.text.DateFormat;
43
import java.time.Duration;
5-
import java.time.format.DateTimeFormatter;
6-
import java.util.Date;
7-
import java.util.Locale;
84

95
import io.github.mfaisalkhatri.data.RegistrationData;
106
import org.openqa.selenium.By;

src/test/java/io/github/mfaisalkhatri/test/JsonDataProviderTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,21 @@
22

33
import static org.testng.Assert.assertEquals;
44

5+
import java.io.File;
6+
import java.io.IOException;
7+
import java.nio.file.Files;
8+
import java.nio.file.Paths;
9+
import java.text.SimpleDateFormat;
10+
import java.util.Date;
511
import java.util.Iterator;
612

713
import io.github.mfaisalkhatri.data.RegistrationData;
814
import io.github.mfaisalkhatri.data.RegistrationDataBuilder;
915
import io.github.mfaisalkhatri.pages.HomePage;
1016
import io.github.mfaisalkhatri.pages.LoginPage;
1117
import io.github.mfaisalkhatri.pages.RegistrationPage;
18+
import org.openqa.selenium.OutputType;
19+
import org.openqa.selenium.TakesScreenshot;
1220
import org.testng.annotations.DataProvider;
1321
import org.testng.annotations.Test;
1422

@@ -29,6 +37,17 @@ public void testRegistrationPasswordAlert (final RegistrationData registrationDa
2937
LoginPage loginPage = homePage.navigateToLoginPage ();
3038
RegistrationPage registrationPage = loginPage.navigateToRegistrationPage ();
3139

40+
String timestamp = new SimpleDateFormat ("yyyyMMdd_HHmmss_SSS").format (new Date ());
41+
42+
File screenshot = ((TakesScreenshot) driver).getScreenshotAs (OutputType.FILE);
43+
String filename = timestamp + ".png";
44+
try {
45+
Files.createDirectories (Paths.get ("screenshots"));
46+
Files.copy (screenshot.toPath (), Paths.get ("screenshots", filename));
47+
} catch (IOException e) {
48+
throw new RuntimeException (e);
49+
}
50+
3251
assertEquals (registrationPage.pageHeader (), "Customer registration");
3352

3453
registrationPage.fillRegistrationForm (registrationData);

src/test/java/io/github/mfaisalkhatri/utils/ScreenshotListener.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,14 @@ public void onTestFailure (final ITestResult result) {
2020
WebDriver driver = ((WebDriverProvider) testClass).getDriver ();
2121
String timestamp = new SimpleDateFormat ("yyyyMMdd_HHmmss_SSS").format (new Date ());
2222

23-
2423
if (driver != null) {
2524
String testName = result.getMethod ()
2625
.getMethodName ();
2726
File screenshot = ((TakesScreenshot) driver).getScreenshotAs (OutputType.FILE);
2827
String filename = timestamp + ".png";
2928
try {
3029
Files.createDirectories (Paths.get ("screenshots"));
31-
Files.copy (screenshot.toPath (), Paths.get ("screenshots", testName+"_"+filename));
30+
Files.copy (screenshot.toPath (), Paths.get ("screenshots", testName + "_" + filename));
3231
} catch (IOException e) {
3332
throw new RuntimeException (e);
3433
}

0 commit comments

Comments
 (0)