Home > Programming, Test Automation > How to: Take a screenshot with CodedUI and C#

How to: Take a screenshot with CodedUI and C#

Hi guys,

Last week I was on holidays, for this reason I haven’t posted anything. So, I will continue with VM scripts and what you can do with them next week. Today, I will post a very, very, very old code that I have done when I was building a framework for a desktop application with Coded UI and C#. It is a function to take screenshots in your automation.

I will refactor this code other time, because I know that it is not following all the best practices, but for now you can copy and change where you want.

The code is on my github:


First: you can see what are the parameters that you need to send:  (PublicFunctions Values, string _stringMessage, Boolean FULLSCREEN = false, Boolean FAIL = false)

Second: intFail is a failure counter and stringPath is a variable that you need to update with the path where will be placed your screenshot

Third: Thread.Sleep(2000) you can remove this and put something like wait for, it is not the best solution use Thread.Sleep

Fourth: This part will take the FULLSCREEN or not, depending what you have sent

Fifth: This part is just formatting the date/time.

Sixth: Now it is saving the picture with the path and the date/time. It is missing a try/catch here, please don’t forget to put in your code xD

Seventh: It is clearing the clipboard for the next screenshot

Eighth: It is counting the failure and catching the failure message

Ninth: The last step is inserting the failure into the database with all the information that you are passing

Thank you guys ! See you next week 🙂

  1. August 29, 2015 at 12:11 pm

    I have read so many content concerning the blogger lovers however this paragraph is in fact
    a good piece of writing, keep it up.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: