Forum Discussion

chhote's avatar
chhote
New Contributor
4 years ago

Step level screenshot in cucumber

I am unable to take step level screenshot in my project. I am using testng along with allure as reporting framework. I am able to take the screenshot but they are only appearing in the tear down section at the end. Now my requirement is to take the screenshot at step level itself which is not happening. I am able to take step level screenshot in my selenium + testng project but it is not happening with the cucumber+testng. Any assistance would be highly appreciated.

5 Replies

  • aslakhellesoy's avatar
    aslakhellesoy
    SmartBear Alumni (Retired)

    Hi. Please share some relevant code so we can see what you're doing.

    • chhote's avatar
      chhote
      New Contributor

       

      Spoiler
       

      Hi ,Thanks for the acknowledgement. PFB the code . FYI i am using Testng runner in my project and i am using a listener to implement the screenshot functionality. Below is the Listerner class which would capture the screenshot when a scenario fails.

       

       

      package com.myproject.automation.listeners;

      import io.qameta.allure.Allure;
      import io.qameta.allure.Attachment;
      import io.qameta.allure.listener.StepLifecycleListener;
      import io.qameta.allure.model.StepResult;

      import java.io.ByteArrayInputStream;
      import java.time.LocalDateTime;
      import java.time.format.DateTimeFormatter;

      import org.openqa.selenium.OutputType;
      import org.openqa.selenium.TakesScreenshot;
      import org.openqa.selenium.WebDriver;
      import org.testng.ITestContext;
      import org.testng.ITestListener;
      import org.testng.ITestResult;

      import com.myproject.automation.baseTest.*;

      public class TestAllureListener implements ITestListener {

      private static String getTestMethodName(ITestResult iTestResult) {
      return iTestResult.getMethod().getConstructorOrMethod().getName();
      }

      // Text attachments for Allure
      @Attachment(value = "Page screenshot", type = "image/png")
      public byte[] saveScreenshotPNG(WebDriver driver) {

      System.out.println("saveScreenshotPNG method called");
      //Allure.addAttachment("error screenshot", new ByteArrayInputStream(((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES)));
      return ((TakesScreenshot) driver).getScreenshotAs(OutputType.BYTES);
      }

      // Text attachments for Allure
      @Attachment(value = "{0}", type = "text/plain")
      public static String saveTextLog(String message) {
      return message;
      }

      // HTML attachments for Allure
      @Attachment(value = "{0}", type = "text/html")
      public static String attachHtml(String html) {
      return html;
      }

      @Override
      public void onStart(ITestContext iTestContext) {
      System.out.println("I am in onStart method " + iTestContext.getName());
      iTestContext.setAttribute("WebDriver", BaseTest.getDriver());
      }

      @Override
      public void onFinish(ITestContext iTestContext) {
      System.out.println("I am in onFinish method " + iTestContext.getName());
      }

      @Override
      public void onTestStart(ITestResult iTestResult) {
      System.out.println("I am in onTestStart method " + getTestMethodName(iTestResult) + " start");
      }

      @Override
      public void onTestSuccess(ITestResult iTestResult) {
      System.out.println("I am in onTestSuccess method " + getTestMethodName(iTestResult) + " succeed");
      }

      @Override
      public void onTestFailure(ITestResult iTestResult) {
      System.out.println("I am in onTestFailure method " + getTestMethodName(iTestResult) + " failed");
      Object testClass = iTestResult.getInstance();
      WebDriver driver = BaseTest.getDriver();
      // Allure ScreenShotRobot and SaveTestLog
      if (driver instanceof WebDriver) {
      System.out.println("Screenshot captured for test case:" + getTestMethodName(iTestResult));
      //Allure.addAttachment("error screenshot", new ByteArrayInputStream(((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES)));
      saveScreenshotPNG(driver);
      }
      // Save a log on allure.
      saveTextLog(getTestMethodName(iTestResult) + " failed and screenshot taken!");
      }

      @Override
      public void onTestSkipped(ITestResult iTestResult) {
      System.out.println("I am in onTestSkipped method " + getTestMethodName(iTestResult) + " skipped");
      }

      @Override
      public void onTestFailedButWithinSuccessPercentage(ITestResult iTestResult) {
      System.out.println("Test failed but it is in defined success ratio " + getTestMethodName(iTestResult));
      }

      }

       

       

      Do let me know in case of any queries.

      • aslakhellesoy's avatar
        aslakhellesoy
        SmartBear Alumni (Retired)

        Thanks for sharing your code.

        Your code doesn't seem to use Cucumber, so I'm a bit puzzled you're asking in this forum. We only offer support for Cucumber, not other testing tools.