mercredi 18 mars 2020

Not able to run Parallel Testing on selenium Grid using Testng and generate Extent Report

My expectation is : To run all my classes parallel in the Test suite and generate an extent report. Actual : When I am running parrallel my Url is not passed to all the threads and many of my test cases are being skipped.Not sure why?But some of the testcases from some classes are able to execute properly.

import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.MediaEntityBuilder;
import com.aventstack.extentreports.MediaEntityModelProvider;
import com.buildingiq.p3automation.common.ActionHelper;
import com.buildingiq.p3automation.common.ConfigReader;
import org.apache.log4j.PropertyConfigurator;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.ITestResult;
import org.testng.annotations.*;

import java.io.IOException;
import java.lang.reflect.Method;
import java.net.URL;
import java.util.HashMap;
import java.util.Properties;


public  class BaseWebDriver {

    public  ThreadLocal<WebDriver> driver = new ThreadLocal
    public  ConfigReader configReader = new ConfigReader();
    public    ExtentReports extent = ExtentManager.createInstance("extent.html");
    public static   ThreadLocal<ExtentTest> parentTest = new ThreadLocal();
    public static ThreadLocal<ExtentTest> test = new ThreadLocal();

    @BeforeSuite
    public  synchronized void setUpExtentReport() {
    }

    @BeforeTest
    public synchronized  void loadlogger() {
        String loggerConfPath = System.getProperty("user.dir")
                + "/src/main/java/com/*****/p3automation/core/logger.properties";
        PropertyConfigurator.configure(loggerConfPath);
    }
    @BeforeClass
    public synchronized void beforeClass() {
        ExtentTest parent = getExtent().createTest(getClass().getName());
        parentTest.set(parent);
    }


 @BeforeMethod
    public synchronized void setUp(Method result)  throws IOException

    {

     Properties props = getProperties().getPropValues();

       String USER_NAME = System.getProperty("user.name");
        String BUILD_NUMBER;
        try {
            BUILD_NUMBER = System.getenv("BUILD_NUMBER");
            if (BUILD_NUMBER.isEmpty()){
                BUILD_NUMBER = USER_NAME;
            }
        }
        catch (Exception e) {
            BUILD_NUMBER = USER_NAME;
        }

        System.out.println("Build number is "+ BUILD_NUMBER);
        String name_of_test = result.getName();
        System.out.println("Test Method - " + name_of_test);



        DesiredCapabilities capability = DesiredCapabilities.chrome();
        capability.setCapability("name", "P3 functional testing --- " + name_of_test);
        capability.setCapability("build", BUILD_NUMBER);

        capability.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
        capability.setCapability(ChromeOptions.CAPABILITY, options);

        driver.set(new RemoteWebDriver(new URL("http://selenium-job.buildingiq.com"), capability));

        getDriver().get(props.getProperty("portal3"));
        getDriver().manage().window().maximize();
         Cookie cookie = new Cookie("zaleniumTestName", name_of_test);
        getDriver().manage().addCookie(cookie);

        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }  


     @AfterMethod
    public  synchronized void teardown(ITestResult result) {
      String testresult="";
        try {
            if (result.getStatus() == ITestResult.SUCCESS) {
                testresult = "true";
                test.get().pass("Test passed");
            } else {
                if(result.getStatus() == ITestResult.FAILURE){
                    testresult = "false";

"+result.getThrowable().getMessage()); String base64Screenshot = ActionHelper.getScreenshotBase(getDriver()); MediaEntityModelProvider mediaModel = MediaEntityBuilder.createScreenCaptureFromBase64String(base64Screenshot).build();

                    test.get().fail(result.getThrowable(),mediaModel);
                }else if(result.getStatus() == ITestResult.SKIP){

                    test.get().skip("The Test case is skipped");
                }
            }
            Cookie cookie = new Cookie("zaleniumTestPassed", testresult);
            getDriver().manage().addCookie(cookie);
        } catch (Exception e) {
            e.printStackTrace();
        }

         getExtent().flush();
         getDriver().close();
         getDriver().quit();
   }

    @AfterSuite
    public synchronized void teardownTest() {

    }

    public synchronized WebDriver getDriver(){
         return driver.get();
    }



    public synchronized ConfigReader getProperties(){
        return configReader;
    }

    public synchronized ExtentReports getExtent(){
        return extent;
    }

}
//Sample of one class where Tests are there
Tests

