Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8946679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:34:45+00:00 2026-06-15T12:34:45+00:00

I am trying to capture a screenshot for each failure occurrence and written following

  • 0

I am trying to capture a screenshot for each failure occurrence and written following code, but this is not working.

public class TestFile {

    WebDriver driver = new FirefoxDriver();

    @Test   
    public void Testone(){
        driver.get("http://www.google.com/");           
    }

    @AfterMethod(alwaysRun=true)
    public void catchExceptions(ITestResult result){
        System.out.println("result"+result);
        String methodName = result.getName();
        System.out.println(methodName);

        if(!result.isSuccess()){         
            try { 
                File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
                FileUtils.copyFile(scrFile,new File("C:\\screenshot2.png" ));
            } catch (IOException e1) {
                e1.printStackTrace();
            }       
        }
    }
}

This is failing at

File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);


stack trace:

[TestNG] Running:
C:\Documents and Settings\537310\Local Settings\Temp\testng-eclipse-1576306112\testng-customsuite.xml

result[TestResult name=Testone status=FAILURE method=TestFile.Testone()[pri:0, instance:com.example.tests.TestFile@1b34126] output={null}]
FAILED CONFIGURATION: @AfterMethod catchExceptions([TestResult name=Testone status=FAILURE method=TestFile.Testone()[pri:0, instance:com.example.tests.TestFile@1b34126] output={null}])
net.sf.cglib.core.CodeGenerationException: java.lang.IllegalAccessException-->Class org.openqa.selenium.remote.Augmenter$CompoundHandler can not access a member of class org.openqa.selenium.firefox.FirefoxDriver with modifiers "protected"
at net.sf.cglib.core.ReflectUtils.newInstance(ReflectUtils.java:235)

list of imports:

package com.example.tests;
import org.testng.annotations.Test;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import org.apache.commons.io.FileUtils;
import org.junit.Before;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T12:34:46+00:00Added an answer on June 15, 2026 at 12:34 pm

    The stacktrace you shared is not the stacktrace, but I think the testng log.
    The example you provided actually works. I just made the test fail, because in the @AfterMethod a screenshot is taken only if the test fails: if(!result.isSuccess())

    Then when I ran the example again, I got:
    java.io.FileNotFoundException: C:\screenshot2.png (Access is denied)
    Then I changed the location of the picture to be on D: where the permissions are correct, and it worked end to end, I can see the screenshot.

    Cheers

    import java.io.File;
    import java.io.IOException;
    
    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.Test;
    
    public class TestFile {
    
        WebDriver driver = new FirefoxDriver();
    
        @Test
        public void Testone() {
    
            driver.get("http://www.google.com/");
            assert false;
    
        }
    
        @AfterMethod(alwaysRun = true)
        public void catchExceptions(ITestResult result) {
            System.out.println("result" + result);
            String methodName = result.getName();
            System.out.println(methodName);
    
            if (!result.isSuccess()) {
    
                try {
    
                    File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
                    FileUtils.copyFile(scrFile, new File("C:\\screenshot2.png"));
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
    
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

we can easily capture a view(screenshot) programmatically in iPhone. But i am trying to
I am trying capture UIIMage with UIGraphicsBeginImageContext(); but my code capture the whole screen
I'm trying to implement a website screen shot class as described here http://www.plentyofcode.com/2007/09/website-screenshot-capture-with-aspnet.html The
I trying to capture the target element's id when function fired, but this keyword
I'm trying to capture dvdauthor's output to a file. So far i found this
I'm trying to capture audio, using the method in this question ; with AVCaptureSession
I am trying to capture screenshots on test failure using selenium-client and rspec. I
I'm trying to capture the following pattern everything except data-extra Here is the search
Ok so im trying to figure out how i can tweak this code to
I'm trying to capture the image from my webcam, but I got the error

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.