Similar Posts

4 Comments

  1. Hi,
    I added the same code but Image is not attached to the report. Image displayed as a line.
    By Looking in to my Code can you please help me where is the error.

    I am using Latest version Extentreports-3.1.1.jar

    public static WebDriver driver;
    public static WebDriverWait wait;
    public static Properties sprops = new Properties();
    public static Properties aprops = new Properties();
    public String ScrPath = System.getProperty(“user.dir”);
    public File SharedRepofile = new File(ScrPath+”\\Repositories\\SharedRepo.properties”);
    public FileInputStream SharedRepoInputFile;
    public FileInputStream AppRepoInputFile;
    public String ErrScPath = System.getProperty(“user.dir”)+”\\ErrorScreenshots\\”;

    public String CaptureScreenShot(String screenshotname) throws Exception{
    DateFormat DF = new SimpleDateFormat(“dd-MM-yyyy_HHmmss”);
    Date D = new Date();
    String time = DF.format(D);
    TakesScreenshot ts = (TakesScreenshot)driver;
    File Source = ts.getScreenshotAs(OutputType.FILE);
    String dest = ErrScPath + screenshotname+”_”+time+”.png”;
    File destination = new File(dest);
    FileUtils.copyFile(Source, destination);
    return dest;
    }

    @BeforeTest()
    public void beforeTest(String Browser, String DataFile, String AppRepofile ) throws Exception {
    if (Browser.equalsIgnoreCase(“Chrome”)) {
    ChromeOptions options = new ChromeOptions();
    options.addArguments(“test-type”);
    options.addArguments(“disable-popup-blocking”);
    DesiredCapabilities capabilities = DesiredCapabilities.chrome();
    capabilities.setCapability(ChromeOptions.CAPABILITY, options);
    System.setProperty(“webdriver.chrome.driver”, “D:\\lib\\chromedriver.exe”);
    driver = new ChromeDriver();
    }
    else if(Browser.equalsIgnoreCase(“IE”)){
    System.setProperty(“webdriver.ie.driver”, “D:\\lib\\IEDriverServer.exe”);
    driver = new InternetExplorerDriver();
    }
    else if(Browser.equalsIgnoreCase(“Firefox”)) {
    System.setProperty(“webdriver.gecko.driver”, “D:\\lib\\geckodriver.exe”);
    driver = new FirefoxDriver();
    }

    Capabilities cap = ((RemoteWebDriver)driver).getCapabilities();
    String OS = System.getProperty(“os.name”);
    String BrowserName = cap.getBrowserName().toLowerCase();
    String Version = cap.getVersion();

    HtmlReporter = new ExtentHtmlReporter(System.getProperty(“user.dir”)+”\\CustomReport\\MyOwnReport.html”);
    Extent = new ExtentReports();
    Extent.attachReporter(HtmlReporter);

    Extent.setSystemInfo(“OS”, OS);
    try {
    Extent.setSystemInfo(“Host Name”, Inet4Address.getLocalHost().toString());
    } catch (UnknownHostException e) {
    e.printStackTrace();
    }
    Extent.setSystemInfo(“Web Browser”, BrowserName);
    Extent.setSystemInfo(“Browser Version : “, Version);

    HtmlReporter.config().setChartVisibilityOnOpen(true);
    HtmlReporter.config().setDocumentTitle(“Logix Health Automation Test Report”);
    HtmlReporter.config().setReportName(“Logix Express”);
    HtmlReporter.config().setTestViewChartLocation(ChartLocation.TOP);
    HtmlReporter.config().setTheme(Theme.STANDARD);

    setExcelFile(ScrPath+DataFile);

    driver.get(GetDatafromCell(“Global”, 2, “URL”));
    driver.manage().window().setSize(new Dimension(1280, 1024));
    Thread.sleep(3000);

    SharedRepoInputFile = new FileInputStream(SharedRepofile);
    sprops.load(SharedRepoInputFile);
    AppRepoInputFile = new FileInputStream(ScrPath+AppRepofile);
    aprops.load(SharedRepoInputFile);

    login(GetDatafromCell(“Global”, 2, “UID”), GetDatafromCell(“Global”, 2, “PWD”));

    Thread.sleep(5000);
    }

    @AfterSuite
    public void afterSuite() {
    File f = new File(System.getProperty(“user.dir”)+”\\CustomReport\\MyOwnReport.html”);
    if (f.exists()) {
    String oldtDir = System.getProperty(“user.dir”) + “\\CustomReport\\Old\\”;
    File fold = new File(oldtDir);
    //f= new File(“MyOwnReport.html”).renameTo(new SimpleDateFormat(“dd-MM-yyyy HH:mm:ss”).format(new Date()+”MyOwnReport.html”);
    String rn = new SimpleDateFormat(“dd-MM-yyyy HH:mm:ss”)+”MyOwnReport.html”;
    File nf = new File(rn);
    f.renameTo(nf);
    try {
    FileUtils.moveFileToDirectory(f, fold, true);
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    Extent.flush();
    }

    @BeforeMethod
    public void beforeMethod(Method method) {
    Test = Extent.createTest((this.getClass().getSimpleName()));
    }

    @Test
    public void forTest() throws Exception {
    driver.findElement(By.linkText(“Dashboard”)).click();
    Thread.sleep(6000);
    }

    @Test
    public void forTest2() throws Exception {
    driver.findElement(By.linkText(“Work Queue”)).click();
    driver.findElement(By.id(“toggleOutstanding”)).click();
    Thread.sleep(3000);
    }
    @AfterMethod
    public void getResult(ITestResult result) throws Exception{
    if (result.getStatus() == ITestResult.FAILURE) {
    // With Java Screen Capture
    String imagepath = CaptureScreenShot(result.getName());
    Test.log(Status.FAIL, MarkupHelper.createLabel(result.getName() + ” Test case FAILED due to below issues:”, ExtentColor.RED));
    //Test.log(Status.FAIL, MarkupHelper.createLabel(result.getThrowable()+ ” Test case FAILED :”, ExtentColor.RED));
    //Test.fail(result.getThrowable());
    //Test.log(Status.FAIL, (Markup) Test.addScreenCaptureFromPath(imagepath));
    //Test.log(Status.FAIL, Test.addScreenCaptureFromPath(imagepath));
    Test.fail(“Snapshot below : ” + Test.addScreenCaptureFromPath(imagepath));
    }
    else if(result.getStatus() == ITestResult.SUCCESS){
    Test.log(Status.PASS, MarkupHelper.createLabel(result.getName() + ” Test Case PASSED”, ExtentColor.GREEN));
    }
    else
    {
    Test.log(Status.SKIP, MarkupHelper.createLabel(result.getName() + ” Test Case SKIPPED”, ExtentColor.ORANGE));
    Test.skip(result.getThrowable());
    }

    }

Leave a Reply

Your email address will not be published.