import com.aventstack.extentreports.ExtentTest;
import com.buildingiq.p3automation.common.ActionHelper;
import com.buildingiq.p3automation.common.FunctionalHelper;
import com.buildingiq.p3automation.common.Waits;
import com.buildingiq.p3automation.core.BaseWebDriver;
import com.buildingiq.p3automation.core.ExtendedException;
import com.buildingiq.p3automation.pageObjects.BuildingPerformancePageObjects;
import com.buildingiq.p3automation.pageObjects.LoginPageObjects;
import com.buildingiq.p3automation.pageObjects.OverviewPageObjects;
import org.openqa.selenium.NoSuchElementException;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

import java.io.IOException;
import java.lang.reflect.Method;

import static org.testng.Assert.assertTrue;



public class BuildingPerformanceTests extends BaseWebDriver {

   ActionHelper actionHelper = new ActionHelper();
    FunctionalHelper functionalHelper = new FunctionalHelper();

    @Test(groups = {"regression"})
    public void createBuildingPerformanceWidget(Method method) throws ExtendedException, IOException {
        ExtentTest child = parentTest.get().createNode("Building Performance Widget Creation", "The user should be able to create the Building Performance Widget");
        test.set(child);
        Waits waits = new Waits(getDriver());
        LoginPageObjects loginPageObjects = new LoginPageObjects(getDriver(),waits);
        OverviewPageObjects overviewPageObjects = new OverviewPageObjects(getDriver(),waits);
        try {
            functionalHelper.loginToPortal3(loginPageObjects.userName,loginPageObjects.pwd,loginPageObjects.login,overviewPageObjects.openDrawer,waits);
            functionalHelper.addDashboard(overviewPageObjects,waits,method.getName());
            int bldingPrformanceCountBeforeAddingBldingPrformance = functionalHelper.getWidgetCount(overviewPageObjects.buildingPerformanceElements);
            test.get().info("Building Performance Chart Elements Count before adding a new Building Performance chart : " + bldingPrformanceCountBeforeAddingBldingPrformance);
            functionalHelper.addWidget(overviewPageObjects.addWidgetIcon,overviewPageObjects.addBuildingPerformanceChartButton,overviewPageObjects.doneButton,waits);
            int bldingPrformanceCountAfterAddingBldingPrformance = functionalHelper.getWidgetCount(overviewPageObjects.buildingPerformanceElements);
            test.get().info("Building Performance Chart Elements count after adding a new Building Performance Chart : " + bldingPrformanceCountAfterAddingBldingPrformance);
            if (bldingPrformanceCountBeforeAddingBldingPrformance == (bldingPrformanceCountAfterAddingBldingPrformance - 1)) {
                test.get().pass("A New Building Performance Chart Widget is created successfully and the Test Case is passed");
                assertTrue(true);
            } else {
                test.get().fail("A New Building Performance Chart Widget is not created and the Test Case is failed");
                assertTrue(false);
            }
        } catch (NoSuchElementException | NullPointerException e) {
            test.get().fail("A New Building Performance Chart Widget is not created and the Test Case is failed due to : " + e.getMessage());
            assertTrue(false);
        }finally {
            functionalHelper.deleteDashboard(overviewPageObjects,waits,method.getName());
            functionalHelper.logoutPortal3(overviewPageObjects.avatarButton, overviewPageObjects.logoutText,waits);
        }
    }


