anshooarora / extentreports-java

Community version of Extent API for Java has moved to https://github.com/extent-framework/
http://extentreports.com
Other
528 stars 318 forks source link

Running multipe tests in one suite #964

Closed scottdtb closed 7 years ago

scottdtb commented 7 years ago

Hello,

I am using TestNG, Extent V3.0.6 with selenium and java.

My issue is that when I am running a suite of individual tests, the report only outputs the final test within that suite.

Here is one of my most basic and shortest test... NOTE: I am printing my logs to the console only for the short term until I get this working consistently.

public class FifthTest{ // Change at start of every new test ExtentHtmlReporter htmlReporter; ExtentReports extent; ExtentTest logger; WebDriver driver;

@BeforeTest
public void startReport(){

    htmlReporter = new ExtentHtmlReporter("/Users/projects/andertons_Test_Results/Test_Results/AndertonsReport");
    extent = new ExtentReports ();
    extent.attachReporter(htmlReporter);
    htmlReporter.config().setDocumentTitle("Title of the Report Comes here");
    htmlReporter.config().setReportName("Name of the Report Comes here");
    htmlReporter.config().setTestViewChartLocation(ChartLocation.TOP);
    htmlReporter.config().setTheme(Theme.DARK);

    System.setProperty("webdriver.chrome.driver", "/Users/drivers/andertons/chromedriver");

    driver = new ChromeDriver();        
    driver.manage().window().setSize(new Dimension(1500,1100));
    driver.get("http://qaa.andertons.co.uk");
}

@Test
public void FifthTest() throws Exception { // Change at start of every new test

    // Start of test
    logger = extent.createTest("FifthTest"); // Change at start of every new test

    // Implicit wait
    driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

    // Start of test
    System.out.println("*** Starting test " + new Object(){}.getClass().getEnclosingMethod().getName() + " ***");

    // Header elements
    System.out.println(":::::Header elements:::::");

    // LOGO
    if(driver.findElements(By.xpath("//*[@id='ci_espot__AMC_Logo']/div/p/img")).size() != 0){
        System.out.println("Logo is Present");
        }else{
        System.out.println("Logo is Absent");
        }

    // MINI-BASKET
    if(driver.findElements(By.id("widget_minishopcart")).size() != 0){
        System.out.println("Mini-basket go is Present");
        }else{
        System.out.println("Mini-basket is Absent");
        }

    // LOYALTY POINTS
    if(driver.findElements(By.xpath("//*[@id='header']/div[1]/div/div[3]/ul/li[1]")).size() != 0){
        System.out.println("Loyalty points is Present");
        }else{
        System.out.println("Loyalty points is Absent");
        }

    // LOGIN
    if(driver.findElements(By.id("Header_GlobalLogin_signOutQuickLink")).size() != 0){
        System.out.println("Login is Present");
        }else{
        System.out.println("Login is Absent");
        }

    // WISH LIST
    if(driver.findElements(By.xpath("//*[@id='header']/div[1]/div/div[1]/ul/li[2]/a")).size() != 0){
        System.out.println("Wish List is Present");
        }else{
        System.out.println("Wish List is Absent");
        }

    // TRACK YOUR ORDER
    if(driver.findElements(By.xpath("//*[@id='header']/div[1]/div/div[1]/ul/li[1]/a")).size() != 0){
        System.out.println("Track your order is Present");
        }else{
        System.out.println("Track your order is Absent");
        }

    // SEARCH
    if(driver.findElements(By.id("SimpleSearchForm_SearchTerm")).size() != 0){
        System.out.println("Search is Present");
        }else{
        System.out.println("Search is Absent");
        }

    // MEGA MENU
    if(driver.findElements(By.id("department-menu")).size() != 0){
        System.out.println("Mega menu is Present");
        }else{
        System.out.println("Mega menu is Absent");
        }

    // Footer elements
    JavascriptExecutor jse1 = (JavascriptExecutor) driver;
    jse1.executeScript("window.scrollTo(0, document.body.scrollHeight)");
    System.out.println(":::::Footer elements:::::");

    // NEWSLETTER
    if(driver.findElements(By.className("newsletter-signup")).size() != 0){
        System.out.println("Newsletter is Present");
        }else{
        System.out.println("Newsletter is Absent");
        }

    // CONTACT US
    if(driver.findElements(By.xpath("//*[@id='ci_espot__AMC_Footer_Contact']/div")).size() != 0){
        System.out.println("Contact Us is Present");
        }else{
        System.out.println("Contact Us is Absent");
        }

    // BLOGS
    if(driver.findElements(By.xpath("//*[@id='js-advice']/ul/li[1]")).size() != 0){
        System.out.println("Blogs is Present");
        }else{
        System.out.println("Blogs is Absent");
        }

    // RETRUNS
    if(driver.findElements(By.xpath("//*[@id='js-customerservices']/ul/li[4]")).size() != 0){
        System.out.println("Returns is Present");
        }else{
        System.out.println("Returns is Absent");
        }

    // DELIVERY
    if(driver.findElements(By.xpath("//*[@id='js-usefulinfo']/ul/li[2]")).size() != 0){
        System.out.println("Delivery is Present");
        }else{
        System.out.println("Delivery is Absent");
        }

    // CHECK YOUTUBE
    driver.findElement(By.xpath("//*[@id='ci_espot__AMC_Footer_Sociallinks']/div/a[1]")).click();
    System.out.println("**** YOUTUBE LINK WORKS ****");

    // CHECK FACEBOOK
    driver.findElement(By.xpath("//*[@id='ci_espot__AMC_Footer_Sociallinks']/div/a[2]")).click();
    System.out.println("**** FACEBOOK LINK WORKS ****");

    // CHECK TWITTER
    driver.findElement(By.xpath("//*[@id='ci_espot__AMC_Footer_Sociallinks']/div/a[3]")).click();
    System.out.println("**** TWITTER LINK WORKS ****");

    // CHECK INSTAGRAM
    driver.findElement(By.xpath("//*[@id='ci_espot__AMC_Footer_Sociallinks']/div/a[4]")).click();
    System.out.println("**** INSTAGRAM LINK WORKS ****");

    }
    @AfterMethod
    public void getResult(ITestResult result)
    {
        if(result.getStatus() == ITestResult.FAILURE) {
            logger.log(Status.FAIL, MarkupHelper.createLabel(result.getName()+" Test case FAILED due to below issues:", ExtentColor.RED));
            logger.fail(result.getThrowable());
        }else if(result.getStatus() == ITestResult.SUCCESS) {
            logger.log(Status.PASS, MarkupHelper.createLabel(result.getName()+" Test Case PASSED", ExtentColor.GREEN));
        } else {
            logger.log(Status.SKIP, MarkupHelper.createLabel(result.getName()+" Test Case SKIPPED", ExtentColor.ORANGE));
            logger.skip(result.getThrowable());
        }
        extent.flush();
    }
    @AfterTest
        public void endReport(){
            driver.quit();
    }

}

This is my testNG suite that I run which runs each of my tests one after the other.

And finally this is the output in my report: screen shot 2017-10-17 at 14 52 30 As you can see from the code and report that I have provided, when I am running the whole suite, only the final test of the suite is actually displayed in the Extent Report. I have previously converted from v2.41.2 to this version which I understand is quite a bit different so maybe its something I am missing.
anshooarora commented 7 years ago

That is because you are creating new instance of ExtentReports before every test. You just need 1 unique instance of it for the entire session.

See here for an example: https://github.com/saikrishna321/AppiumTestDistribution/blob/master/src/main/java/com/report/factory/ExtentManager.java

scottdtb commented 7 years ago

Hi @anshooarora thanks for the quick reply.

Very useful response however the best method which works best for my code is appending the report before each test. Your guidance lead me to this so thanks.

Keep up the good work!