18 January 2026:


This forum is now archived and is in read-only mode. Please continue discussions on our improved new Sahi Pro Community forum.



Sahi Pro is an enterprise grade test automation platform which can automate web, mobile, API, windows and java based applications and SAP.

How to take screenshot in PhantomJS?

merlinmerlin Members
edited March 2014 in Sahi Pro
When I run a script in Sahi through PhantomJS and If a script fails then the screenshot which I get is my desktop snap.
I am trying to get the failed scenario screenshot but not able to proceed.. so need help

Best Answer

  • Well, thats a tricky one, it is possible though.
    Have a look at http://phantomjs.org/documentation/ there you will find something on how to take screenshots. The tricky part is telling when to take the screenshot.
    You may use the web server module. http://phantomjs.org/api/webserver/
    Just create a server which when requested will take a screenshot. In Sahis onScriptFailure _execute() a script which calls the phantomjs server (e.g. on linux with wget or curl).

    Would this be a solution for you? And could you please post an example here when you get it to run?
    Regards
    Wormi

Answers

  • merlinmerlin Members
    Can you give detail description about how to call a server from onScriptFailure _execute() as m new to this topic and trying to figure out this..
  • globalwormingglobalworming Moderators
    edited March 2014
    Look in the sahi documentation for callback functions. There is an _onScriptFailure API. When the script fails you call something like _execute("wget localhost:$phantomPort) and this will triger the screenshot
  • merlinmerlin Members
    hi
    m not able to proceed, I need more help on this topic as phantomjs people are telling there is no such option and should use SlimerJS as it has the bowser window on-screen option.
    please suggest some other way
    Thank You
  • Why would the phantomjs team redirect you to slimerjs?
    It's all in the docs:
    https://github.com/ariya/phantomjs/wiki/API-Reference-WebServer
    http://phantomjs.org/screen-capture.html
    http://daker.me/2013/07/testing-your-responsive-design-with-phantomjs.html

    and that's all you should need to figure it out.

    I could write you all the code that's needed, but I would consider this work and would charge you for that. If that's ok for you, send me a mail and we talk rates.

    Regards
    Wormi
  • Wormi,
    phantomjs takes url of the page to take screenshot? what if this page requires authentication? phantonjs will not have that when it opens that url. There will be session going on in your current browser with particular jsessionid. I have done lot of research on this. i dont think you can take sceen capture of errored out page from your current browser window using phantomjs.
  • Well, I successfully run suites on phantomjs every day.
    To take a screenshot, you just call page.render(). The naviagtion is provided by sahi which also handles authentication.

    Ok so some code:
    phantomjs (sahi.js) script:
    if (phantom.args.length === 0) {
      console.log('Usage: sahi.js <Sahi Playback Start URL>');
      phantom.exit();
    } else {
      var address = phantom.args[0];
      console.log('Loading ' + address);
      var page = new WebPage();
      page.open(address, function(status) {
        if (status === 'success') {
          page.viewportSize = { width: 1000, height: 1000 };
          page.render('/tmp/screenShot' + i +  '.png');
          i++;
          var title = page.evaluate(function() {
            return document.title;
          });
          console.log('Page title is ' + title);
        } else {
          console.log('FAIL to load the address');
        }
      });
    }
    
    browser.xml:
        <browserType>
            <name>phantomjs</name>
            <displayName>PhantomJS</displayName>
            <icon>safari.png</icon>
            <path>/usr/bin/phantomjs</path>
            <options>--proxy=localhost:9999 $userDir/../extlib/phantomjs/sahi.js</options>
            <processName>phantomjs</processName>
            <capacity>100</capacity>
            <useSystemProxy>false</useSystemProxy>
        </browserType>
    

    now when running a suite, a screenshot from every site will be taken and written to /tmp.
    And as I said, it's all in the doc, there are blogposts about this etc. Why should this not work?
Sign In or Register to comment.