     @Test(groups = {"regression"})
    public void deleteBuildingPerformanceWidget(Method method) throws ExtendedException,IOException {
        ExtentTest child = parentTest.get().createNode("Building Performance Widget Deletion", "The user should be able to delete the Building Performance Widget");
        test.set(child);
        Waits waits = new Waits(getDriver());
        LoginPageObjects loginPageObjects = new LoginPageObjects(getDriver(),waits);
        OverviewPageObjects overviewPageObjects = new OverviewPageObjects(getDriver(),waits);
        BuildingPerformancePageObjects buildingPerformancePageObjects = new BuildingPerformancePageObjects(getDriver(),waits);
        try {
            functionalHelper.loginToPortal3(loginPageObjects.userName,loginPageObjects.pwd,loginPageObjects.login,overviewPageObjects.openDrawer,waits);
            functionalHelper.addDashboard(overviewPageObjects,waits,method.getName());
            int bldingPrformanceCountBeforeDeletingBldingPrformance = functionalHelper.getWidgetCount(overviewPageObjects.buildingPerformanceElements);
            test.get().info("Building Performance Chart Elements Count before deleting a  Building Performance chart : " + bldingPrformanceCountBeforeDeletingBldingPrformance);
            functionalHelper.addWidget(overviewPageObjects.addWidgetIcon,overviewPageObjects.addBuildingPerformanceChartButton,overviewPageObjects.doneButton,waits);
            int bldingPrformanceCountAfterAddingBldingPrformance = functionalHelper.getWidgetCount(overviewPageObjects.buildingPerformanceElements);
            test.get().info("Building Performance Chart Elements count after adding a new Building Performance Chart : " + bldingPrformanceCountAfterAddingBldingPrformance);
            if (bldingPrformanceCountAfterAddingBldingPrformance == (bldingPrformanceCountBeforeDeletingBldingPrformance + 1)) {
                test.get().pass("A New Building Performance Chart Widget is created successfully and the Test Case is passed");
                assertTrue(true);
            } else {
                test.get().fail("A New Building Performance Chart Widget is not created and the Test Case is failed");
                assertTrue(false);
            }
            functionalHelper.removesWidget(buildingPerformancePageObjects.widgetActions.get(0), buildingPerformancePageObjects.removeWidget.get(buildingPerformancePageObjects.removeWidget.size() - 1),waits);
            int bldingPrformanceChartCountAfterDeletingBldingPrformance = functionalHelper.getWidgetCount(overviewPageObjects.buildingPerformanceElements);
            test.get().info("Building Performance Chart Elements Count After deleting a  Building Performance chart : " + bldingPrformanceChartCountAfterDeletingBldingPrformance);
            if (bldingPrformanceCountBeforeDeletingBldingPrformance == bldingPrformanceChartCountAfterDeletingBldingPrformance ) {
                test.get().pass("A Building Performance Chart Widget is deleted successfully and the Test Case is passed");
                assertTrue(true);
            } else {
                test.get().fail("A Building Performance Chart Widget is not deleted and the Test Case is failed");
                assertTrue(false);
            }
        }catch (NoSuchElementException|NullPointerException e){
            test.get().fail("A  Building Performance Chart Widget is not deleted and the Test Case is failed due to : " + e.getMessage());
            assertTrue(false);
        }catch(Exception e){
            test.get().fail("A  Building Performance Chart Widget is not deleted and the Test Case is failed due to : " + e.getMessage());
            assertTrue(false);
        }finally {
            functionalHelper.deleteDashboard(overviewPageObjects,waits,method.getName());
            functionalHelper.logoutPortal3(overviewPageObjects.avatarButton, overviewPageObjects.logoutText,waits);
        }
    }


