Thursday, March 30, 2023

Where Can You Find Free SELENIUM REAL TIME PROJECT Resources

 This project includes the following frameworks and libraries:
  1.  ChromeOptions
  2.  TestNG
  3.  Maven
  4.  Extent Report
  5.  Data Provider Method
  6.  Excel File Read (JXL)
  7.  Send Reports Automatically to Email
  8.  Failed Tests Screenshots
Selenium Code:

package Practice;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.lang.reflect.Method;

import java.text.SimpleDateFormat;

import java.time.Duration;

import java.util.Date;

import java.util.Properties;

import java.util.concurrent.TimeUnit;

import javax.activation.DataHandler;

import javax.activation.DataSource;

import javax.activation.FileDataSource;

import javax.mail.BodyPart;

import javax.mail.Message;

import javax.mail.MessagingException;

import javax.mail.Multipart;

import javax.mail.PasswordAuthentication;

import javax.mail.Session;

import javax.mail.Transport;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeBodyPart;

import javax.mail.internet.MimeMessage;

import javax.mail.internet.MimeMultipart;

import org.apache.commons.io.FileUtils;

import org.openqa.selenium.OutputType;

import org.openqa.selenium.TakesScreenshot;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

import org.openqa.selenium.chrome.ChromeOptions;

import org.testng.ITestResult;

import org.testng.annotations.AfterMethod;

import org.testng.annotations.AfterSuite;

import org.testng.annotations.AfterTest;

import org.testng.annotations.BeforeMethod;

import org.testng.annotations.BeforeTest;

import org.testng.annotations.DataProvider;

import org.testng.annotations.Test;

import com.relevantcodes.extentreports.ExtentReports;

import com.relevantcodes.extentreports.ExtentTest;

import com.relevantcodes.extentreports.LogStatus;

import jxl.Sheet;

import jxl.Workbook;

import jxl.read.biff.BiffException;

public class Selenium_Test {

public WebDriver driver;

public ExtentReports extent;

public ExtentTest extentTest;

@SuppressWarnings("deprecation")

@BeforeMethod

public void setup(Method method) {

ChromeOptions chromeOptions = new ChromeOptions();

chromeOptions.addArguments("--remote-allow-origins=*");

this.driver = new ChromeDriver(chromeOptions);

driver.manage().window().maximize();

driver.manage().deleteAllCookies();

driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(30));

driver.get("https://suriyaparithy.blogspot.com/");

driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

extentTest = extent.startTest(method.getName(), "This is a test for " + method.getName());

}

@BeforeTest(alwaysRun = true)

public void setExtent() {

extent = new ExtentReports("./test-output/Reports/Report.html", true);

extent.addSystemInfo("User Name", "Suriya");

extent.addSystemInfo("Environment", "Automation Testing");

extent.addSystemInfo("Application", "Blog");

extent.addSystemInfo("Test Scenario", "Functionality Testing");

}

public String getScreenshot(WebDriver driver, String screenshotName) throws IOException {

String dateName = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());

TakesScreenshot ts = (TakesScreenshot) driver;

File source = ts.getScreenshotAs(OutputType.FILE);

String destination = System.getProperty("user.dir") + "/FailedTestsScreenshots/" 

+ screenshotName + dateName + ".png";

File finalDestination = new File(destination);

FileUtils.copyFile(source, finalDestination);

return destination;

}

// DATA PROVIDER METHOD in JXL

String[][] data = null;

@DataProvider(name = "loginData")

public String[][] loginDataProvider() throws BiffException, IOException {

data = getExcelData();

return data;

}

public String[][] getExcelData() throws BiffException, IOException {

FileInputStream excel = new FileInputStream("D:\\SUR\\src\\test\\resources\\login.xls");

Workbook workbook = Workbook.getWorkbook(excel);

Sheet sheet = workbook.getSheet(0);

int rowCount = sheet.getRows();

int columnCount = sheet.getColumns();

String testData[][] = new String[rowCount - 1][columnCount];

for (int i = 1; i < rowCount; i++) {

for (int j = 0; j < columnCount; j++) {

testData[i - 1][j] = sheet.getCell(j, i).getContents();

}

}

return testData;

}

@Test(enabled = true, priority = 1)

public void testExcelData() {

System.out.println("suriya");

}

@AfterMethod(alwaysRun = true)

public void Down(ITestResult result) throws IOException {

if (result.getStatus() == ITestResult.FAILURE) {

extentTest.log(LogStatus.FAIL, "TEST CASE FAILED IS " + result.getName());

extentTest.log(LogStatus.FAIL, "TEST CASE FAILED IS " + result.getThrowable());

String screenshotPath = getScreenshot(driver, result.getName());

extentTest.log(LogStatus.FAIL, extentTest.addScreenCapture(screenshotPath));

} else if (result.getStatus() == ITestResult.SKIP) {

extentTest.log(LogStatus.SKIP, "Test Case SKIPPED IS " + result.getName());

} else if (result.getStatus() == ITestResult.SUCCESS) {

extentTest.log(LogStatus.PASS, "Test Case PASSED IS " + result.getName());

}

extent.endTest(extentTest);

// driver.quit();

}

@AfterTest(alwaysRun = true)

public void endReport() {

extent.flush();

// extent.close();

}

@AfterSuite

public void sendEmailReport() {

Properties props = new Properties();

props.put("mail.smtp.host", "smtp.gmail.com");

props.put("mail.smtp.socketFactory.port", "465");

props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

props.put("mail.smtp.auth", "true");

props.put("mail.smtp.port", "465");

Session session = Session.getDefaultInstance(props,

new javax.mail.Authenticator() {

protected PasswordAuthentication getPasswordAuthentication() {

return new PasswordAuthentication("yourgmail@gmail.com", "your app password");

}

});

try {

Message message = new MimeMessage(session);

message.setFrom(new InternetAddress("your@gmail.com"));

message.setRecipients(Message.RecipientType.TO,

InternetAddress.parse("email1@gmail.com, email2@gmail.com"));

message.setSubject("Automation Testing Report");

BodyPart messageBodyPart1 = new MimeBodyPart();

messageBodyPart1.setText("This is testng report");

String[] filenames = { "D:\\SURIYA\\test-output\\emailable-report.html",

"D:\\SURIYA\\test-output\\index.html" };

Multipart multipart = new MimeMultipart();

for (String filename : filenames) {

MimeBodyPart messageBodyPart = new MimeBodyPart();

DataSource source = new FileDataSource(filename);

messageBodyPart.setDataHandler(new DataHandler(source));

messageBodyPart.setFileName(filename);

multipart.addBodyPart(messageBodyPart);

}

message.setContent(multipart);

Transport.send(message);

System.out.println("=====Email Sent=====");

} catch (MessagingException e) {

throw new RuntimeException(e);

}

}

}

No comments:

Post a Comment