0

I'm currently researching a way in which I can implement a screen capture method in my acceptance test suite in Scala Cucumber after each step definition is completed in the scenario.

I have already implemented a method that will take a screenshot of a webpage if one of the automation test fails by invoking the method in the after hooks class. This does work fine but this will only capture the web page once the entire scenario has been completed.

I wasn't sure if there was something like before and after hooks that could be applied to the steps instead of the scenario.

Hooks.scala

@After
def tearDown(result: Scenario){

if (result.isFailed) {
  ifCurrentDriverTakesSnapshot {
    takesSnapshot =>
    Snapshotter.takeErrorSnapshot(takesSnapshot, result)
  }
}

Snapshotter.Scala

def takeErrorSnapshot(takesScreenshot: TakesScreenshot, result: Scenario) 
  = {
  try {
    val screenshot = takesScreenshot.getScreenshotAs(OutputType.BYTES)
    result.embed(screenshot, "image/png")
  }
  catch {
    case e: WebDriverException =>
    e.printStackTrace(System.err)
  }
}

I would like to be able to do this in a class or method that can be called after each new page is opened or after each step definition. I could right a step definition that would handle the screen capture but I would like to do in a better way as I have 100's of test scenarios so it would be better to avoid adding a step for this in between every step definition as they have done in the below link.

Cucumber Java screenshots

If anyone could share some light on the matter i'd greatly appreciate it as I'm struggling to find much on the subject.

Thanks!!!

ZachH
  • 1
  • 1
  • What is the reason you want to take a screenshot after each step? To take a screenshot on fail have a look here: https://stackoverflow.com/questions/21226985/java-cucumber-on-junit-test-failure-hook – Marit Jan 13 '18 at 07:53
  • I have already implemented an on fail method but thanks. I want to ideally be able to take screenshots of each different page (e.i when a link is clicked) throughout each of the user scenarios/journeys. – ZachH Jan 13 '18 at 18:43
  • Still curious about the reason you'd want to do that. :) Afaik there is no AfterStep hook currently. – Marit Jan 13 '18 at 19:13
  • It would be so that user journeys can be automated to print screen each new page on the journey so that comparisons can be easily made when pages are changed:) And as there are a large amount of a/c tests and user journeys it would be ideal to be able to screen capture there so that changes between iterations can be shown. – ZachH Jan 14 '18 at 07:50
  • Not sure cucumber would be the best tool for that. Have you looked into something like Galen for screen compare? – Marit Jan 14 '18 at 15:03
  • I will look into Galen thanks. I'm pretty sure cucumber wouldn't be the best option its just that it allows me to then use the automated tests which are also the user journeys. – ZachH Jan 15 '18 at 11:15
  • Unless you find a way to take a screenshot on each page, but I don't think that supported by Cucumber (or at least I wouldn't know how). Not sure Galen is what you're looking for, as I think that looks into comparing screens and making sure they did *not* change.. Good luck! – Marit Jan 15 '18 at 17:02

0 Answers0