TestProject Forum
Powered by leading experts in the test automation community

How to capture screenshot when test fails in web application with coded test

When some elements can’t be accessed by selenium, exception is raised.
In the report, we can see the call stack.
Additionally, I want to see the screen. So I added some code like below.

try {

} catch (Exception ex) {

report.step(“Failed”, true, TakeScreenshotConditionType.Always);
}

But I can’t see the captured screenshot.
Could anyone help me for how to get screenshot in error cases?

Thanks,
Yongjo

Hello @yongzo.park.

The code you’ve used to capture the screenshot here will take and save a screenshot once you have uploaded it to the TestProject platform as a coded test and execute it from there. The screenshot will appear in your reports section according to the condition you have set.

You can take and save screenshots locally like this:

  // Take screenshot.
        TakesScreenshot scrShot =((TakesScreenshot) driver);
        // Save as file.
        File SrcFile=scrShot.getScreenshotAs(OutputType.FILE);
        // Create destination for screenshot to be saved.
        File DestFile=new File("PATH\\TO\\SAVE\\IMAGE_NAME.jpg");
        //Copy file to destination.
        try {
            FileUtils.copyFile(SrcFile, DestFile);
        } catch (IOException e) {
            e.printStackTrace();
        }

Hello @david.goichman,

As of now, I want to see the screenshot in TestProject Report section. But with my code, I can’t see the screen. I can only see the exception information. That’s why I ask for how to capture here.
Maybe at later, I will try to save screenshot in local storage of test agent.
At that time, your guide will be helpful to me.

Thanks,
Yongjo

Hello,
report.step(“Failed”, true, TakeScreenshotConditionType.Always); does indeed take a screenshot, here is an example class that will always fail at the last step.

All the steps other than navigating to the URL will take screenshots.
Perhaps your exception is not getting thrown?

Please take a look at the following code, specifically the exception I am throwing at the end of the execute method along with the comments.

  @Test(
    name = "TAKE_SCREENSHOT",
    version = "1.1"
)
public class Takescreenshot implements WebTest {
  @Parameter(
      description = "Auto generated application URL parameter",
      defaultValue = "https://www.google.com/",
      direction = ParameterDirection.INPUT
  )
  public String ApplicationURL;

  public ExecutionResult execute(WebTestHelper helper) throws FailureException {

    WebDriver driver = helper.getDriver();

    // set timeout for driver actions (similar to step timeout)
    driver.setTimeout(5000);
    TestReporter report = helper.getReporter();
    By by;
    boolean booleanResult;

    // Navigate to Google.com
    booleanResult = driver.testproject().navigateToUrl(ApplicationURL);
    report.step(String.format("Navigate to '%s'",ApplicationURL), booleanResult, TakeScreenshotConditionType.Failure);

    // Type Google in text box.
    by = By.cssSelector("[name='q']");
    booleanResult = driver.testproject().typeText(by,"GOOGLE");
    report.step("Type 'GOOGLE' in 'q4'", booleanResult, TakeScreenshotConditionType.Always);

    // Click on image.
    by = By.cssSelector("#hplogo");
    booleanResult = driver.testproject().click(by);
    report.step("Click 'hplogo1'", booleanResult, TakeScreenshotConditionType.Always);

    // Click on search button.
    by = By.xpath("//*[@id='tsf']/div[2]/div[1]/div[3]/center/input[1]");
    booleanResult = driver.testproject().click(by);
    report.step("Click 'ELEMENT 1'", booleanResult, TakeScreenshotConditionType.Always);

    // Purposely fail this step.
    try {
      driver.findElement(By.xpath(null));
    } catch (Exception e) {
      System.out.println("Threw an exception");
      report.step("Something Went wrong", false, TakeScreenshotConditionType.Always);
      // TestProject().method() returns a boolean, does not throw an exception.
      driver.testproject().pause(2000);
      return ExecutionResult.FAILED;
    }

    // Will never be reached.
    return ExecutionResult.PASSED;
  }
}

Something else, if the screenshot image does not immediately appear, you can try refreshing the page and it’ll show up after a bit.