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!

50 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. 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
  4. 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
  5. Mình đã tìm thấy các thông tin cần thiết ở đây, cảm ơn bạn. Mình cũng muốn giới thiệu về một Công ty dịch thuật uy tín - Công ty cổ phần dịch thuật miền trung - MIDtrans có văn phòng chính tại địa chỉ 02 Hoàng Diệu, TP Đồng Hới, tỉnh Quảng Bình có Giấy phép kinh doanh số 3101023866 cấp ngày 9/12/2016 là đơn vị chuyên cung cấp dịch vụ dịch thuật, phiên dịch dành các cá nhân. Hệ thống thương hiệu và các Công ty dịch thuật con trực thuộc: công ty dịch thuật hcm - dịch thuật sài gòn 247 , địa chỉ 47 Điện Biên Phủ, Phường Đakao, Quận 1 TP HCM, dịch thuật buôn mê thuột, đăk lăk : địa chỉ 100 , Lê lợi, TP Buôn Mê Thuột là nhà cung ứng dịch vụ dịch thuật uy tín hàng đầu tại Đăk Lăk; viet translate : dịch vụ dịch thuật cho người nước ngoài có nhu cầu, giao diện tiếng Anh dễ sử dụng; dịch thuật công chứng bình thạnh : nhà cung ứng dịch vụ dịch vụ dịch thuật phiên dịch hàng đầu tại Quận Bình Thạnh, TP HCM; dịch thuật đà nẵng midtrans : Địa chỉ 54 Đinh Tiên Hoàng, Quận Hải Châu, TP Đà Nẵng chuyên cung cấp dịch vụ dịch thuật công chứng, dịch thuật chuyên ngành tại Đà Nẵng; dịch thuật hà nội midtrans : địa chỉ 101 Láng Hạ, Đống Đa, Hà Nội là nhà cung ứng dịch vụ biên dịch, phiên dịch chuyên nghiệp tại địa bàn Hà Nội. Chúng tôi chuyên cung cấp các dịch vụ biên dịch và phiên dịch, dịch thuật công chứng chất lượng cao hơn 50 ngôn ngữ khác nhau như tiếng Anh, Nhật, Hàn, Trung, Pháp, Đức, Nga, Tây Ban Nha, Bồ Đào Nha, Ý, Ba Lan, Phần Lan, Thái Lan, Hà Lan, Rumani, Lào, Campuchia, Philippin, Indonesia, La Tinh, Thụy Điển, Malaysia, Thổ Nhĩ Kỳ..vv... Dịch thuật MIDtrans tự hào với đội ngũ lãnh đạo với niềm đam mê, khát khao vươn tầm cao trong lĩnh vực dịch thuật, đội ngũ nhân sự cống hiến và luôn sẵn sàng cháy hết mình. Chúng tôi phục vụ từ sự tậm tâm và cố gắng từ trái tim những người dịch giả.Tự hào là công ty cung cấp dịch thuật chuyên ngành hàng đầu với các đối tác lớn tại Việt nam trong các chuyên ngành hẹp như: y dược (bao gồm bệnh lý), xây dựng (kiến trúc), hóa chất, thủy nhiệt điện, ngân hàng, tài chính, kế toán. Các dự án đã triển khai của Công ty dịch thuật chuyên nghiệp MIDtrans đều được Khách hàng đánh giá cao và đạt được sự tín nhiệm về chất lượng biên phiên dịch đặc biệt đối với dịch hồ sơ thầu , dịch thuật tài liệu tài chính ngân hàng, dịch thuật tài liệu y khoa đa ngữ chuyên sâu. Đó là kết quả của một hệ thống quản lý chất lượng dịch thuật chuyên nghiệp, những tâm huyết và kinh nghiệm biên phiên dịch nhiều năm của đội ngũ dịch giả của chúng tôi. Hotline: 0947688883. email: info@dichthuatmientrung.com.vn . Các bạn ghé thăm site ủng hộ nhé. Cám ơn nhiều

    ReplyDelete
  6. Good blog explanation
    Sanjary Kids is one of the best play school and preschool in Hyderabad,India. Give your child the best preschool experience by choosing the best playschool of Hyderabad in Abids. we provide programs like Play group,Nursery,Junior KG,Senior KG,and provides Teacher Training Program.
    Preschool teacher training course in hyderabad

    ReplyDelete
  7. 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.
    Selenium online training

    ReplyDelete
  8. Good blog information of the topic

    Sanjary Academy is the best Piping Design institute in Hyderabad, Telangana. It is the best Piping design Course in India and we have offer professional Engineering Courses like Piping design Course, QA/QC Course, document controller course, Pressure Vessel Design Course, Welding Inspector Course, Quality Management Course and Safety Officer Course.
    Piping Design Course
    Piping Design Course in Hyderabad ­
    Piping Design Course in India­

    ReplyDelete
  9. Nice post. Thanks for sharing the valuable information. it’s really helpful. Who want to learn this blog most helpful. Keep sharing on updated posts…
    Selenium online training
    Selenium certification training
    Selenium online course
    Selenium training course

    ReplyDelete
  10. Thanks for sharing
    "Sanjary Academy provides excellent training for Piping design course. Best Piping Design Training Institute in Hyderabad,
    Telangana. We have offer professional Engineering Course like Piping Design Course,QA / QC Course,document Controller
    course,pressure Vessel Design Course, Welding Inspector Course, Quality Management Course, #Safety officer course."
    Piping Design Course in India­
    Piping Design Course in Hyderabad
    Piping Design Course in Hyderabad
    QA / QC Course
    QA / QC Course in india
    QA / QC Course in Hyderabad

    ReplyDelete
  11. For Blockchain training in Bangalore, Visit:
    Blockchain training in Bangalore

    ReplyDelete
  12. Visit for Blockchain training in Bangalore:- Blockchain training in Bangalore

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

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

    ReplyDelete
  15. I think you did an awesome job explaining it. Sure beats having to research it on my own. Thanks
    Devi Ahilya Vishwa Vidhyalaya BCOM 1st, 2nd & Final Year TimeTable 2020

    ReplyDelete
  16. Thanks for Sharing This Article.It is very so much valuable content. I hope these Commenting lists will help to my website
    blockchain online training

    ReplyDelete
  17. Feeling so good to read your information's in the blog.
    thanks for sharing your ideas with us and add more info.
    Top 10 Makeup Artists in Kolkata

    ReplyDelete
  18. Thanks for Sharing This Article.It is very so much valuable content. fue turkey

    ReplyDelete
  19. This site helps to clear your all query.
    This is really worth reading. nice informative article.bsc 1st year result
    mg university bcom result

    ReplyDelete
  20. This was answered by Keto Advanced Fat Burner while I came through that storm marvelous. Fortunately, we'll assume you're ready to give up on the notion of Keto Advanced Fat Burner. It is a record amount. This gives Keto Advanced Fat Burner less of a chance to have more Keto Advanced Fat Burner. That's the moment to go for it. Don't believe us? That is elementary to do.




    Keto Advanced Fat Burner

    ReplyDelete
  21. Thank you for posting this Article! lessburn focuses on providing top-notch Business Information, Business Support andDigital Marketing Services to all companies ranging from budding startups to large multinational organizations across the globe.

    ReplyDelete
  22. thanks for sharing such a wonderful information from this post
    Salesforce CPQ Course
    Learn CPQ Salesforce

    ReplyDelete
  23. I think this is one of the most significant info for me.And i’m glad reading your article.

    Banasthali Vidhyapith time table

    bilashpur university time table

    ReplyDelete
  24. If you're looking for something specific, you can always use the search bar to find exactly what you're looking for. Plus, movies flame is always updating their selection, so you'll never miss a new release.

    ReplyDelete
  25. Great Information, thank you so much for sharing with us. Visit Deysphotography for Pre-wedding Photoshoot, best maternity photographer in delhi, Pre-wedding Photoshoot, and Baby Photoshoot at Home services.
    Pre-wedding Photoshoot

    ReplyDelete
  26. Bulk messaging is an efficient way to reach a large number of people with the same message, and it can be a very effective tool for promoting your business. When used correctly, bulk messaging can be a great way to reach new customers, or to keep existing customers informed about your business.

    ReplyDelete
  27. I'm thrilled to hear that you find the content wonderful, awesome, and highly useful. It's incredibly gratifying to know that the explanations provided have been clear and comprehensive, enabling you to grasp various concepts and benefiting you significantly. Your positive feedback encourages me, and I'm glad to have been a valuable source of information through the blog. Thank you for sharing your experience, and I'm delighted to have been of assistance.
    Oracle BI publisher Training

    ReplyDelete
  28. Visit Orange Pill Clinic for leading Nursing and Doctor Consultation Services at Home, Home Nursing Services in Delhi NCR, and Nursing Services for Suturing Removal at Home. For more information visit our website.
    Home Nursing Services in Delhi NCR

    ReplyDelete
  29. Great insights on using Mocha and PhantomJS for automated screenshots! This technique is incredibly useful for ensuring the visual integrity of web pages during development. As someone deeply involved in digital marketing, I find that understanding such tools can greatly enhance the quality of online marketing campaigns. For anyone looking to expand their skill set, especially in areas like web testing and analytics, a comprehensive Best Digital Marketing Course In Noida can be a game-changer. Thanks for sharing this!

    ReplyDelete