Open your application with CodedUI and C#

Hello guys, I will post a snippet code which you can use to open your Desktop Windows Application and start your automated test. You can see the code on my github account too. Do not forget to change the path of your application and maybe the timeout to load the application.

 

using System;
using System.Diagnostics;
using Microsoft.VisualStudio.TestTools.UITesting.WinControls;
using Microsoft.VisualStudio.TestTools.UITesting.Playback;
using System.Windows.Forms;

namespace ProjectName
{
	public class Application{

	        public static void Open()
	        {
	            System.Diagnostics.Process proc = new System.Diagnostics.
Process();
	            proc.EnableRaisingEvents = false;
	            proc.StartInfo.FileName = "C:\\Users\\yourUser\\AppData
\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\UI.appref-ms";
	            try
	            	{ 				
	             	   proc.Start();
			   WaitApplicationLoad();
			}
 		    catch (Exception e)
	             	{
	                   MessageBox.Show(e.Message);
	               	}
	        }

	        public static bool WaitApplicationLoad(){
	        	WinButton button = new WinButton();
			while(!button.WaitForControlExist())
			{
				PlayBack.Wait(5000);
			}
	        }	
	}
}

 

Why are you using Playback instead of the Thread.Sleep ?

  1. There is PlaybackSettings.ThinkTimeMultiplier which you can modify your sleep.  By default this variable is 1 but you can increase\decrease it to change the wait time all over the code.  For example, if you are specifically testing over slow network (some other slow performance case), you can change this variable at one place (or even in the configuration file) to 1.5 to add 50% extra wait at all places.
  2. Playback.Wait() internally calls Thread.Sleep() (after above computation) in smaller chunks in a for-loop while checking for user cancel\break operation.  In other words, Wait() lets you cancel playback before the end of the wait whereas sleep might not or throw exception.

I’ve found this code to open the application, but to be honest it’s a recorded code, which means that it’s generated automatically when you are recording the manual clicks on your application. I don’t recommend because with the recording comes a lot of trash code. But you can change the names and remove the trash of the code anyway.

 

   public class UISwitcherProDesktopTPWindow : WinWindow
    {

        public UISwitcherProDesktopTPWindow()
        {
            #region Search Criteria
            this.SearchProperties[WinWindow.PropertyNames.Name] = 
"Application Name";
            this.SearchProperties.Add(new PropertyExpression(WinWindow.
PropertyNames.ClassName, "WindowsForms10.Window", 
PropertyExpressionOperator.Contains));
            this.WindowTitles.Add("Application Desktop");
            #endregion
        }
    }

 

Thank you guys, it’s just this for today. See you next week 🙂

 

References:

https://github.com/rafaelaazevedo/CodedUI/blob/master/OpenApplication.cs

https://social.msdn.microsoft.com/Forums/en-US/a42cf655-e202-4bd4-82e2-036d7015cab5/how-do-i-make-codedui-wait-for-application-to-fully-load?forum=vsautotest

https://social.msdn.microsoft.com/Forums/en-US/a2a0d838-c55e-4304-a508-b66f79f9da69/waitforcontrolexist-returns-false-event-if-control-available-on-screen?forum=vsautotest

http://blogs.msdn.com/b/gautamg/archive/2010/02/12/how-to-make-playback-wait-for-certain-event.aspx

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:

https://github.com/rafaelaazevedo/CodedUI/blob/master/PrintScreen.cs

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 🙂