Forum Discussion

kcarr86's avatar
kcarr86
Occasional Contributor
2 years ago
Solved

Save a Screenshot of Application window

Hello, I am fairly new to TestComplete and have been exclusively working with Keyword Tests. I'm trying to take a screenshot of an application window and then save that file to a folder on my computer. I've listed the attempts I've made below:

 

1. I tried incorporating the Snipping Tool app into my test recording. I received the following error: "The object 'ScreenClippingControl' does not exist"

2. I tried using the PrintScreen button in my test recording but it defaulted to the Snipping Tool app. Same problem as #1.

3. I tried using the Post Screenshot operation in my test but it only posts a picture to the test log. I want a .png or .jpeg of the screenshot downloaded to a folder on my computer.

4. I attempted to convert my Keyword Test into a script. After searching through this forum, I tried using the SaveToFile method. No file was generated in the folder I specified. My code is below (CAPS signifying placeholders for client-sensitive material):

 

var pic = MYPHOTO.Picture();

pic.SaveToFile("C:\Users\MYNAME\Desktop\Test Docs\Test Screenshots")

 

5. I attempted to add another code solution I found on the forums. I received a "TypeError: This is not a function" when I entered the following code:

 

Sys.Desktop.Picture.SaveToFile("C:\Users\MYNAME\Desktop\Test Docs\Test Screenshots\Desktop.jpg")

 

In summary, I'm certain I'm missing a crucial step but I'm not sure what I'm doing wrong and what option is going to produce the desired outcome. Can any TestComplete experts help out this beginner?

 

 

  • You can use this to capture the entire screen and save it to where your project logs are saved (ProjectSuite/Project/Log/Current_DateTime_Folder/Picture1.png):

     

    Log.Picture(Sys.Desktop.Picture(), "Image of the error");

     

    and if you need to move the file to another directory you can do the following:

     

    aqFileSystem.MoveFile(Log.Picture(Sys.Desktop.Picture(), "Image moved to C:\temp"), "C:\\temp\\"); 

     

    NOTE: this will automatically remove the image from the TC posted log, so it won't display in the TC app, but will instead be moved to the directory you specify (in this case C:\temp)

     OR better yet, copy the file and you get it in both folders:

     

    aqFileSystem.CopyFile(Log.Picture(Sys.Desktop.Picture(), "Image copied to C:\temp"), "C:\\temp\\"); 

     

     

     

  • I think it would be easier for you to use a Data Driven Loop. This will give you a much easier way to access your Excel information.

    https://support.smartbear.com/testcomplete/docs/keyword-testing/basic/data-driven-loops.html

    You can use a string variable to build the file name that you want from the Excel data and then use that variable in your path.

    mypathstring = myexceldata + ".jpg"

    Using intermediate steps like this seems like extra work sometimes but it's worth a little time here in case you need to troubleshoot when things aren't working.

8 Replies

  • Kitt's avatar
    Kitt
    Regular Contributor

    You can use this to capture the entire screen and save it to where your project logs are saved (ProjectSuite/Project/Log/Current_DateTime_Folder/Picture1.png):

     

    Log.Picture(Sys.Desktop.Picture(), "Image of the error");

     

    and if you need to move the file to another directory you can do the following:

     

    aqFileSystem.MoveFile(Log.Picture(Sys.Desktop.Picture(), "Image moved to C:\temp"), "C:\\temp\\"); 

     

    NOTE: this will automatically remove the image from the TC posted log, so it won't display in the TC app, but will instead be moved to the directory you specify (in this case C:\temp)

     OR better yet, copy the file and you get it in both folders:

     

    aqFileSystem.CopyFile(Log.Picture(Sys.Desktop.Picture(), "Image copied to C:\temp"), "C:\\temp\\"); 

     

     

     

    • kcarr86's avatar
      kcarr86
      Occasional Contributor

      Thank you so much for the advice, the MoveFile method resolved the issue of downloading the screenshot to the folder of my choice!

      The new problem I have now is turning this script into a loop that renames each screenshot, ideally mapping the unique ID of each app window to the file name. I'm using the RenameFile method and this is how I've set up the script so far:

      Log.Picture(Sys.Desktop.Picture(), "test1.jpg");
      aqFileSystem.RenameFile(Log.Picture(Sys.Desktop.Picture(), "test1.jpg"), "C:\\Users\\MYNAME\\Desktop\\Test Docs\\Test Screenshots\\test1.jpg")

      My loop is designed to pull ID numbers from an excel sheet to open an ID-specific app window, take a screenshot, and save it to the designated folder with the ID number as the file name. The code below is used to pull the IDs in the excel sheet:

      Aliases.MYAPP.frmMainMenu.SplitContainer1.SplitterPanel2.ctlMain.ucConsumerSearch.gcConsumers.TextEdit.SetText(Project.Variables.Var1.Value("ConsumerID"))

      Is it possible to insert that code into the NewPath argument for the Rename method? Or is there a better way to map the ID value in the excel sheet to the screenshot filename?

      Note: Apologies for the message formatting, my reply keeps getting blocked by an invalid HTML message error.

      • Marsha_R's avatar
        Marsha_R
        Champion Level 3

        I think it would be easier for you to use a Data Driven Loop. This will give you a much easier way to access your Excel information.

        https://support.smartbear.com/testcomplete/docs/keyword-testing/basic/data-driven-loops.html

        You can use a string variable to build the file name that you want from the Excel data and then use that variable in your path.

        mypathstring = myexceldata + ".jpg"

        Using intermediate steps like this seems like extra work sometimes but it's worth a little time here in case you need to troubleshoot when things aren't working.

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    If you're new to TC and you're working with Keyword testing, a good starting point is to read through Keyword Tests. In there, you'll will come across Working With Images in Basic Tasks, which explains how to capture images.