Ask a Question

Save a Screenshot of Application window

SOLVED
kcarr86
New Contributor

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?

 

 

8 REPLIES 8
Marsha_R
Community Hero

If you are looking at this one

https://community.smartbear.com/t5/TestComplete-Questions/how-do-you-capture-a-screen-and-output-to-...

 

then in your step 4 you left out the Sys.Desktop part


Marsha_R
[Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
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)

8Kit_0-1669760728295.png

 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\\"); 

 

 

 

[[ kITt: keep IT testing ]]
rraghvani
Valued Contributor

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.

kcarr86
New 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.

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.


Marsha_R
[Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame

Hi Marsha_R,

Thank you for your help so far. I've set up the Data Driven Loop to pull the IDs from my Excel file but I'm not sure where to implement your variable solution. This is how I've interpreted your advice:

var mypathstring = EXCELDATA + ".jpg"

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

Would I just need to reference the name of the Excel file to create the variable and then call the variable as part of my file path in the RenameFile method as illustrated above? Or would I need to create a variable for each 5-digit ID in my file shown below:

var mypathstring = "11111" + ".jpg"

var mypathstring2 = "22222" + ".jpg"

var mypathstring3 = "33333" + ".jpg"

something like this should work for you

var mypathstring = EXCELDATA + ".jpg"

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

You will reuse mypathstring with each iteration of the loop. You could also just use test.jpg for your picture since you are going to rename it each time anyway.


Marsha_R
[Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame

Success!

Thank you for all of your help! Here is how I solved the issue:

mypathstring = Project.Variables.Var1.Value("ConsumerID") +".jpg"

Since the Data Driven Loop wizard converted my Excel file into the bolded value above, I was then able to successfully use that to reproduce Marsha_R's solution in the RenameFile method below:

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

The test produced jpg files that were named according to the ID field in my Excel document as desired. Thank you again for your assistance!

cancel
Showing results for 
Search instead for 
Did you mean: