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.

taking snapshots of browser windows

lausserlausser Members
edited November -1 in Sahi - Open Source
Hi,
i wanted to share what i just set up.
function takeShapshot() {
   var $scriptPath = ('' + ScriptRunner.getScript().getFilePath());
   if ($scriptPath.search(/\\/)) { // Windows
     var $arrScriptPath = $scriptPath.split(/\\/);
   } else {
     var $arrScriptPath = $scriptPath.split(/\//);
   }
   var $testScript = $arrScriptPath.pop().replace(/\.sah/,'');
   var $now = new Date();
   var $snapfile = $testScript + '_' +
       $now.getFullYear() + '_' +
       String("0" + $now.getMonth() + 1).slice(-2) + '_' +
       String("0" + $now.getDay()).slice(-2) + '__' +
       String("0" + $now.getHours()).slice(-2) + '_' +
       String("0" + $now.getMinutes()).slice(-2) + '_' +
       String("0" + $now.getSeconds()).slice(-2) + '.jpg';
   // this gives you a filename like <scriptname>_2009_04_09__14_59_12.jpg
   // the date/time would ideally be the same as the protocol file
   // first i wanted to use this:  
   //var $snapfilefile = _callServer("net.sf.sahi.util.Utils.createLogFileName") + '.jpg';
   // but i didn't manage it to work
  _execute('\"C:/Program Files/IrfanView/i_view32.exe\" /capture=2 ' +  '/convert=C:\\\\Users\\nagios\\\\sahi\\\\snapshots\\\\' + $snapfile);
}
You need the excellent image processing tool from www.irfanview.com for this. Now when you call snapshot() in your tests, you'll find a picture of the failed page in your snapshots/ directory.
There's another thread about how to get browser windows to execute in the foreground.

Gerhard

Comments

  • narayannarayan Administrators
    Gerhard, that rocks! Thanks a ton for sharing!
    Regards,
    Narayan
  • Wow! Thats great work done :)
  • Thanks for the reply to my "_callServer"-post. This makes the function simpler and the screenshot's filename now corresponds to the logfile.
    function takeShapshot() {
       var $testScript = ('' + ScriptRunner.getScript().getScriptName()).replace(/\.sah/,'');
       var $snapfile = Packages.net.sf.sahi.util.Utils.createLogFileName($testScript) + '.jpg';
      _execute('\"C:/Program Files/IrfanView/i_view32.exe\" /capture=2 ' + '/convert=C:\\\\Users\\nagios\\\\sahi\\\\snapshots\\\\' + $snapfile);
    }
    
    e.g. app_web_naprax_contact_check_feedback__09Apr2009__18_33_30.jpg

    Gerhard
  • Can anyone post the java equivalent for the above implementation please ?

    Also, is it possible that this code would work even if:
    1. The browser window is minimized
    2. There are multiple instances of the browser open.
    Would it capture the exact screen where the error was caused.

    Thanks,
    Ajay
  • is there another way in taking snapshots..,, and it can be shown on log reports???
  • Hi benj0143,

    Have a look at : http://sahi.co.in/forums/viewtopic.php?id=1200

    Also, there is a API called

    _takeSnapShot(); and

    _takeSnapShots(toggle); which will take snapshot and show it on the log reports.This is available only in Sahi Pro version.

    Regards
    Sumitra
  • lausser: Your code works fine for me when I playback a sahi script from Sahi-pro controller. But when I try to execute the script from a Java program no images are getting captured. What could be the reason for this? My Java code:-

    import java.io.IOException;

    import net.sf.sahi.test.TestRunner;

    public class SahiScriptLauncher {

    public static void main(String[] args) throws IOException, InterruptedException {
    // TODO Auto-generated method stub


    final String suiteName = "path_to / abcd.sah"; //consider here a path to file

    final String browserType = "chrome";

    String base = "url"; //consider an actual url here

    String threads = "1";

    TestRunner testRunner = new TestRunner(suiteName, browserType, base, threads);

    String status = testRunner.execute();



    }

    }
Sign In or Register to comment.