import org.openqa.selenium.
WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
public class LoginTest {
private WebDriver driver;
private Logger logger = Logger.getLogger(LoginTest.class);
private Recorder recorder;
private String reportFolder = "report";
private String loggerFolder = "logger";
private String screenshotFolder = "screenshot";
private String videoFolder = "videos";
@BeforeTest
public void beforeTest() {
driver = new ChromeDriver();
// Initialize the Recorder.
recorder = new Recorder();
recorder.startRecording("login_test.mp4");
// Create the report folder
File reportFolderFile = new File(reportFolder);
if (!reportFolderFile.exists()) {
reportFolderFile.mkdir();
}
// Create the logger folder
File loggerFolderFile = new File(reportFolder + "/" +
loggerFolder);
if (!loggerFolderFile.exists()) {
loggerFolderFile.mkdir();
}
// Create the screenshot folder
File screenshotFolderFile = new File(reportFolder + "/" +
screenshotFolder);
if (!screenshotFolderFile.exists()) {
screenshotFolderFile.mkdir();
}
// Create the video folder
File videoFolderFile = new File(reportFolder + "/" + videoFolder);
if (!videoFolderFile.exists()) {
videoFolderFile.mkdir();
}
}
@Test
public void loginFail() {
driver.get("https://ttv.docsmail.com/Login");
driver.findElementById("txtEmail").sendKeys("[email protected]");
driver.findElementById("txtPassword").sendKeys("123456");
driver.waitUntil(ExpectedConditions.visibilityOfElementLocated(By.id("btnLogin
")));
driver.findElementById("btnLogin").click();
//displays an error message.
String lblPasswordErrText =
driver.findElementById("lblPasswordErr").getText();
logger.info("The login error message is: " + lblPasswordErrText);
Assert.assertEquals(lblPasswordErrText, "The inputted email or
password is not correct.");
// Wait until the error message is displayed.
driver.waitUntil(ExpectedConditions.visibilityOfElementLocated(By.id("lblP
asswordErr")));
// Take a screenshot of the login page.
File screenshotFile = driver.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshotFile, new File(reportFolder + "/" +
screenshotFolder + "/login_fail.png"));
// Stop recording the video.
recorder.stopRecording();
// Save the log to the logger folder.
saveLogToLoggerFolder();
// Save the video to the video folder.
saveVideoToVideoFolder();
}
@Test
public void loginSuccess() {
driver.get("https://ttv.docsmail.com/Login");
driver.findElementById("txtEmail").sendKeys("[email protected]");
driver.findElementById("txtPassword").sendKeys("quan@123");
driver.waitUntil(ExpectedConditions.visibilityOfElementLocated(By.id("btnLogin
")));
driver.findElementById("btnLogin").click();
// logged in successfully message.
String lblLoggedinSuccessfullyText =
driver.findElementById("lblLoggedinSuccessfully").getText();
logger.info("The login success message is: " +
lblLoggedinSuccessfullyText);
Assert.assertEquals(lblLoggedinSuccessfullyText, "You have logged
in successfully!");
// Wait until the success message is displayed.
driver.waitUntil(ExpectedConditions.visibilityOfElementLocated(By.id("lblL
oggedinSuccessfully ")));
// Take a screenshot of the login page.
File screenshotFile = driver.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshotFile, new File(reportFolder + "/" +
screenshotFolder + "/login_success.png"));
// Stop recording the video.
recorder.stopRecording();
// Save the log to the logger folder.
saveLogToLoggerFolder();
// Save the video to the video folder.
saveVideoToVideoFolder();
}
@AfterTest
public void afterTest() {
driver.quit();
private void saveLogToLoggerFolder() {
// Get the log as a string.
String log = logger.getLog();
// Create a file in the logger folder with the current timestamp as
the filename.
File logFile = new File(reportFolder + "/" + loggerFolder + "/" +
String.valueOf(System.currentTimeMillis()) + ".log");
// Write the log to the file.
FileUtils.writeStringToFile(logFile, log);
}
private void saveVideoToVideoFolder() {
// Get the video as a byte array.
byte[] video = recorder.getVideo();
// Create a file in the video folder with the current timestamp as the
filename.
File videoFile = new File(reportFolder + "/" + videoFolder + "/" +
String.valueOf(System.currentTimeMillis()) + ".mp4");
// Write the video to the file.
FileUtils.writeByteArrayToFile(videoFile, video);
}