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.

Take screenshots with Sahi

edited November -1 in Sahi - Open Source
Hi,

Is it possible to insert "brake point" in Sahi script to request a screenshot of browser window under test?

Similar solution was developed for Selenium (binil.wordpress.com/2006/12/22/taking-screenshots-with-selenium/)

Thanks for support.
Rob
Tagged:

Comments

  • Hi,

    I am not sure of your exact requirements but I remember there was a thread in the forum mentioning how we can use eclipse IDE to to create and execute sahi scripts. If you can configure it for sahi than I hope we can use the break point facility of the IDE wherever required.
    Another quick fix that I can think of as of now (if u wanna take screenshots manually) is that put an alert statement just before where you want to take a screenshot. The script will halt there waiting for you to take action on the alert. You can drag the alert aside and take screen shot. A bit weired but it works!

    Regards,
    Pankaj
  • I think this was the thread for eclipse details
    http://sahi.co.in/forums/viewtopic.php?id=1051
  • Pankaj,

    Thank you for your answer.
    My requirement is to automate test and save screenshot images at crucial points during test execution for future evaluation by human.

    Your suggestion about using alerts is really good, but would it be possible to call an external utility to take a screenshot at "brake point" inside the script?

    Thanks
    Rob
  • I am just guessing but i think you can think on these lines. I wonder how much it is practically possible.
    Use _execute() command to run some printscreen utility that can be run through commandline.
  • dpdp Members
    following thread should help you

    http://sahi.co.in/forums/viewtopic.php?id=888
  • narayannarayan Administrators
    I am not sure about a break point, but you can invoke a snapshot using this small function:
    function takeSnapShot(){
        try {
            var robot = new java.awt.Robot();
            var toolkit = new java.awt.Toolkit.getDefaultToolkit();
            var screenSize = toolkit.getScreenSize();
            var screenRect = new java.awt.Rectangle(0, 0, screenSize.width, screenSize.height);
            var image = robot.createScreenCapture(screenRect);
    
            var file = new java.io.File("D:/path/snapshot.png");
            javax.imageio.ImageIO.write(image, "png", file);
        } catch (e) {
            print(e);
        }
    }
    
    // Call it
    takeSnapShot();
    

    This will work only if you are running tests in a single thread, since other windows may come on top of this during multithreaded playback.
  • narayannarayan Administrators
    You will need to call takeSnapShot from a recovery function.

    http://sahi.co.in/w/_setrecovery-recovering-from-a-scenario-without-try-catch

    Ex.

    _setRecovery(takeSnapShot);

    will call this function whenever there is a failure or error.
  • JaoBrJaoBr Members
    Thanks Narayan.

    Here it only worked with an 'error', not with an 'failure'.
    I have only the textbox "name", I did the following:
    _popup("mysystem")._setValue(_textbox("name"), "JaoBr");
    _popup("mysystem")._assertNotEqual("JaoBr, _textbox("name").value);
    

    It's an failure, but the recovery function wasn't called.

    But when I do the following:
    _popup("mysystem")._setValue(_textbox("name"), "JaoBr");
    _popup("mysystem")._assertNotEqual("JaoBr, _textbox("namee").value);
    

    It calls the recovery function because it's an error.

    Also, it doesn't work with the _continueOnError function? I don't want my script to be stoped.
  • keatskeats Members
    I am getting some error when i am using the solution given by narayan above for capturing screen shot. Can someone help me to resolve this please?
    I am using this function in my script.
    function takeSnapShotNew($Filename){

    try {
    var robot = new java.awt.Robot();
    var toolkit = new java.awt.Toolkit.getDefaultToolkit();
    var screenSize = toolkit.getScreenSize();
    var screenRect = new java.awt.Rectangle(0, 0, screenSize.width, screenSize.height);
    var image = robot.createScreenCapture(screenRect);
    var file = new java.io.File($Filename+".png");

    javax.imageio.ImageIO.write(image, "png", file);
    _debug("after image io");
    } catch (e) {
    print(e);
    }
    }


    When i run this function i get following error on sahi console,
    Rhino lib:ReferenceError: "javax" is not defined.

    It seem to go to catch after this line
    javax.imageio.ImageIO.write(image, "png", file);


    I am not sure why i am getting this. I worked with my dev team here and they tried placing rt.lib file in possible lib directory but in vain.

    Thanks.
  • beesmanbeesman Members
    edited August 2010
    hi

    i capture like this:
    1) download winshot
    http://www.woodybells.com/winshot.html
    2)
    public class GuiCamera {
        
        private String fileName;
    
        private String defaultName = "GuiCamera";
    
        static int serialNum = 0;
    
        private String imageFormat;
    
        private String defaultImageFormat = "png";
    
        Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
        //Dimension dc = Toolkit.getDefaultToolkit().get
        
    
        /***************************************************************************
         * The default construct will use the default Image file surname
         * "GuiCamera", and default image format "png"
         **************************************************************************/
        public GuiCamera() {
            fileName = defaultName;
            imageFormat = defaultImageFormat;
            
    //        GuiCamera.class.getName();
        }
    
        /***************************************************************************
         * @param s
         *            the surname of the snapshot file
         * @param format
         *            the format of the image file, it can be "jpg" or "png"
         * 
         **************************************************************************/
        public GuiCamera(String s, String format) {
            fileName = s;
            imageFormat = format;
        }
    
        /***************************************************************************
         * snapShot the Gui once
         **************************************************************************/
        public void snapShot() {
            snapShot(0, 0,    (int) d.getWidth(), (int) d.getHeight());
        }
        
        /***************************************************************************
         * snapShot the Gui once
         **************************************************************************/
        public void snapShot(int x, int y, int width, int height) {
            try {
                // 
                BufferedImage screenshot = (new Robot()).createScreenCapture(new Rectangle(x, y,
                        width, height));
                serialNum++;
                // 
                String name = fileName + String.valueOf(serialNum) + "." + imageFormat;
                File f = new File(name);
                System.out.print("Save File " + name);
                // 
                ImageIO.write(screenshot, imageFormat, f);
                System.out.print("..Finished! ");
            } catch (Exception ex) {
                System.out.println(ex);
            }
        }
        
    
        public static void main(String[] args) {
            winshot();
            //GuiCamera cam = new GuiCamera("Test", "png");
            
            //GuiCamera.getInstance("C:\\temp\\Test").snapShot();
            System.out.println(System.getProperty("java.home"));
            //snapIt();
            //System.out.println((int)"&".charAt(0));
            //System.exit(status)
            //cam.snapShot();
        }
        
        public static GuiCamera getInstance(String path){
            return new GuiCamera(path, "png");
        }
        
        public static void snapIt(){
            GuiCamera.getInstance("C:\\temp\\Test").snapShot();
        }
        
        public static void xxxx(){
            System.out.println("test..............");
        }
    
        public static void winshot(){
            try {
                java.awt.Robot robot = new java.awt.Robot();
                robot.keyPress(java.awt.event.KeyEvent.VK_ALT); 
                robot.keyPress(java.awt.event.KeyEvent.VK_PRINTSCREEN );
                //robot.keyPress(KeyEvent.VK_P );    
                robot.keyRelease(java.awt.event.KeyEvent.VK_ALT);
                //robot.keyPress(KeyEvent.VK_P );
                robot.keyRelease(java.awt.event.KeyEvent.VK_PRINTSCREEN);
                Thread.sleep(500);
            } catch (Exception e) {
                System.out.println(e);
                e.printStackTrace();
            }
        }
    }
    
    function winshot(){
        while (_condition(document.readyState != "complete")) {
            _wait(500);   
        }
    
        Packages.net.sf.sahi.capture.GuiCamera.winshot();
    }
    
    
    3)screen will be capture by winshot
  • keatskeats Members
    ok .thanks.
  • narayannarayan Administrators
    Keats,

    javax not being found is mostly due to wrong version of Java. Check the output of
    java -version
    and add the correct Java to your PATH. That should mostly fix the issue.

    Regards,
    Narayan
  • shobboshobbo Members
    edited December 2010
    narayan wrote:
    (..) This will work only if you are running tests in a single thread, since other windows may come on top of this during multithreaded playback.

    I had a problem running one test at a time but I found the solution.
    As I was using browser_types.xml and -threads didnt work as expected I did a little research and found capacity tag in the mentioned xml file.
  • sakkusakku Members
    Thanks to all .The snap shot function is very helpful for me ....
  • Hi All,

    Even though i am running my test in single thread , browser is getting minimized and not able to take screen shot of browser . Please suggest a way to keep my browser active (or how to maximize the window)

    Regards,
    Nithin
  • Hi All,

    yes, im also facing same problem, when we minimize the browser it is not able to take screenshot of browser,

    when we minimized it is capturing the desktop screenshot instead of browser

    pls let us know, how to take screenshot of browser even when we minimized the browser

    thanks in advance
  • On Unix maschines you can use xvfb and xdotool to manipulate the browser. Some browsers have some "kiosk" or "app" mode, in which they start fullscreen... but hide the URL bar http://peter.sh/experiments/chromium-command-line-switches/#enable-kiosk-mode
    #!/bin/sh
    # call this file from Sahi Script while you are at http://sahi.example.com/_s_/dyn/Driver_initialized
    
    BROWSERNAME=$1
    DISPLAY=:$2
    WID=$(./xdotool search --name "Sahi Start - $1" | head -1)
    ./xdotool windowsize $WID 100% 100%
    exit 1
    
  • Hi,

    Screen shot is working for me only in case if there is any failure in SAHI controller. Like script stopped due to some script. But in case if there is any assert is failing then in that case screen shot is not working.

    here goes my code-

    function takeSnapShot($ScrenShot){
    try {
    var robot = new java.awt.Robot();
    var toolkit = new java.awt.Toolkit.getDefaultToolkit();
    var screenSize = toolkit.getScreenSize();
    var screenRect = new java.awt.Rectangle(0, 0, screenSize.width, screenSize.height);
    var image = robot.createScreenCapture(screenRect);

    var file = new java.io.File("D:/path/$ScreenShot.png");
    javax.imageio.ImageIO.write(image, "png", file);
    } catch (e) {
    System.out.print(""+e);

    }
    }
  • Hello Narayan,

    My screen shot function is working in case of failure not in error below is the script-
    function takeSnapShot($ScrenShot){
    try {
    var robot = new java.awt.Robot();
    var toolkit = new java.awt.Toolkit.getDefaultToolkit();
    var screenSize = toolkit.getScreenSize();
    var screenRect = new java.awt.Rectangle(0, 0, screenSize.width, screenSize.height);
    var image = robot.createScreenCapture(screenRect);

    var file = new java.io.File("D:/path/$ScreenShot.png");
    javax.imageio.ImageIO.write(image, "png", file);
    } catch (e) {
    System.out.print(""+e);

    }
    }

    Error- in asserton fail case the function is not calling but moving forward to next step.

    Failure- failure message in sahi controller and take the screen shot and execution also stop.

    So my question is how can i take the screen shot in case of any assertion fails and how come once i saw a failure message on sahi it should move to next step for another transaction.

    Please guide me.

    Gyanadeep
Sign In or Register to comment.