Selenium web driver tests using testng and HTML reports using ExtentReports
We’ll use two simple tests that are executed against the http://relevantcodes.com website. We’ll have ExtentReports generate a HTML report that contain the appropriate test results.
checkLinks1 will succeed where in checkLinks2 fails on lnk_Google
Home_Page.java
package pageObjects;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class Home_Page {
private static WebElement element = null;
public static WebElement lnk_Home(WebDriver driver){
element = driver.findElement(By.xpath("//a[@title='Home']"));
return element;
}
public static WebElement lnk_Articles(WebDriver driver){
element = driver.findElement(By.xpath("//a[contains(.,'Articles')]"));
return element;
}
public static WebElement lnk_Downloads(WebDriver driver){
element = driver.findElement(By.xpath("//a[contains(.,'Downloads')]"));
return element;
}
public static WebElement lnk_Contact(WebDriver driver){
element = driver.findElement(By.xpath("//a[contains(.,'Contact')]"));
return element;
}
public static WebElement lnk_Subscribe(WebDriver driver){
element = driver.findElement(By.xpath("//a[contains(.,'Subscribe')]"));
return element;
}
public static WebElement lnk_Google(WebDriver driver){
element = driver.findElement(By.xpath("//a[contains(.,'Roogle')]"));
return element;
}
}
PageObjectModel.java
package pageObjects;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
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.firefox.FirefoxDriver;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeSuite;
import com.relevantcodes.extentreports.DisplayOrder;
import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.LogStatus;
public class PageObjectModel {
public static WebDriver driver = null;
public static final ExtentReports extent = ExtentReports.get(PageObjectModel.class);
public static String reportLocation = "reports/";
public static String imageLocation = "images/";
public static File lDir = new File("");
public static String absolutePath = lDir.getAbsolutePath();
static File folder1 = new File(reportLocation);
public static String createScreenshot(WebDriver driver) {
UUID uuid = UUID.randomUUID();
// generate screenshot as a file object
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
try {
// copy file object to designated location
FileUtils.copyFile(scrFile, new File(reportLocation + imageLocation + uuid + ".png"));
} catch (IOException e) {
System.out.println("Error while generating screenshot:\n" + e.toString());
}
return imageLocation + uuid + ".png";
}
@BeforeSuite
public static void beforeTestSuite(){
extent.init(reportLocation + "TestReport.html", true, DisplayOrder.BY_OLDEST_TO_LATEST);
extent.configuration().header().introSummary("Test Report");
extent.configuration().footer().useExtentFooter(false);
}
@BeforeMethod(alwaysRun = true)
public static void startDriver(Method method) {
if(!folder1.exists() )
{
folder1.mkdir();
}
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://relevantcodes.com");
extent.log(LogStatus.INFO, "startDriver", "Browser launched");
extent.startTest(method.getName());
}
@AfterMethod(alwaysRun = true)
public static void stopDriver(ITestResult testResult) throws IOException {
if (testResult.getStatus() == ITestResult.FAILURE) {
extent.log(LogStatus.FAIL, "Check error message","View details below:",absolutePath+"/"+reportLocation+"/"+createScreenshot(getDriver()));
//extent.log(LogStatus.FAIL, "test method failed", testResult.getTestName());
}
driver.quit();
extent.log(LogStatus.INFO, "stopDriver", "Browser closed");
extent.endTest();
}
public static WebDriver getDriver(){
return driver;
}
}
Test1.java
package testng;
import org.testng.SkipException;
import org.testng.annotations.Test;
import pageObjects.Home_Page;
import pageObjects.PageObjectModel;
import com.relevantcodes.extentreports.LogStatus;
public class Test1 extends PageObjectModel {
@Test(enabled=true)
private static void checkLinks1() {
// extent.startTest("passTest");
Home_Page.lnk_Home(driver).click();
extent.log(LogStatus.INFO, "StepName", "click lnk_Home");
Home_Page.lnk_Articles(driver).click();
extent.log(LogStatus.INFO, "StepName", "click lnk_Articles");
Home_Page.lnk_Downloads(driver).click();
extent.log(LogStatus.INFO, "StepName", "lnk_Downloads");
Home_Page.lnk_Contact(driver).click();
extent.log(LogStatus.INFO, "StepName", "lnk_Contact");
// extent.endTest();
}
//This test will fail as there is no link by name google.
@Test(enabled=true)
private static void checkLinks2() {
//extent.startTest("failTest");
Home_Page.lnk_Home(driver).click();
extent.log(LogStatus.INFO, "StepName", "click lnk_Home");
Home_Page.lnk_Articles(driver).click();
extent.log(LogStatus.INFO, "StepName", "click lnk_Articles");
Home_Page.lnk_Downloads(driver).click();
extent.log(LogStatus.INFO, "StepName", "lnk_Downloads");
Home_Page.lnk_Contact(driver).click();
extent.log(LogStatus.INFO, "StepName", "lnk_Contact");
Home_Page.lnk_Google(driver).click();
extent.log(LogStatus.INFO, "StepName", "lnk_Google");
}
@Test(enabled=true)
private static void checkLinks3() {
//Not implemented skipping tests
extent.log(LogStatus.WARNING, "checkLinks3", "SKIPPED");
throw new SkipException("Testing skip.");
}
}