    @Parameters({"Building1"})
    @Test (groups = {"regression","smoke"})
    public void buildingPerformanceWidgetDataCheck(String Building,Method method) throws ExtendedException,IOException {
        ExtentTest child = parentTest.get().createNode("Building Performance Widget Data Check", "The user should be able to see the data for the Selected building in the Building Performance Widget");
        test.set(child);
        Waits waits = new Waits(getDriver());
        LoginPageObjects loginPageObjects = new LoginPageObjects(getDriver(),waits);
        OverviewPageObjects overviewPageObjects = new OverviewPageObjects(getDriver(),waits);
        BuildingPerformancePageObjects buildingPerformancePageObjects = new BuildingPerformancePageObjects(getDriver(),waits);
        try {
            functionalHelper.loginToPortal3(loginPageObjects.userName,loginPageObjects.pwd,loginPageObjects.login,overviewPageObjects.openDrawer,waits);
            functionalHelper.addDashboard(overviewPageObjects,waits,method.getName());
            int bldingPrformanceCount = functionalHelper.getWidgetCount(overviewPageObjects.buildingPerformanceElements);
            test.get().info("Building Performance Chart Elements Count  " + bldingPrformanceCount);
            functionalHelper.addWidget(overviewPageObjects.addWidgetIcon,overviewPageObjects.addBuildingPerformanceChartButton,overviewPageObjects.doneButton,waits);
            int bldingPrformanceChartCount1 = functionalHelper.getWidgetCount(overviewPageObjects.buildingPerformanceElements);
            test.get().info("Building Performance Chart Elements Count After adding a  Building Performance chart : " + bldingPrformanceChartCount1);
            if (bldingPrformanceChartCount1 == (bldingPrformanceCount +1)) {
                test.get().pass("A Building Performance Chart Widget is added successfully ");
                assertTrue(true);
            } else {
                test.get().fail("A Building Performance Chart Widget is not added and the Test Case is failed,due to failure in addition of Building Performance Chart");
                assertTrue(false);
            }
            buildingPerformancePageObjects.clickWidgetActions();
            test.get().info("User clicked on the Widget Action buttons");
            actionHelper.sleepsInMilliSeconds(2000);
            buildingPerformancePageObjects.clickWidgetSetting();
            test.get().info("User clicked on Widget Settings under Widget Action");
            waits.explicitWaitElementToBeClickableByWebElement(buildingPerformancePageObjects.inputBuildingName);
            buildingPerformancePageObjects.clickInputBuildingName();
            test.get().info("User clicked in the Building Search field");
            buildingPerformancePageObjects.enterInputBuildingName(Building);
            test.get().info("User entered the Building name in the Building Search field");
            waits.explicitWaitElementToBeClickableByWebElement(buildingPerformancePageObjects.selectBuilding);
            buildingPerformancePageObjects.clickSelectedBuilding();
            test.get().info("User selected the Building ");
            buildingPerformancePageObjects.clickApplyButton();
            test.get().info("User clicked on Apply Button");
            actionHelper.sleepsInMilliSeconds(5000);
            functionalHelper.clickDatePicker(buildingPerformancePageObjects.datePicker.get(0));
            actionHelper.sleepsInMilliSeconds(5000);
            functionalHelper.selectAndEnterCustomDate(buildingPerformancePageObjects.calenderStartDate,buildingPerformancePageObjects.calenderEndDate,buildingPerformancePageObjects.customeDate,buildingPerformancePageObjects.saveBtn);
            test.get().info("User entered the start Date and End Date and saved");
            actionHelper.sleepsInMilliSeconds(30000);
            if (buildingPerformancePageObjects.verifyBuildingPerformanceWidgetData()) {
                test.get().pass("A Building Performance Chart Widget Data Check Test Case is passed");
                assertTrue(true);
            } else {
                test.get().fail("A Building Performance Chart Widget Data Check Test Case is failed");
                assertTrue(false);
            }
        }catch (NoSuchElementException |NullPointerException e){
            test.get().fail("A  Building Performance Chart Data Check Test Case is failed due to :  " + e.getMessage());
            assertTrue(false);
        }catch(Exception e){
            test.get().fail("A  Building Performance Chart Data Check Test Case is failed due to : " + e.getMessage());
            assertTrue(false);
        }finally {
            functionalHelper.deleteDashboard(overviewPageObjects,waits,method.getName());
            functionalHelper.logoutPortal3(overviewPageObjects.avatarButton, overviewPageObjects.logoutText,waits);
        }
    }
}



ExtentManager :


import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;


public class ExtentManager {

   public static ExtentReports extent;
   public static ExtentHtmlReporter htmlReporter;
    public static ExtentReports getInstance() {
        if (extent == null)
            createInstance("extent.html");

        return extent;
    }

    public static synchronized ExtentReports createInstance(String fileName) {
        htmlReporter = new ExtentHtmlReporter(fileName);

        extent =new ExtentReports();
        extent.attachReporter(htmlReporter);
        return extent;

    }

}


Testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="P3Suite" parallel="classes" thread-count="1">
    <test  name="RegressionTests">
         <parameter name = "Building1" value="AU_ACT_CentraPlaza"/>
         <parameter name = "Building2" value="St John of God Hospital"/>
         <parameter name = "userName" value="****.biq@gmail.com"/>
         <parameter name = "pwd" value="*******"/>
         <parameter name = "subject" value="did this"/>
         <parameter name = "editedTxt" value="--Edited By Automation"/>
        <classes>
      <class name="com.buildingiq.regressionTests.BuildingInfoTests"/>
           <class name="com.buildingiq.regressionTests.BuildingPerformanceTests"/>
            <class name="com.buildingiq.regressionTests.CalenderWidgetTests"/>
            <class name="com.buildingiq.regressionTests.EnergyBenchMarkWidgetTests"/>
            <class name="com.buildingiq.regressionTests.EnergyChartWidgetTests"/>
            <class name="com.buildingiq.regressionTests.GeneralWidgetTests"/>
            <class name="com.buildingiq.regressionTests.LoginPageTests"/>
            <class name="com.buildingiq.regressionTests.MultiMeterWidgetTests"/>
            <class name="com.buildingiq.regressionTests.OverviewPageTests"/>
            <class name="com.buildingiq.regressionTests.SavingsOverviewWidgetTests"/>
        </classes>
    </test>
</suite>

Aucun commentaire:

Enregistrer un commentaire