2014-12-04

Screenshots with Mocha-PhantomJS

Today I wondered whether you can take screenshots in your mocha-phantomjs tests. Within 15 minutes I found out that my colleagues at Reaktor had already solved the problem in their own fork. So I took a look and with some help got screenshots working in the open-source application I'm working on for the Board of Education in Finland. Then I made a pull request and now I'm telling you how to make screenshots work.
So, in your test code, write a helper for generating a screenshot on demand:
function takeScreenshot() {
  if (window.callPhantom) {
    var date = new Date()
    var filename = "target/screenshots/" + date.getTime()
    console.log("Taking screenshot " + filename)
    callPhantom({'screenshot': filename})
  }
}
In this function you decide where to put the screenshots and how to name them. In my case, I'm putting them undertarget/screenshots.
If you want to generate a screenshot for each test failure you just add the following into your test code.
  afterEach(function () {
    if (this.currentTest.state == 'failed') {
      takeScreenshot()
    }
  })
And next time you run your mocha-phantomjs tests, KABOOM, you have screenshots. Good times!
Update: A couple of days later, version 3.5.2 with screenshot support was released!

13 comments:

  1. Thank you for allowing me to read it, welcome to the next in a recent article. And thanks for sharing the nice article, keep posting or updating news article.
    Block Chain Training in Bangalore

    Block Chain Training in tambaram

    Block Chain Training in chennai

    Block Chain Training in annanagar

    Block Chain Training in pune

    Block Chain Training in velachery

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Great post. Needed to write simple word that Thanks for suggestions. Keep it up! best sap simple finance online training institute in hyderabad

    ReplyDelete
  4. Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.

    rpa training in chennai | rpa training in chennai
    rpa training in pune | rpa online training | rpa training in bangalore

    ReplyDelete
  5. Really you have done great job,There are may person searching about that now they will find enough resources by your post

    java training in jayanagar | java training in electronic city

    java training in chennai | java training in USA

    ReplyDelete
  6. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
    python training in tambaram | python training in annanagar | python training in jayanagar

    ReplyDelete
  7. Great thoughts you got there, believe I may possibly try just some of it throughout my daily life.
    Online DevOps Certification Course - Gangboard
    Best Devops Training institute in Chennai

    ReplyDelete
  8. A very nice guide. I will definitely follow these tips. Thank you for sharing such detailed article. I am learning a lot from you.

    rpa training in electronic-city | rpa training in btm | rpa training in marathahalli | rpa training in pune

    ReplyDelete