I am using extent reports to generate logs for my selenium tests with Page object model and I have separate classes for pages & tests. I am able to generate extent report logs but my login page logs are being repeated. I doubt this is because the login is being used in all other tests but I am unable to resolve this, as I am new to Java. Please help.
Extent reports class
package util;
import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.ExtentTest;
import com.relevantcodes.extentreports.LogStatus;
public class Reports {
public static ExtentReports extent;
public static ExtentTest test;
public static ExtentReports getInstance() {
if(extent == null) {
GetExtent();
}
return extent;
}
public static ExtentReports GetExtent() {
/*Create ExtentReports object passing location and report name as argument. Notice a new Result Log folder will be created inside project and the report name will be TestReport.html*/
extent = new ExtentReports(System.getProperty("user.dir") + "/RESULT_LOG" + "/TestReport.html", true);
// Add details to our report
extent.addSystemInfo("Selenium Version", "3.0.1").addSystemInfo("Environment", "QA");
return extent;
}
// Start Test Case
public void startTest (String testCaseName, String description) {
// Create ExtentTest passing test case name and description
test = extent.startTest(testCaseName, description);
}
// Log Test status, Test name and Test details
public void logStatus (LogStatus testStatus, String testStepName, String testDetails){
// Log test status
test.log(testStatus, testStepName, testDetails);
}
// Capture screenshot and log into report
public void screenshotLog (LogStatus logStatus, String testStepName, String screenShotPath){
// Attach screenshots
test.log(logStatus, testStepName + test.addScreenCapture(screenShotPath));
}
// End Test Case
public void endTest() {
// End test
extent.endTest(test);
extent.flush();
}
public void endReport(){
extent.close();
}
}
Login Page class
package pages;
import base.TestBase;
import com.relevantcodes.extentreports.LogStatus;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import util.Reports;
public class LoginPage extends TestBase {
Reports reports;
//Page Factories
@FindBy(xpath="//input[@name='email']")
WebElement username;
@FindBy(xpath="//input[@name='pwd']")
WebElement password;
@FindBy(xpath="//fieldset[@id='submitButton']")
WebElement loginBtn;
@FindBy(xpath = "//a[@title='guest']")
WebElement guestTab;
@FindBy(xpath = "//a[@title='grid']")
WebElement gridViewTab;
//Initializing the page objects
public LoginPage() {
PageFactory.initElements(driver, this);
reports = new Reports();
}
//Actions
public String validateLoginPageTitle() {
String ActualTitle =driver.getTitle();
reports.logStatus(LogStatus.INFO, "Get the title", "Title is fetched" + " <span class='label success'> Success</span>");
return ActualTitle;
}
public FloorView login(String un, String pw) throws InterruptedException {
username.sendKeys(un);
// Extent Report Logging. Enter Log status for HTML report
reports.logStatus(LogStatus.INFO, "Enter user name", "User name entered" + " <span class='label success'> Success</span>");
password.sendKeys(pw);
// Extent Report Logging. Enter Log status for HTML report
reports.logStatus(LogStatus.INFO, "Enter password", "Password entered" + " <span class='label success'>Success</span>");
loginBtn.click();
Thread.sleep(7000);
// Extent Report Logging. Enter Log status for HTML report
reports.logStatus(LogStatus.INFO, "Click Login", "Login button clicked" + " <span class='label success'> Success</span>");
return new FloorView();
}
public GuestPage navigateToGuests(){
guestTab.click();
return new GuestPage();
}
public GridView navigateToGridView(){
gridViewTab.click();
return new GridView();
}
}
Login Page Test
package testcases;
import base.TestBase;
import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.LogStatus;
import org.testng.Assert;
import org.testng.annotations.*;
import pages.FloorView;
import pages.LoginPage;
import util.Reports;
public class LoginPageTest extends TestBase {
LoginPage loginPage;
FloorView floorView;
Reports reports;
static ExtentReports extent;
public LoginPageTest() {
super();
}
@BeforeSuite
public void setupReport(){
extent = Reports.getInstance();
}
@BeforeMethod
public void setUp() {
initialization();
loginPage = new LoginPage();
reports = new Reports();
}
@Test(priority=1)
public void LoginPageTitleTest(){
// Start Extent Report
reports.startTest("Login Page Title Test","Launch application and verify that the correct web page is opened");
String title = loginPage.validateLoginPageTitle();
try {
Assert.assertEquals(title, "Eat App Restaurant");
reports.logStatus(LogStatus.PASS, "Validate Page title", "Page title Validated" + " <span class='label success'> Success</span>");
} catch (AssertionError e) {
reports.logStatus(LogStatus.FAIL, "Validate Page title", "Wrong title, test failed" + " <span class='fail label'>Fail</span>");
}
}
@Test(priority=2)
public void LoginTest() throws InterruptedException {
// Start Extent Report
reports.startTest("Login Test","Navigate to Application, verify Login page is opened. Login to the application and verify that login is successful, and floor view is opened by default.");
floorView = loginPage.login(prop.getProperty("username"), prop.getProperty("password"));
String currentURL = driver.getCurrentUrl();
if(currentURL.equals("https://cactus-staging.netlify.com/floor")) {
reports.logStatus(LogStatus.PASS, "Verify login is successful", "Logged in, Floor View opened" + " <span class='label success'> Success</span>"); }
else{
reports.logStatus(LogStatus.FAIL, "Verify login is successful", "Login failed" + " <span class='fail label'>Fail</span>");
}
}
@AfterMethod
public void tearDown() {
reports.endTest();
driver.quit();
}
@AfterSuite
public void afterSuite(){
reports.endReport();
}
}
Grid view page
package pages;
import base.TestBase;
import com.relevantcodes.extentreports.LogStatus;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import util.Reports;
import java.util.List;
public class GridView extends TestBase {
Reports reports;
//Page factories
@FindBy(xpath="//button[@class='pages-styles--addButton-9Gd']/span[2]")
List<WebElement> bookingSlots;
@FindBy(xpath="//ol[@class='shared-components-walkin-styles--list-3JB']/li")
List<WebElement> partySize;
@FindBy(xpath="//button[contains(text(),'Finish Reservation')]")
WebElement finishReservationBtn;
public GridView() {
PageFactory.initElements(driver, this);
reports = new Reports();
}
AddBookingView addBookingView = new AddBookingView();
public void reserveFromGridView(String pax, String slot, String table) throws InterruptedException {
//Choose Party size
for (WebElement li : partySize) {
if (li.getText().equals(pax)) {
li.click();
break;
}
}
reports.logStatus(LogStatus.INFO, "Select covers", "Covers selected" + " <span class='label success'> Success</span>");
//Choose booking time slot
for (WebElement li : bookingSlots) {
if (li.getText().equalsIgnoreCase(slot)) {
li.click();
break;
}
}
Thread.sleep(7000);
reports.logStatus(LogStatus.INFO, "Select booking slot", "Booking slot selected" + " <span class='label success'> Success</span>");
//Choose Table
addBookingView.chooseTable(table);
reports.logStatus(LogStatus.INFO, "Select table", "Table selected" + " <span class='label success'> Success</span>");
//Finish reservation
finishReservationBtn.click();
Thread.sleep(5000);
reports.logStatus(LogStatus.INFO, "Click Finish", "Clicked on Finish" + " <span class='label success'> Success</span>");
}
}
Grid view test class
package testcases;
import base.TestBase;
import com.relevantcodes.extentreports.LogStatus;
import org.testng.annotations.*;
import pages.FloorView;
import pages.GridView;
import pages.LoginPage;
import util.Reports;
import util.TestUtil;
public class GridViewTest extends TestBase {
LoginPage loginPage;
FloorView floorView;
GridView gridView;
String sheetName = "gridview";
Reports reports;
@BeforeMethod
//Browser set up and log in to the application
public void setUp() throws InterruptedException{
initialization();
loginPage = new LoginPage();
reports = new Reports();
floorView = loginPage.login(prop.getProperty("username"), prop.getProperty("password"));
Thread.sleep(5000);
}
@DataProvider
public Object[][] CactusTestData(){
Object data[][] = TestUtil.getTestData(sheetName);
return data;
}
@Test(priority=14, dataProvider="CactusTestData")
public void reserveFromGridViewTest(String pax, String bookingTime, String table) throws InterruptedException {
// Start Extent Report
reports.startTest("Grid View Test","Launch the application and make a reservation from Grid view");
gridView = loginPage.navigateToGridView();
reports.logStatus(LogStatus.INFO, "Navigate to Grid View", "Opened grid view" + " <span class='label success'> Success</span>");
gridView.reserveFromGridView(pax,bookingTime,table);
reports.logStatus(LogStatus.PASS, "Finish reservation", "Reservation is made" + " <span class='label success'> Success</span>");
}
//Quit browser
@AfterMethod
public void tearDown()
{
reports.endTest();
driver.quit();
}
}
So, the report is being generated like this. The login page test has repeated lines of the logs.Click to view the report generated
This is because two of your test classes has this login method invoked. Login Page Test class - floorView = loginPage.login(prop.getProperty("username"), prop.getProperty("password"));
Grid view test class - @BeforeMethod floorView = loginPage.login(prop.getProperty("username"), prop.getProperty("password"));
If you are trying to run these tests independently, then you have start separate extent test nodes on the seperate Extent report.