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 ?
- 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.
- 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
http://blogs.msdn.com/b/gautamg/archive/2010/02/12/how-to-make-playback-wait-for-certain-event.aspx