Forum Discussion

JeffSm's avatar
JeffSm
Contributor
15 years ago

how do you capture a screen and output to a file?

I have a need to do a screen capture at various points during my test.  I can have TestComplete capture the screen, launch mspaint, then paste the clipboard info to mspaint and save.  I've figured out how to do all this just using shortcut keys.  But my problem is that I need to test my application in 6 different languages, and the shortcuts are not always the same for every function.  How can I in TestComplete capture a screen shot, then save as a .jpeg file to the PC's file system?  I don't need to compare it to anything, just need to be able to view later.



I'm running TestComplete 7.52



Thanks,

Jeff

9 Replies

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    Hey, Jeff,



    The simplest solution is to use the Picture.SaveToFile method.  Check the online help documentation for some of the more complicated details but a very easy test (in DelphiScript) looks like this:



    procedure TestPictureStuff;



    var

        PictObj;



    begin

    PictObj := Sys.Desktop;



    PictObj.Picture.SaveToFile('C:\TEMP\Desktop.jpg');

    end;
  • Hi Robert,



    I'm fairly new to TestComplete, and I'm writing my scripts in jscript.  I've adapted what you suggested as the following, but I just get an exception (Object doesn't support this property or method).  How should this look in jscript?  Is there a way to just capture my application window, rather than the entire desktop?




    function picture()


    picture()

    {


    var PictObj;


    PictObj = Sys.Desktop;


    PictObj.Picture.SaveToFile("C:\\TEMP\\test.jpg");


    }



    Thanks!

    Jeff


  • Picture is a method of onscreen objects such as the desktop. That method returns an object of type Picture, which in turn has a SaveToFile method. You need to add () to call the Picture method, then call the SaveToFile method on the object you get back, as follows:



    function picture()

    {

       var PictObj;

       PictObj = Sys.Desktop;

       PictObj.Picture().SaveToFile("C:\\TEMP\\test.jpg");

    }



    This is an equivalent function, which may be a bit clearer:


    function picture()

    {

       var pic = Sys.Desktop.Picture();



       pic.SaveToFile("C:\\TEMP\\test.jpg");

    }





  • Hi Tony,



    Thanks for the suggestion.  It really helped.  What you suggested worked great, but what I really want is to only capture the application window.  Your suggestion got me headed in the right direction.  Here's what I ended up with, and gets me exactly what I'm looking for.




    var pic = Sys.Desktop.ActiveWindow();


    pic = .Desktop.ActiveWindow();

    Regions.AddPicture(pic, "test.jpg");


    Regions.GetPicture("test.jpg").SaveToFile("C:\\TEMP\\test.jpg");



    Jeff

  • Hi Tony,



    Thanks for the suggestion.  It really helped.  What you suggested worked great, but what I really want is to only capture the application window.  Your suggestion got me headed in the right direction.  Here's what I ended up with, and gets me exactly what I'm looking for.




    var pic = Sys.Desktop.ActiveWindow();



    Regions.AddPicture(pic, "test.jpg");


    Regions.GetPicture("test.jpg").SaveToFile("C:\\TEMP\\test.jpg");



    Jeff

  • Hi Tony,



    This solution has been working perfectly for me.  But now for some reason I'm having a problem and I don't understand what's happened.  When I run my scripts, I'm getting an error "Unable to add the image. Failed to save the image."  If a .jpg image file exists in the regions store before I run the script with the name as what's in the script , it's removed during execution.  It seems that I can't write to the regions store during script execution.  Do you have any idea why this would be the case.  As I said, this has worked fine, but now I'm having a problem.



    Thanks,

    Jeff
  • Hi Jeff,


    To help us investigate the problem, please zip your entire project suite folder along with the log of the failed test execution and send us the archive via our Contact Support form (http://www.automatedqa.com/support/message). Make sure that messages in your log correspond to the latest version of your tests.

  • What does this code look like in VBScript?  I'm trying to do the same thing, but am using VBScript instead and am new at using TestComplete and writing scripts period :)
  • Figured it out:



    Sub Main


    Sys.Desktop.Picture.SaveToFile "C:\TEMP\Desktop.jpg"


    End Sub


        

    Thanks!