Home
%3CLINGO-SUB%20id%3D%22lingo-sub-825352%22%20slang%3D%22en-US%22%3EUI%20testing%20for%20Windows%20apps%20with%20WinAppDriver%20and%20Appium%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-825352%22%20slang%3D%22en-US%22%3E%3CP%3EIn%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2FWindows-Dev-AppConsult%2FAdd-telemetry-to-your-Electron-application-with-Application%2Fba-p%2F802091%22%20target%3D%22_blank%22%20rel%3D%22noopener%22%3Eanother%20post%20on%20this%20blog%3C%2FA%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Ewe%20have%20seen%20how%20telemetry%20is%20important%20to%20implement%20a%20good%20DevOps%20story%20for%20your%20software%20projects.%3C%2FP%3E%0A%3CP%3EAnother%20important%20piece%20of%20the%20puzzle%20is%20testing.%20In%20order%20to%20be%20agile%20and%20deliver%20fast%2C%20we%20need%20to%20make%20sure%20that%20the%20quality%20bar%20is%20always%20met.%20This%20is%20where%20automated%20testing%20comes%20in.%20When%20we%20commit%20new%20code%20to%20our%20repository%2C%20we%20need%20to%20make%20sure%20we%20haven't%20broken%20anything%20before%20releasing%20a%20new%20version%20of%20our%20application.%20When%20we%20implement%20testing%2C%20typically%20we%20focus%20only%20on%20the%20business%20logic%20side.%20This%20is%20why%20we%20often%20adopt%20unit%20testing%2C%20which%20takes%20our%20logic%20and%20split%20it%20into%20small%20pieces%2C%20which%20are%20tested%20separately%20and%20independently.%20However%2C%20client%20applications%20aren't%20made%20only%20by%20business%20logic%2C%20but%20also%20the%20user%20interface%20plays%20an%20important%20role.%20The%20business%20logic%20of%20our%20application%2C%20in%20fact%2C%20can%20be%20bug%20free%2C%20but%20there%20may%20be%20visual%20issues%20that%20we%20have%20missed%20because%20they%20happen%20only%20on%20a%20specific%20combination%20of%20resolution%2C%20screen%20size%2C%20etc.%20For%20example%2C%20if%20our%20UI%20doesn't%20fully%20support%20devices%20with%20high%20DPIs%2C%20an%20important%20button%20or%20another%20actionable%20item%20might%20not%20be%20visible%2C%20preventing%20our%20customers%20to%20use%20the%20application.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EHowever%2C%20UI%20testing%20isn't%20%22easy%22%20to%20do%20as%20unit%20testing.%20The%20full%20application%20must%20be%20up%20%26amp%3B%20running%2C%20while%20unit%20testing%20takes%20care%20of%20performing%20small%20tasks%2C%20which%20should%20be%20as%20much%20isolated%20and%20independent%20from%20each%20other.%20Additionally%2C%20unit%20testing%20tests%20only%20code%2C%20so%20to%20trigger%20the%20execution%20you%20just%20need%20to%20invoke%20that%20code%20(a%20method%2C%20a%20class%2C%20etc.).%20In%20UI%20testing%2C%20instead%2C%20you%20need%20to%20actually%20interact%20with%20the%20application%2C%20in%20order%20to%20trigger%20events.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EPlease%20welcome%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fmicrosoft%2FWinAppDriver%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EWindows%20Application%20Driver%3C%2FA%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E(from%20now%20on%2C%20WinAppDriver)%2C%20which%20is%20an%20open%20source%20service%20which%20supports%20performing%20UI%20automation%20against%20Windows%20applications%2C%20regardless%20of%20the%20technology%20used%20to%20build%20it%20(UWP%2C%20WPF%2C%20Windows%20Forms%2C%20Win32%2C%20etc.).%20Under%20the%20hood%2C%20WinAppDriver%20is%20nothing%20more%20than%20a%20REST%20service%2C%20which%20supports%20performing%20commands%20against%20a%20Windows%20application%20by%20leveraging%20HTTP%20methods%20and%20JSON%20payloads.%20However%2C%20thanks%20to%20a%20testing%20framework%20called%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22http%3A%2F%2Fappium.io%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EAppium%3C%2FA%3E%2C%20we%20can%20use%20a%20dedicated%20SDK%20to%20simulate%20commands%20and%20actions%20executed%20by%20our%20application.%20We%20can%20simulate%20the%20pressing%20of%20a%20button%3B%20or%20the%20typing%20of%20a%20text%20inside%20a%20TextBox%3B%20etc.%20This%20way%2C%20we%20can%20simulate%20the%20most%20common%20flow%20of%20actions%20and%20interactions%20performed%20by%20the%20user%20and%20make%20sure%20that%20everything%20works%20as%20expected.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ERecently%2C%20WinAppDriver%20has%20also%20been%20added%20to%20the%20Azure%20DevOps%20offering.%20The%20service%2C%20in%20fact%2C%20comes%20pre-installed%20on%20every%20Windows%20hosted%20agent%20and%2C%20with%20a%20dedicated%20task%2C%20we%20can%20simply%20start%20and%20stop%20it%20whenever%20we%20need%20it.%20This%20way%2C%20we%20can%20include%20UI%20testing%20as%20part%20of%20our%20CI%2FCD%20pipeline%20and%20make%20sure%20that%20all%20the%20UI%20tests%20are%20executed%20whenever%20we%20commit%20new%20code%20to%20our%20repository.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20this%20post%20we're%20going%20to%20learn%20the%20basics%20of%20adding%20UI%20tests%20to%20a%20WPF%20application%2C%20by%20leveraging%20WinAppDriver%20and%20Appium.%20In%20the%20next%20post%2C%20instead%2C%20we're%20going%20to%20learn%20how%20to%20integrate%20this%20flow%20into%20our%20CI%2FCD%20pipeline.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22lets-build-a-wpf-app%22%20id%3D%22toc-hId-1621841589%22%20id%3D%22toc-hId-1621841589%22%3ELet's%20build%20a%20WPF%20app!%3C%2FH3%3E%0A%3CP%3EAs%20starting%20point%2C%20we're%20going%20to%20build%20a%20simple%20WPF%20app%2C%20which%20we're%20going%20to%20put%20under%20UI%20testing.%20The%20choice%20between%20the%20full%20.NET%20Framework%20or%20.NET%20Core%203.0%20is%20up%20to%20you%3A%20Appium%20relies%20on%20the%20visual%20elements%20of%20the%20application%2C%20not%20on%20the%20technology%20leveraged%20to%20build%20it.%20The%20purpose%20of%20the%20app%20is%20really%20simple%3A%20in%20a%20TextBox%20we're%20going%20to%20write%20our%20name%3B%20by%20pressing%20a%20button%2C%20we're%20going%20to%20display%20a%20greeting%20message%20in%20the%20form%20of%20%22Hello%22%20followed%20by%20the%20name%20you've%20typed.%20Here%20is%20the%20UI%3A%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20id%3D%22pragma-line-20%22%20class%3D%22language-xml%20hljs%22%3E%3CSPAN%20class%3D%22hljs-tag%22%3E%26lt%3B%3CSPAN%20class%3D%22hljs-name%22%3EWindow%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-attr%22%3Ex%3AClass%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22WpfUiTesting.Wpf.MainWindow%22%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-attr%22%3Exmlns%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22http%3A%2F%2Fschemas.microsoft.com%2Fwinfx%2F2006%2Fxaml%2Fpresentation%22%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-attr%22%3Exmlns%3Ax%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22http%3A%2F%2Fschemas.microsoft.com%2Fwinfx%2F2006%2Fxaml%22%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-attr%22%3Exmlns%3Ad%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22http%3A%2F%2Fschemas.microsoft.com%2Fexpression%2Fblend%2F2008%22%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-attr%22%3Exmlns%3Amc%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22http%3A%2F%2Fschemas.openxmlformats.org%2Fmarkup-compatibility%2F2006%22%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-attr%22%3Exmlns%3Alocal%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22clr-namespace%3AWpfUiTesting.Wpf%22%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-attr%22%3Emc%3AIgnorable%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22d%22%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-attr%22%3ETitle%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22MainWindow%22%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-attr%22%3EHeight%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22450%22%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-attr%22%3EWidth%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22800%22%3C%2FSPAN%3E%26gt%3B%3C%2FSPAN%3E%0A%20%20%20%20%0A%20%20%20%20%3CSPAN%20class%3D%22hljs-tag%22%3E%26lt%3B%3CSPAN%20class%3D%22hljs-name%22%3EStackPanel%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-attr%22%3Ex%3AName%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22StackRoot%22%3C%2FSPAN%3E%26gt%3B%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-tag%22%3E%26lt%3B%3CSPAN%20class%3D%22hljs-name%22%3ETextBlock%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-attr%22%3EText%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22Enter%20your%20name%3A%22%3C%2FSPAN%3E%20%2F%26gt%3B%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-tag%22%3E%26lt%3B%3CSPAN%20class%3D%22hljs-name%22%3ETextBox%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-attr%22%3Ex%3AName%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22txtName%22%3C%2FSPAN%3E%20%26gt%3B%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-tag%22%3E%26lt%3B%3CSPAN%20class%3D%22hljs-name%22%3EButton%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-attr%22%3EClick%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22OnSayHello%22%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-attr%22%3Ex%3AName%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22sayHelloButton%22%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-attr%22%3EContent%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22Say%20hello%22%3C%2FSPAN%3E%20%2F%26gt%3B%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-tag%22%3E%26lt%3B%3CSPAN%20class%3D%22hljs-name%22%3ETextBlock%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-attr%22%3Ex%3AName%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22txtResult%22%3C%2FSPAN%3E%20%2F%26gt%3B%3C%2FSPAN%3E%0A%20%20%20%20%3CSPAN%20class%3D%22hljs-tag%22%3EStackPanel%3C%2FSPAN%3E%26gt%3B%0A%3CSPAN%20class%3D%22hljs-tag%22%3EWindow%3C%2FSPAN%3E%26gt%3B%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3ENothing%20fancy%3A%20a%20TextBox%20where%20to%20type%20the%20name%3B%20a%20button%20to%20trigger%20the%20action%3B%20a%20TextBlock%20to%20display%20the%20greeting%20message.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20786px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F128786iA3D6FBDF06B5CAB5%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22MainApp.png%22%20title%3D%22MainApp.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EAs%20you%20can%20guess%2C%20also%20the%20event%20handler%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EOnSayHello%3C%2FSTRONG%3E%2C%20which%20is%20executed%20when%20the%20button%20is%20pressed%2C%20is%20simple%20as%20well%3A%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20id%3D%22pragma-line-45%22%20class%3D%22language-csharp%20hljs%22%3E%3CSPAN%20class%3D%22hljs-function%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Eprivate%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Evoid%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-title%22%3EOnSayHello%3C%2FSPAN%3E(%3CSPAN%20class%3D%22hljs-params%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Eobject%3C%2FSPAN%3E%20sender%2C%20RoutedEventArgs%20e%3C%2FSPAN%3E)%3C%2FSPAN%3E%0A%7B%0A%20%20%20%20txtResult.Text%20%3D%20%3CSPAN%20class%3D%22hljs-string%22%3E%24%22Hello%20%3CSPAN%20class%3D%22hljs-subst%22%3E%7BtxtName.Text%7D%3C%2FSPAN%3E%22%3C%2FSPAN%3E%3B%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CH3%20id%3D%22lets-add-some-ui-testing%22%20id%3D%22toc-hId--930315372%22%20id%3D%22toc-hId--930315372%22%3ELet's%20add%20some%20UI%20testing%3C%2FH3%3E%0A%3CP%3ENow%20let's%20add%20some%20testing.%20We%20want%20to%20make%20sure%20that%2C%20whenever%20the%20user%20presses%20the%20%22Say%20hello%22%20button%2C%20the%20TextBlock%20named%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EtxtResult%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Econtains%20effectively%20the%20string%20%22Hello%22%20followed%20by%20the%20value%20typed%20by%20the%20user%20in%20the%20TextBox%20named%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EtxtName%3C%2FSTRONG%3E.%20The%20first%20step%20is%20to%20create%20a%20testing%20project.%20Appium%20offers%20a%20SDK%20which%20allows%20to%20identify%20UI%20elements%20on%20the%20screen%20and%20to%20interact%20with%20them%2C%20leveraging%20WinAppDriver.%20As%20such%2C%20it%20isn't%20tight%20to%20a%20specific%20testing%20framework%2C%20but%20we%20can%20use%20the%20one%20we%20prefer%20most.%20In%20my%20case%2C%20I'm%20going%20to%20use%20the%20inbox%20Visual%20Studio%20solution%2C%20which%20is%20MSTest.%20Right%20click%20on%20your%20solution%20and%20a%20new%20project%20which%20type%20is%20Unit%20Test%20Project%20(.NET%20Framework)%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F128787i38BEFF7A75ADFD26%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22unittestproject.png%22%20title%3D%22unittestproject.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3ENow%20we%20need%20to%20install%20the%20Appium%20SDK%2C%20which%20will%20give%20us%20classes%20and%20methods%20to%20interact%20with%20the%20UI%20of%20the%20application.%20Right%20click%20on%20the%20test%20project%2C%20choose%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EManage%20NuGet%20packages%3C%2FSTRONG%3E%3CSPAN%3E%2C%20look%20for%20and%20install%20the%20package%20called%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fwww.nuget.org%2Fpackages%2FAppium.WebDriver%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3EAppium.WebDriver%3C%2FA%3E%3CSPAN%3E.%20Before%20starting%20to%20actually%20write%20some%20tests%2C%20we%20need%20to%20do%20some%20preparation!%20First%2C%20we%20need%20WinAppDriver%20up%20%26amp%3B%20running%2C%20since%20it%20acts%20as%20a%20middle%20man%20between%20our%20tests%20and%20the%20WPF%20application.%20We%20can%20download%20it%20from%20the%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fmicrosoft%2FWinAppDriver%2Freleases%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EReleases%3C%2FA%3E%3CSPAN%3E%26nbsp%3Bsection%20of%20the%20GitHub%20project.%20Be%20aware%2C%20you%20need%20to%20download%20the%20service%2C%20not%20WinAppDriver%20UI%20Recorder%2C%20which%20is%20a%20different%20tool.%20At%20the%20time%20of%20writing%2C%201.1.1%20is%20the%20latest%20release.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F128788i8FC03995C748FD04%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22winappdriverdownloaad.png%22%20title%3D%22winappdriverdownloaad.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3EAfter%20you%20have%20downloaded%20the%20MSI%20and%20executed%20it%2C%20you%20will%20find%20the%20service%20installed%20at%20the%20location%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3Ec%3A%5CProgram%20Files%20(x86)%5CWindows%20Application%20Driver%3C%2FSTRONG%3E.%20Go%20there%20and%20double%20click%20on%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EWinAppDriver.exe%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Efile%20to%20launch%20it.%20The%20service%20is%20a%20command%20line%20tool%2C%20so%20you%20will%20see%20a%20command%20prompt%20appearing%20with%20the%20information%20about%20the%20URL%20and%20the%20port%20which%20the%20service%20is%20listening%20to.%20The%20default%20one%20is%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3E%3CA%20href%3D%22http%3A%2F%2F127.0.0.1%3A4723%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttp%3A%2F%2F127.0.0.1%3A4723%2F%3C%2FA%3E%3C%2FSTRONG%3E.%20The%20command%20prompt%20will%20act%20also%20as%20a%20logger%3A%20every%20time%20the%20Appium%20SDK%20will%20perform%20any%20interaction%2C%20we%20will%20see%20the%20underline%20HTTP%20requests%20and%20JSON%20payloads%20being%20displayed%20in%20the%20window.%3C%2FP%3E%0A%3CH4%20id%3D%22initialize-the-session%22%20id%3D%22toc-hId-615981458%22%20id%3D%22toc-hId-615981458%22%3E%3CA%20id%3D%22pragma-line-66%22%20target%3D%22_blank%22%3E%3C%2FA%3EInitialize%20the%20session%3C%2FH4%3E%0A%3CP%3EBefore%20starting%20a%20test%2C%20we%20need%20to%20initialize%20an%20Appium%20session%3A%20we%20need%20to%20specify%20which%20is%20the%20application%20we%20want%20to%20launch%2C%20the%20target%20platform%2C%20the%20WinAppDriver%20URL%2C%20etc.%20We%20do%20this%20inside%20a%20method%20decorated%20with%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3E%5BClassInitialize%5D%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eattribute%2C%20which%20is%20the%20one%20used%20by%20MSTest%20to%20define%20a%20method%20that%20must%20be%20invoked%20every%20time%20we%20start%20a%20new%20testing%20session.%20Let's%20take%20a%20look%20at%20the%20code%2C%20which%20we'll%20comment%20later%3A%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20id%3D%22pragma-line-70%22%20class%3D%22language-csharp%20hljs%22%3E%5B%3CSPAN%20class%3D%22hljs-meta%22%3ETestClass%3C%2FSPAN%3E%5D%0A%3CSPAN%20class%3D%22hljs-keyword%22%3Epublic%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Eclass%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-title%22%3EWpfTest%3C%2FSPAN%3E%0A%7B%0A%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Eprotected%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Econst%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Estring%3C%2FSPAN%3E%20WindowsApplicationDriverUrl%20%3D%20%3CSPAN%20class%3D%22hljs-string%22%3E%22http%3A%2F%2F127.0.0.1%3A4723%22%3C%2FSPAN%3E%3B%0A%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Eprivate%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Econst%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Estring%3C%2FSPAN%3E%20WpfAppId%20%3D%20%3CSPAN%20class%3D%22hljs-string%22%3E%40%22C%3A%5CUsers%5Cmpagani%5CSource%5CSamples%5CWpfUiTesting%5CWpfUiTesting.Wpf%5Cbin%5CDebug%5CWpfUiTesting.Wpf.exe%22%3C%2FSPAN%3E%3B%0A%0A%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Eprotected%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Estatic%3C%2FSPAN%3E%20WindowsDriver%3CWINDOWSELEMENT%3E%20session%3B%0A%0A%20%20%5B%3CSPAN%20class%3D%22hljs-meta%22%3EClassInitialize%3C%2FSPAN%3E%5D%0A%20%20%3CSPAN%20class%3D%22hljs-function%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Epublic%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Estatic%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Evoid%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-title%22%3ESetup%3C%2FSPAN%3E(%3CSPAN%20class%3D%22hljs-params%22%3ETestContext%20context%3C%2FSPAN%3E)%3C%2FSPAN%3E%0A%20%20%7B%0A%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Eif%3C%2FSPAN%3E%20(session%20%3D%3D%20%3CSPAN%20class%3D%22hljs-literal%22%3Enull%3C%2FSPAN%3E)%0A%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Evar%3C%2FSPAN%3E%20appiumOptions%20%3D%20%3CSPAN%20class%3D%22hljs-keyword%22%3Enew%3C%2FSPAN%3E%20AppiumOptions()%3B%0A%20%20%20%20%20%20%20%20%20%20appiumOptions.AddAdditionalCapability(%3CSPAN%20class%3D%22hljs-string%22%3E%22app%22%3C%2FSPAN%3E%2C%20WpfAppId)%3B%0A%20%20%20%20%20%20%20%20%20%20appiumOptions.AddAdditionalCapability(%3CSPAN%20class%3D%22hljs-string%22%3E%22deviceName%22%3C%2FSPAN%3E%2C%20%3CSPAN%20class%3D%22hljs-string%22%3E%22WindowsPC%22%3C%2FSPAN%3E)%3B%0A%20%20%20%20%20%20%20%20%20%20session%20%3D%20%3CSPAN%20class%3D%22hljs-keyword%22%3Enew%3C%2FSPAN%3E%20WindowsDriver%3CWINDOWSELEMENT%3E(%3CSPAN%20class%3D%22hljs-keyword%22%3Enew%3C%2FSPAN%3E%20Uri(WindowsApplicationDriverUrl)%2C%20appiumOptions)%3B%0A%0A%20%20%20%20%20%20%7D%0A%20%20%7D%0A%7D%0A%3C%2FWINDOWSELEMENT%3E%3C%2FWINDOWSELEMENT%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EWe%20define%20a%20couple%20of%20constants%20to%20hold%20some%20information.%20The%20first%20one%2C%20called%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EWindowsApplicationDriverUrl%3C%2FSTRONG%3E%2C%20is%20the%20URL%20where%20the%20WinAppDriver%20service%20is%20available%20(do%20you%20remember%20the%20URL%20we%20have%20seen%20when%20we%20have%20launched%20the%20service%20in%20the%20previous%20section%3F).%20The%20second%20one%2C%20called%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EWpfAppId%3C%2FSTRONG%3E%2C%20is%20the%20identifier%20of%20the%20application%20which%2C%20in%20case%20of%20a%20Win32%20one%2C%20is%20the%20full%20path%20of%20the%20executable.%20In%20this%20case%2C%20it's%20the%20folder%20which%20contains%20the%20build%20output%20of%20the%20WPF%20application%20we%20have%20previously%20created.%20Lastly%2C%20we%20define%20a%20static%20property%20to%20store%20our%20session%2C%20which%20type%20is%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EWindowsDriver%3C%2FSTRONG%3E.%20These%20are%20actually%20classes%20coming%20from%20the%20Appium%20SDK%3A%20we%20are%20using%20the%20driver%20for%20Windows%20to%20connect%20to%20one%20or%20more%20application%20windows%20(sorry%20if%20it%20sounds%20like%20a%20bad%20joke%20%3D)).%3C%2FP%3E%0A%3CP%3EThe%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3ESetup()%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Emethod%20is%20the%20one%20decorated%20with%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3E%5BClassInitialize%5D%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eattribute%2C%20which%20means%20that%20it%20will%20be%20executed%20before%20each%20test%20session.%20To%20bootstrap%20a%20session%2C%20Appium%20supports%20multiple%20parameters%2C%20which%20are%20handled%20with%20the%20collection%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EAppiumOptions%3C%2FSTRONG%3E.%20The%20only%20required%20one%20is%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3Eapp%3C%2FSTRONG%3E%2C%20which%20must%20contain%20the%20identifier%20of%20the%20application%20we%20want%20to%20test.%20In%20case%20of%20a%20Win32%20application%2C%20it's%20the%20full%20path%20of%20the%20executable.%20For%20clarity%2C%20we%20also%20specify%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EdeviceName%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eone%2C%20which%20tells%20to%20the%20framework%20which%20platform%20our%20application%20is%20targeting%20(in%20our%20case%2C%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EWindowsPC%3C%2FSTRONG%3E).%20Appium%2C%20in%20fact%2C%20isn't%20a%20testing%20framework%20only%20for%20Windows%20desktop%20apps%2C%20but%20it%20fully%20supports%20also%20mobile%20apps%20and%20web%20apps.%20In%20the%20end%2C%20we%20start%20a%20new%20session%20by%20creating%20a%20new%20instance%20of%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EWindowsDriver%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eobject%2C%20passing%20a%20parameter%20the%20WinAppDriver%20url%20and%20the%20options%20we%20have%20just%20created.%3C%2FP%3E%0A%3CH4%20id%3D%22toc-hId--1936175503%22%20id%3D%22toc-hId--1936175503%22%3E%26nbsp%3B%3C%2FH4%3E%0A%3CH4%20id%3D%22making-our-app-testable%22%20id%3D%22toc-hId--193365168%22%20id%3D%22toc-hId--193365168%22%3EMaking%20our%20app%20testable%3C%2FH4%3E%0A%3CP%3EAs%20already%20mentioned%2C%20the%20way%20Appium%20works%20is%20that%2C%20through%20WinAppDriver%2C%20is%20able%20to%20walk%20through%20the%20tree%20of%20visual%20object%20which%20compose%20the%20window%20of%20our%20application%20and%20interact%20with%20the%20various%20controls%20in%20an%20automated%20way%2C%20without%20any%20user%20interaction.%20Let's%20take%20a%20look%20at%20the%20window%20of%20the%20application%20we%20have%20built%20to%20better%20understand%20the%20scenario.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20786px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F128790i1D78DC1DA2E728E6%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22TestingApp.png%22%20title%3D%22TestingApp.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThere%20are%203%20controls%20we%20need%20to%20interact%20with%3A%3C%2FP%3E%0A%3CUL%20id%3D%22pragma-line-107%22%3E%0A%3CLI%20id%3D%22pragma-line-107%22%3EThe%20TextBox%20where%20to%20type%20the%20name%3C%2FLI%3E%0A%3CLI%20id%3D%22pragma-line-108%22%3EThe%20Button%20to%20trigger%20the%20interaction%3C%2FLI%3E%0A%3CLI%20id%3D%22pragma-line-109%22%3EThe%20TextBlock%20where%20the%20result%20will%20appear%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3EWe%20don't%20need%2C%20for%20example%2C%20to%20interact%20with%20the%20first%20TextBlock%2C%20which%20is%20just%20a%20header%20with%20an%20informative%20text.%20Or%20with%20the%20window%20itself.%20As%20such%2C%20the%20first%20problem%20which%20arises%20is%20finding%20the%20right%20way%20to%20identify%20which%20objects%20we%20are%20interested%20to.%3C%2FP%3E%0A%3CP%3EThe%20session%20object%20we%20have%20built%20in%20the%20previous%20section%20provides%20multiple%20methods%20to%20find%20elements%20in%20the%20window%2C%20suitable%20for%20different%20type%20of%20applications.%20For%20example%2C%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EFindElementByTagName()%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eand%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EFindElementByCssSelector()%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Emethods%20are%20dedicated%20to%20web%20applications.%20The%20one%20that%20we%20can%20leverage%20for%20a%20desktop%20application%20is%20called%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EFindElementByAccessibilityId()%3C%2FSTRONG%3E%2C%20which%20uses%20the%20automation%20properties%20supported%20by%20Windows%20to%20identify%20a%20specific%20element%20for%20accessibility%20purposes.%20So%2C%20before%20writing%20the%20tests%2C%20let's%20wire%20some%20of%20these%20properties.%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20id%3D%22pragma-line-115%22%20class%3D%22language-xml%20hljs%22%3E%3CSPAN%20class%3D%22hljs-tag%22%3E%26lt%3B%3CSPAN%20class%3D%22hljs-name%22%3EWindow%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-attr%22%3Ex%3AClass%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22WpfUiTesting.Wpf.MainWindow%22%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-attr%22%3Exmlns%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22http%3A%2F%2Fschemas.microsoft.com%2Fwinfx%2F2006%2Fxaml%2Fpresentation%22%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-attr%22%3Exmlns%3Ax%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22http%3A%2F%2Fschemas.microsoft.com%2Fwinfx%2F2006%2Fxaml%22%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-attr%22%3Exmlns%3Ad%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22http%3A%2F%2Fschemas.microsoft.com%2Fexpression%2Fblend%2F2008%22%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-attr%22%3Exmlns%3Amc%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22http%3A%2F%2Fschemas.openxmlformats.org%2Fmarkup-compatibility%2F2006%22%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-attr%22%3Exmlns%3Alocal%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22clr-namespace%3AWpfUiTesting.Wpf%22%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-attr%22%3Emc%3AIgnorable%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22d%22%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-attr%22%3ETitle%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22MainWindow%22%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-attr%22%3EHeight%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22450%22%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-attr%22%3EWidth%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22800%22%3C%2FSPAN%3E%26gt%3B%3C%2FSPAN%3E%0A%20%20%20%20%0A%20%20%20%20%3CSPAN%20class%3D%22hljs-tag%22%3E%26lt%3B%3CSPAN%20class%3D%22hljs-name%22%3EStackPanel%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-attr%22%3Ex%3AName%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22StackRoot%22%3C%2FSPAN%3E%26gt%3B%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-tag%22%3E%26lt%3B%3CSPAN%20class%3D%22hljs-name%22%3ETextBlock%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-attr%22%3EText%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22Enter%20your%20name%3A%22%3C%2FSPAN%3E%20%2F%26gt%3B%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-tag%22%3E%26lt%3B%3CSPAN%20class%3D%22hljs-name%22%3ETextBox%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-attr%22%3Ex%3AName%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22txtName%22%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-attr%22%3EAutomationProperties.AutomationId%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22txtName%22%3C%2FSPAN%3E%2F%26gt%3B%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-tag%22%3E%26lt%3B%3CSPAN%20class%3D%22hljs-name%22%3EButton%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-attr%22%3EClick%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22OnSayHello%22%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-attr%22%3EAutomationProperties.AutomationId%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22sayHelloButton%22%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-attr%22%3EContent%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22Say%20hello%22%3C%2FSPAN%3E%20%20%2F%26gt%3B%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-tag%22%3E%26lt%3B%3CSPAN%20class%3D%22hljs-name%22%3ETextBlock%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-attr%22%3Ex%3AName%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22txtResult%22%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-attr%22%3EAutomationProperties.AutomationId%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22txtResult%22%3C%2FSPAN%3E%20%2F%26gt%3B%3C%2FSPAN%3E%0A%20%20%20%20%3CSPAN%20class%3D%22hljs-tag%22%3EStackPanel%3C%2FSPAN%3E%26gt%3B%0A%3CSPAN%20class%3D%22hljs-tag%22%3EWindow%3C%2FSPAN%3E%26gt%3B%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EAs%20you%20can%20see%2C%20we%20have%20assigned%20an%20identifier%20to%20the%20controls%20we%20need%20to%20interact%20with%2C%20using%20the%20attached%20property%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EAutomationProperties.AutomationId%3C%2FSTRONG%3E.%20Now%20we%20can%20reference%20with%20the%20controls%20from%20the%20Appium%20SDK%20using%20these%20identifiers.%20Let's%20create%20a%20first%20simple%20test%3A%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20id%3D%22pragma-line-136%22%20class%3D%22language-csharp%20hljs%22%3E%5B%3CSPAN%20class%3D%22hljs-meta%22%3ETestMethod%3C%2FSPAN%3E%5D%0A%3CSPAN%20class%3D%22hljs-function%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Epublic%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Evoid%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-title%22%3EAddNameToTextBox%3C%2FSPAN%3E()%3C%2FSPAN%3E%0A%7B%0A%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Evar%3C%2FSPAN%3E%20txtName%20%3D%20session.FindElementByAccessibilityId(%3CSPAN%20class%3D%22hljs-string%22%3E%22txtName%22%3C%2FSPAN%3E)%3B%0A%20%20%20%20txtName.SendKeys(%3CSPAN%20class%3D%22hljs-string%22%3E%22Matteo%22%3C%2FSPAN%3E)%3B%0A%20%20%20%20session.FindElementByAccessibilityId(%3CSPAN%20class%3D%22hljs-string%22%3E%22sayHelloButton%22%3C%2FSPAN%3E).Click()%3B%0A%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Evar%3C%2FSPAN%3E%20txtResult%20%3D%20session.FindElementByAccessibilityId(%3CSPAN%20class%3D%22hljs-string%22%3E%22txtResult%22%3C%2FSPAN%3E)%3B%0A%20%20%20%20Assert.AreEqual(txtResult.Text%2C%20%3CSPAN%20class%3D%22hljs-string%22%3E%24%22Hello%20%3CSPAN%20class%3D%22hljs-subst%22%3E%7BtxtName.Text%7D%3C%2FSPAN%3E%22%3C%2FSPAN%3E)%3B%0A%7D%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EThe%20purpose%20of%20this%20test%20is%20to%20verify%20that%20the%20basic%20UI%20interaction%20of%20the%20application%20works%20well.%20We%20want%20to%20type%20something%20in%20the%20TextBox%2C%20press%20the%20button%20and%20make%20sure%20that%20the%20TextBlock%20will%20display%20the%20message%20%22Hello%22%20followed%20by%20what%20we%20have%20typed.%3C%2FP%3E%0A%3CP%3EYou%20can%20assign%20to%20the%20method%20the%20name%20you%20prefer.%20The%20important%20part%20is%20that%20you%20mark%20it%20with%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3E%5BTestMethod%5D%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eattribute%2C%20which%20is%20the%20one%20used%20by%20MSTest%20to%20identify%20which%20are%20the%20actual%20tests%20to%20execute.%20Inside%20the%20test%2C%20we%20leverage%20the%20session%20we%20have%20previously%20created%20and%20the%20identifiers%20we%20have%20previously%20assigned%20in%20XAML%20to%20get%20a%20reference%20to%20the%20controls%20we%20want%20to%20interact%20with.%20We%20just%20need%20to%20invoke%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EFindElementByAccesibilityId()%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Emethod%2C%20followed%20by%20its%20identifier.%20Once%20we%20have%20the%20reference%2C%20the%20SDK%20offers%20many%20methods%20to%20simulate%20the%20most%20common%20type%20of%20interactions.%20You%20can%20see%20two%20of%20them%20in%20the%20code%3A%3C%2FP%3E%0A%3CUL%20id%3D%22pragma-line-152%22%3E%0A%3CLI%20id%3D%22pragma-line-152%22%3EWe%20simulate%20the%20user%20typing%20something%2C%20by%20calling%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3ESendKeys()%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Emethod%20on%20the%20TextBlock.%20As%20parameter%2C%20we%20pass%20the%20text%20we%20want%20to%20type%2C%20in%20my%20case%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EMatteo%3C%2FSTRONG%3E.%3C%2FLI%3E%0A%3CLI%20id%3D%22pragma-line-153%22%3EWe%20simulate%20the%20button%20click%2C%20by%20calling%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EClick()%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Emethod%20on%20the%20Button.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3EIn%20the%20end%2C%20we%20use%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EAssert.AreEqual()%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Emethod%20of%20MSTest%20to%20check%20if%20the%20string%20stored%20in%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EText%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eproperty%20of%20the%20TextBlock%20actually%20matches%20the%20string%20%22Hello%22%20followed%20by%20the%20content%20of%20the%20TextBox.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22testing-the-test-%22%20id%3D%22toc-hId-1745958672%22%20id%3D%22toc-hId-1745958672%22%3ETesting%20the%20test%20%3D)%3C%2Fimg%3E%3C%2FH3%3E%0A%3CP%3EBefore%20running%20the%20test%2C%20we%20need%20to%20make%20sure%3A%3C%2FP%3E%0A%3CUL%20id%3D%22pragma-line-160%22%3E%0A%3CLI%20id%3D%22pragma-line-160%22%3EWinAppDriver%20is%20up%20%26amp%3B%20running.%20If%20not%2C%20make%20sure%20to%20double%20click%20on%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EWinAppDriver.exe%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Ein%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EC%3A%5CProgram%20Files%20(x86)%5CWindows%20Application%20Driver%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Efolder.%3C%2FLI%3E%0A%3CLI%20id%3D%22pragma-line-161%22%3EWe%20have%20compiled%20at%20least%20once%20the%20WPF%20application.%20Building%20the%20tests%2C%20in%20fact%2C%20doesn't%20automatically%20trigger%20the%20compilation%20of%20the%20application.%20Make%20sure%20that%20the%20executable%20is%20stored%20in%20the%20same%20path%20you%20have%20specified%20in%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EWpfAppId%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Econstant%20declared%20in%20the%20test%20class.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3ENow%20right%20click%20on%20the%20test%20project%20and%20choose%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3ERun%20Tests%3C%2FSTRONG%3E.%20If%20you%20have%20done%20everything%20properly%2C%20you%20should%20see%20some%20voodoo%20magic%20happening%20%3D)The%20WPF%20application%20will%20start%2C%20then%20you%20will%20see%20the%20text%20%22Matteo%22%20being%20automatically%20typed%20inside%20the%20TextBox%20and%20the%20button%20being%20clicked.%20If%20you%20now%20open%20the%20Test%20Explorer%20tab%20in%20Visual%20Studio%20(you%20can%20find%20it%20under%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EView%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Emenu)%2C%20you%20should%20see%20that%20the%20test%20is%20passing%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20686px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F128791i84522F600B29F952%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22TestExplorer.png%22%20title%3D%22TestExplorer.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EIf%20you%20open%20the%20command%20prompt%20where%20WinAppDriver%20is%20running%2C%20you%20will%20see%20the%20HTTP%20calls%20that%20have%20been%20performed%20under%20the%20hood.%20For%20example%2C%20the%20following%20one%20happens%20when%20the%20test%20suite%20tries%20to%20identify%20the%20element%20with%20id%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EtxtName%3C%2FSTRONG%3E%3CSPAN%3E%3A%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20979px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F128793i411A692E351DE026%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22WinAppDriver.png%22%20title%3D%22WinAppDriver.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22improving-our-test-class%22%20id%3D%22toc-hId--806198289%22%20id%3D%22toc-hId--806198289%22%3EImproving%20our%20test%20class%3C%2FH3%3E%0A%3CP%3EEverything%20went%20well%2C%20but%20if%20we%20try%20to%20repeat%20the%20test%20plan%20or%20to%20add%20another%20test%2C%20we%20will%20notice%20some%20not%20so%20great%20behaviors%3A%3C%2FP%3E%0A%3CUL%20id%3D%22pragma-line-175%22%3E%0A%3CLI%20id%3D%22pragma-line-175%22%3EWhen%20the%20test%20session%20is%20completed%2C%20the%20WPF%20application%20is%20left%20open.%20As%20such%2C%20every%20test%20plan%20will%20launch%20a%20new%20instance%20of%20the%20WPF%20application%2C%20leading%20to%20have%20lot%20of%20instances%20of%20the%20application%20running%20at%20the%20same%20time.%3C%2FLI%3E%0A%3CLI%20id%3D%22pragma-line-176%22%3ELet's%20say%20we%20want%20to%20run%20another%20test%20which%20involves%20typing%20something%20else%20in%20the%20TextBox.%20The%20test%20outcome%20will%20be%20compromised%20by%20the%20fact%20that%20the%20TextBox%20already%20includes%20a%20text%2C%20which%20was%20the%20input%20of%20the%20first%20test.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3ETo%20make%20our%20test%20suite%20better%20and%20to%20solve%20these%20problems%2C%20we%20can%20leverage%20two%20features%20of%20MSTest%20(which%20are%20offered%20as%20well%20by%20every%20major%20testing%20suite).%20The%20first%20one%20is%20the%20ability%20to%20define%20a%20test%20initialization%20method%2C%20which%20will%20be%20invoked%20before%20every%20test%20(which%20shouldn't%20be%20confused%20with%20the%20one%20we%20already%20have%20decorated%20with%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3E%5BClassInitialize%5D%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eattribute%2C%20which%20instead%20is%20global%20and%20it's%20invoked%20once%20before%20running%20all%20the%20tests%20in%20the%20suite).%20We%20can%20easily%20define%20a%20method%20that%20will%20clear%20the%20content%20of%20the%20TextBox%2C%20so%20that%20every%20test%20will%20start%20from%20a%20clean%20situation.%20Here%20is%20how%20to%20define%20it%3A%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20id%3D%22pragma-line-180%22%20class%3D%22language-csharp%20hljs%22%3E%5B%3CSPAN%20class%3D%22hljs-meta%22%3ETestInitialize%3C%2FSPAN%3E%5D%0A%3CSPAN%20class%3D%22hljs-function%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Epublic%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Evoid%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-title%22%3EClear%3C%2FSPAN%3E()%3C%2FSPAN%3E%0A%7B%0A%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Evar%3C%2FSPAN%3E%20txtName%20%3D%20session.FindElementByAccessibilityId(%3CSPAN%20class%3D%22hljs-string%22%3E%22txtName%22%3C%2FSPAN%3E)%3B%0A%20%20%20%20txtName.Clear()%3B%0A%7D%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EAll%20we%20need%20to%20do%20is%20to%20mark%20it%20with%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3E%5BTestInitialize%5D%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eattribute.%20Inside%20the%20method%2C%20we%20just%20use%20the%20familiar%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EFindElementByAccessibilityId()%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Emethod%20exposed%20by%20the%20session%20to%20find%20the%20TextBox%20and%20clear%20all%20the%20text%2C%20by%20using%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EClear()%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Emethod.%3C%2FP%3E%0A%3CP%3EThe%20second%20feature%20is%20the%20ability%20to%20define%20a%20cleanup%20method%2C%20which%20will%20be%20invoked%20when%20all%20the%20tests%20have%20been%20executed.%20We're%20going%20to%20use%20it%20to%20close%20the%20application%3A%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20id%3D%22pragma-line-193%22%20class%3D%22language-csharp%20hljs%22%3E%5B%3CSPAN%20class%3D%22hljs-meta%22%3EClassCleanup%3C%2FSPAN%3E%5D%0A%3CSPAN%20class%3D%22hljs-function%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Epublic%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Estatic%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Evoid%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-title%22%3ECleanup%3C%2FSPAN%3E()%3C%2FSPAN%3E%0A%7B%0A%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Eif%3C%2FSPAN%3E%20(session%20!%3D%20%3CSPAN%20class%3D%22hljs-literal%22%3Enull%3C%2FSPAN%3E)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20session.Close()%3B%0A%20%20%20%20%20%20%20%20session.Quit()%3B%0A%20%20%20%20%7D%0A%7D%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EThis%20method%20is%20defined%20as%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3Estatic%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eand%20it's%20marked%20with%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3E%5BClassCleanup%5D%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eattribute.%20Inside%20it%2C%20we%20close%20the%20session%20by%20calling%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EClose()%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Emethod%20and%20then%20we%20quit%20the%20app%2C%20by%20invoking%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EQuit()%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Emethod.%3C%2FP%3E%0A%3CP%3ETo%20make%20sure%20everything%20is%20working%2C%20let's%20add%20another%20test.%20This%20time%2C%20we're%20going%20to%20add%20one%20that%20will%20lead%20to%20a%20failure%3A%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20id%3D%22pragma-line-209%22%20class%3D%22language-csharp%20hljs%22%3E%5B%3CSPAN%20class%3D%22hljs-meta%22%3ETestMethod%3C%2FSPAN%3E%5D%0A%3CSPAN%20class%3D%22hljs-function%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Epublic%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Evoid%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-title%22%3EAddWrongNameToTextBox%3C%2FSPAN%3E()%3C%2FSPAN%3E%0A%7B%0A%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Evar%3C%2FSPAN%3E%20txtName%20%3D%20session.FindElementByAccessibilityId(%3CSPAN%20class%3D%22hljs-string%22%3E%22txtName%22%3C%2FSPAN%3E)%3B%0A%20%20%20%20txtName.SendKeys(%3CSPAN%20class%3D%22hljs-string%22%3E%22Matteo%22%3C%2FSPAN%3E)%3B%0A%20%20%20%20session.FindElementByAccessibilityId(%3CSPAN%20class%3D%22hljs-string%22%3E%22sayHelloButton%22%3C%2FSPAN%3E).Click()%3B%0A%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Evar%3C%2FSPAN%3E%20txtResult%20%3D%20session.FindElementByAccessibilityId(%3CSPAN%20class%3D%22hljs-string%22%3E%22txtResult%22%3C%2FSPAN%3E)%3B%0A%20%20%20%20Assert.AreEqual(txtResult.Text%2C%20%3CSPAN%20class%3D%22hljs-string%22%3E%24%22Hello%20Matt%22%3C%2FSPAN%3E)%3B%0A%7D%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EThis%20test%20is%20similar%20to%20the%20previous%20one.%20The%20only%20difference%20is%20that%20it%20will%20check%20the%20result%20against%20the%20fixed%20string%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EHello%20Matt%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Ewhich%2C%20of%20course%2C%20will%20cause%20a%20failure%20because%20the%20test%20actually%20types%20the%20name%20Matteo%20in%20the%20TextBox.%20Now%20run%20again%20the%20tests%20and%20you%20will%20notice%20two%20things%3A%3C%2FP%3E%0A%3CUL%20id%3D%22pragma-line-224%22%3E%0A%3CLI%20id%3D%22pragma-line-224%22%3EBetween%20the%20execution%20of%20the%20first%20and%20the%20second%20test%2C%20the%20TextBox%20will%20be%20cleared.%3C%2FLI%3E%0A%3CLI%20id%3D%22pragma-line-225%22%3EAt%20the%20end%20of%20the%20test%2C%20the%20application%20will%20be%20closed.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3EAs%20a%20bonus%2C%20we%20can%20use%20the%20failed%20test%20to%20see%20what%20happens%20in%20such%20a%20scenario.%20The%20Test%20Explorer%20tool%20will%20clearly%20display%20the%20exception%20that%20has%20been%20raised%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20697px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F128796i9E98EEE70A5D3901%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22FailedTest.png%22%20title%3D%22FailedTest.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EAs%20you%20can%20see%2C%20the%20message%20is%20pretty%20clear%3A%20the%20TextBlock%20contains%20the%20text%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EHello%20Matteo%3C%2FSTRONG%3E%3CSPAN%3E%2C%20while%20the%20string%20we%20have%20used%20for%20comparison%20is%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EHello%20Matt%3C%2FSTRONG%3E%3CSPAN%3E.%20What%20if%20was%20another%20kind%20of%20exception%2C%20not%20related%20to%20a%20failed%20assert%3F%20Tests%20are%2C%20in%20the%20end%2C%20code%2C%20so%20they%20can%20include%20bugs%20as%20well!%20In%20this%20case%2C%20you%20can%20right%20click%20on%20the%20test%20project%20and%20choose%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EDebug%20Tests%3C%2FSTRONG%3E%3CSPAN%3E.%20A%20real%20debugger%20will%20be%20attached%2C%20allowing%20you%20to%20set%20breakpoints%20or%20to%20analyze%20the%20code%20in%20case%20of%20exceptions%2C%20like%20you%20would%20do%20with%20the%20real%20application.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22testing-a-packaged-application%22%20id%3D%22toc-hId-936612046%22%20id%3D%22toc-hId-936612046%22%3ETesting%20a%20packaged%20application%3C%2FH3%3E%0A%3CP%3EWhich%20is%20the%20best%20way%20to%20deploy%20and%20distribute%20our%20WPF%20application%3F%20MSIX%2C%20of%20course!%20Let's%20package%20our%20application%20then!%20I%20won't%20go%20into%20the%20details%2C%20since%20we%20have%20talked%20about%20MSIX%20packaging%20multiple%20times%20on%20this%20blog.%20If%20you%20need%20some%20guidance%2C%20you%20can%20refer%20to%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fwindows%2Fmsix%2Fdesktop%2Fdesktop-to-uwp-packaging-dot-net%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eofficial%20documentation%3C%2FA%3E.%20Right%20click%20on%20your%20solution%2C%20choose%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EAdd%20%E2%86%92%20New%20Project%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eand%20choose%20the%20Windows%20Application%20Packaging%20Project%20template.%20Once%20it%20has%20been%20created%2C%20right%20click%20on%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EApplications%3C%2FSTRONG%3E%2C%20choose%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EAdd%20reference%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eand%20select%20the%20WPF%20project.%20That's%20it!%3C%2FP%3E%0A%3CP%3EIf%20we%20stop%20for%20a%20minute%20to%20think%2C%20however%2C%20we%20will%20realize%20that%20the%20current%20test%20suite%20isn't%20suitable%20for%20a%20packaged%20application.%20We%20have%20set%2C%20as%20entry%20point%2C%20the%20full%20executable%20deployed%20by%20Visual%20Studio%2C%20but%20this%20isn't%20how%20a%20MSIX%20package%20works.%20You%20don't%20manually%20launch%20an%20executable%2C%20Windows%20does%20that%20for%20you%20based%20on%20the%20entry%20point%20defined%20in%20the%20manifest.%20And%20it%20could%20not%20even%20be%20a%20standard%20Win32%20executable%2C%20since%20WinAppDriver%20supports%20testing%20also%20UWP%20application.%20We%20need%20to%20change%20the%20way%20we%20define%20our%20entry%20point.%20Luckily%2C%20it's%20quite%20easy%3A%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20id%3D%22pragma-line-241%22%20class%3D%22language-csharp%20hljs%22%3E%5B%3CSPAN%20class%3D%22hljs-meta%22%3ETestClass%3C%2FSPAN%3E%5D%0A%3CSPAN%20class%3D%22hljs-keyword%22%3Epublic%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Eclass%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-title%22%3EWpfTest%3C%2FSPAN%3E%0A%7B%0A%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Eprotected%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Econst%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Estring%3C%2FSPAN%3E%20WindowsApplicationDriverUrl%20%3D%20%3CSPAN%20class%3D%22hljs-string%22%3E%22http%3A%2F%2F127.0.0.1%3A4723%22%3C%2FSPAN%3E%3B%0A%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Eprivate%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Econst%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Estring%3C%2FSPAN%3E%20WpfAppId%20%3D%20%3CSPAN%20class%3D%22hljs-string%22%3E%40%22WpfUiTesting_e627vcndsd2rc!App%22%3C%2FSPAN%3E%3B%0A%0A%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Eprotected%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Estatic%3C%2FSPAN%3E%20WindowsDriver%3CWINDOWSELEMENT%3E%20session%3B%0A%0A%20%20%20%20%5B%3CSPAN%20class%3D%22hljs-meta%22%3EClassInitialize%3C%2FSPAN%3E%5D%0A%20%20%20%20%3CSPAN%20class%3D%22hljs-function%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Epublic%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Estatic%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Evoid%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-title%22%3ESetup%3C%2FSPAN%3E(%3CSPAN%20class%3D%22hljs-params%22%3ETestContext%20context%3C%2FSPAN%3E)%3C%2FSPAN%3E%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Eif%3C%2FSPAN%3E%20(session%20%3D%3D%20%3CSPAN%20class%3D%22hljs-literal%22%3Enull%3C%2FSPAN%3E)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Evar%3C%2FSPAN%3E%20appiumOptions%20%3D%20%3CSPAN%20class%3D%22hljs-keyword%22%3Enew%3C%2FSPAN%3E%20AppiumOptions()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20appiumOptions.AddAdditionalCapability(%3CSPAN%20class%3D%22hljs-string%22%3E%22app%22%3C%2FSPAN%3E%2C%20WpfAppId)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20appiumOptions.AddAdditionalCapability(%3CSPAN%20class%3D%22hljs-string%22%3E%22deviceName%22%3C%2FSPAN%3E%2C%20%3CSPAN%20class%3D%22hljs-string%22%3E%22WindowsPC%22%3C%2FSPAN%3E)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20session%20%3D%20%3CSPAN%20class%3D%22hljs-keyword%22%3Enew%3C%2FSPAN%3E%20WindowsDriver%3CWINDOWSELEMENT%3E(%3CSPAN%20class%3D%22hljs-keyword%22%3Enew%3C%2FSPAN%3E%20Uri(WindowsApplicationDriverUrl)%2C%20appiumOptions)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D%0A%3C%2FWINDOWSELEMENT%3E%3C%2FWINDOWSELEMENT%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EThe%20initialization%20code%20is%20the%20same%2C%20except%20for%20the%20identifier%20of%20the%20app%2C%20which%20isn't%20anymore%20the%20full%20path%20of%20the%20executable%2C%20but%20it's%20the%20Package%20Family%20Name%20followed%20by%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3E!App%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Esuffix.%20We%20can%20find%20the%20Package%20Family%20Name%20by%20double%20clicking%20on%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EPackage.appxmanifest%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Efile%20in%20the%20Windows%20Application%20Packaging%20Project%20and%20moving%20to%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EPackaging%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Etab%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20829px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F128797iF2592B5E048989B9%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22PFN.png%22%20title%3D%22PFN.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EIt's%20a%20unique%20value%2C%20generated%20based%20on%20the%20package%20name%20and%20a%20hash%20of%20your%20publisher%20name.%20That's%20it.%20Now%20deploy%20the%20packaged%20application%20at%20least%20once%20(right%20click%20on%20the%20Windows%20Application%20Packaging%20Project%20and%20choose%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EDeploy%3C%2FSTRONG%3E%3CSPAN%3E)%2C%20then%20run%20the%20tests%20again.%20You%20will%20see%20the%20packaged%20version%20of%20the%20app%20starting%20automatically%2C%20ready%20to%20receive%20inputs%20from%20WinAppDriver.%20Great%20job%2C%20everything%20is%20goo..%20well%20%2C%20not%20really.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20683px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F128798i81470DBAD26578C0%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22MSIXerror.png%22%20title%3D%22MSIXerror.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAll%20the%20tests%20are%20now%20failing%20with%20the%20following%20error%3A%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20id%3D%22pragma-line-275%22%20class%3D%22language-text%20hljs%22%3EClass%20Initialization%20method%20WpfUiTesting.Tests.WpfTest.Setup%20threw%20exception.%20OpenQA.Selenium.WebDriverException%3A%20OpenQA.Selenium.WebDriverException%3A%20Failed%20to%20locate%20opened%20application%20window%20with%20appId%3A%20WpfUiTesting_e627vcndsd2rc!App%2C%20and%20processId%3A%2026244%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EThe%20MSIX%20packaging%2C%20unfortunately%2C%20messed%20up%20a%20bit%20our%20testing%20and%2C%20despite%20the%20appId%20and%20the%20processId%20are%20correct%2C%20WinAppDriver%20isn't%20able%20to%20locate%20anymore%20the%20main%20window%20of%20our%20application.%20Are%20we%20doomed%3F%20Of%20course%20not!%20The%20Appium%20SDK%20offers%20an%20option%20to%20create%20a%20desktop%20session%20instead%20of%20an%20app%20session%20which%2C%20instead%20of%20giving%20us%20access%20to%20single%20UI%20elements%20of%20an%20application%2C%20it%20gives%20us%20access%20to%20all%20the%20UI%20elements%20inside%20Windows.%20This%20option%20is%20useful%20when%20we%20need%20to%20test%2C%20for%20example%2C%20special%20activation%20scenarios%2C%20like%20Cortana%20or%20a%20protocol%2C%20in%20which%20the%20starting%20point%20isn't%20the%20application%20traditionally%20opened%20because%20the%20user%20has%20double%20clicked%20on%20an%20icon.%20This%20option%20allows%20us%20also%20to%20implement%20a%20workaround%20for%20our%20blocker.%20Thanks%20to%20a%20desktop%20session%2C%20we%20can%20manually%20look%20for%20the%20window%20of%20our%20application%20and%20get%20a%20reference%20to%20it.%20Using%20this%20approach%20will%20make%20the%20test%20suite%20initialization%20a%20little%20bit%20more%20verbose%2C%20but%20it%20will%20make%20things%20working%20again.%20It%20sounds%20like%20a%20reasonable%20trade-off%20%3D)%3C%2Fimg%3E%3C%2FP%3E%0A%3CP%3EAs%20first%20step%2C%20we%20need%20to%20give%20a%20unique%20identifier%20to%20our%20window.%20We%20can%20use%2C%20again%2C%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EAutomationProperties.AutomationId%3C%2FSTRONG%3E%3A%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20id%3D%22pragma-line-284%22%20class%3D%22language-xml%20hljs%22%3E%3CSPAN%20class%3D%22hljs-tag%22%3E%26lt%3B%3CSPAN%20class%3D%22hljs-name%22%3EWindow%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-attr%22%3Ex%3AClass%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22WpfUiTesting.Wpf.MainWindow%22%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-attr%22%3Exmlns%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22http%3A%2F%2Fschemas.microsoft.com%2Fwinfx%2F2006%2Fxaml%2Fpresentation%22%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-attr%22%3Exmlns%3Ax%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22http%3A%2F%2Fschemas.microsoft.com%2Fwinfx%2F2006%2Fxaml%22%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-attr%22%3Exmlns%3Ad%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22http%3A%2F%2Fschemas.microsoft.com%2Fexpression%2Fblend%2F2008%22%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-attr%22%3Exmlns%3Amc%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22http%3A%2F%2Fschemas.openxmlformats.org%2Fmarkup-compatibility%2F2006%22%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-attr%22%3Exmlns%3Alocal%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22clr-namespace%3AWpfUiTesting.Wpf%22%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-attr%22%3Emc%3AIgnorable%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22d%22%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-attr%22%3EAutomationProperties.AutomationId%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22WpfUITestingMainWindow%22%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-attr%22%3ETitle%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22MainWindow%22%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-attr%22%3EHeight%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22450%22%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-attr%22%3EWidth%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22800%22%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20%26gt%3B%3C%2FSPAN%3E%0A%20%20%20%20%0A%20%20%20%20%3CSPAN%20class%3D%22hljs-tag%22%3E%26lt%3B%3CSPAN%20class%3D%22hljs-name%22%3EStackPanel%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-attr%22%3EMargin%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%2212%22%3C%2FSPAN%3E%26gt%3B%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-tag%22%3E%26lt%3B%3CSPAN%20class%3D%22hljs-name%22%3ETextBlock%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-attr%22%3EText%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22Enter%20your%20name%3A%22%3C%2FSPAN%3E%20%2F%26gt%3B%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-tag%22%3E%26lt%3B%3CSPAN%20class%3D%22hljs-name%22%3ETextBox%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-attr%22%3Ex%3AName%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22txtName%22%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-attr%22%3EAutomationProperties.AutomationId%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22txtName%22%3C%2FSPAN%3E%2F%26gt%3B%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-tag%22%3E%26lt%3B%3CSPAN%20class%3D%22hljs-name%22%3EButton%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-attr%22%3EClick%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22OnSayHello%22%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-attr%22%3EAutomationProperties.AutomationId%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22sayHelloButton%22%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-attr%22%3EContent%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22Say%20hello%22%3C%2FSPAN%3E%20%2F%26gt%3B%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-tag%22%3E%26lt%3B%3CSPAN%20class%3D%22hljs-name%22%3ETextBlock%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-attr%22%3Ex%3AName%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22txtResult%22%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-attr%22%3EAutomationProperties.AutomationId%3C%2FSPAN%3E%3D%3CSPAN%20class%3D%22hljs-string%22%3E%22txtResult%22%3C%2FSPAN%3E%20%2F%26gt%3B%3C%2FSPAN%3E%0A%20%20%20%20%3CSPAN%20class%3D%22hljs-tag%22%3EStackPanel%3C%2FSPAN%3E%26gt%3B%0A%3CSPAN%20class%3D%22hljs-tag%22%3EWindow%3C%2FSPAN%3E%26gt%3B%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EWe%20have%20assigned%20to%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EWindow%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Ethe%20identifier%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EWpfUITestingMainWindow%3C%2FSTRONG%3E%2C%20which%20we're%20going%20to%20use%20later.%20Let's%20take%20a%20look%20at%20the%20updated%20test%20initialization%20code%3A%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20id%3D%22pragma-line-308%22%20class%3D%22language-csharp%20hljs%22%3E%5B%3CSPAN%20class%3D%22hljs-meta%22%3ETestClass%3C%2FSPAN%3E%5D%0A%3CSPAN%20class%3D%22hljs-keyword%22%3Epublic%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Eclass%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-title%22%3EWpfTest%3C%2FSPAN%3E%0A%7B%0A%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Eprotected%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Econst%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Estring%3C%2FSPAN%3E%20WindowsApplicationDriverUrl%20%3D%20%3CSPAN%20class%3D%22hljs-string%22%3E%22http%3A%2F%2F127.0.0.1%3A4723%22%3C%2FSPAN%3E%3B%0A%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Eprivate%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Econst%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Estring%3C%2FSPAN%3E%20WpfAppId%20%3D%20%3CSPAN%20class%3D%22hljs-string%22%3E%40%22WpfUiTesting_e627vcndsd2rc!App%22%3C%2FSPAN%3E%3B%0A%0A%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Eprotected%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Estatic%3C%2FSPAN%3E%20WindowsDriver%3CWINDOWSELEMENT%3E%20session%3B%0A%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Eprotected%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Estatic%3C%2FSPAN%3E%20WindowsDriver%3CWINDOWSELEMENT%3E%20DesktopSession%3B%0A%0A%20%20%20%20%3CSPAN%20class%3D%22hljs-function%22%3E%3CSPAN%20class%3D%22hljs-keyword%22%3Epublic%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Estatic%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-keyword%22%3Evoid%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22hljs-title%22%3ESetup%3C%2FSPAN%3E(%3CSPAN%20class%3D%22hljs-params%22%3ETestContext%20context%3C%2FSPAN%3E)%3C%2FSPAN%3E%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Eif%3C%2FSPAN%3E%20(session%20%3D%3D%20%3CSPAN%20class%3D%22hljs-literal%22%3Enull%3C%2FSPAN%3E)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Evar%3C%2FSPAN%3E%20appiumOptions%20%3D%20%3CSPAN%20class%3D%22hljs-keyword%22%3Enew%3C%2FSPAN%3E%20AppiumOptions()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20appiumOptions.AddAdditionalCapability(%3CSPAN%20class%3D%22hljs-string%22%3E%22app%22%3C%2FSPAN%3E%2C%20WpfAppId)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20appiumOptions.AddAdditionalCapability(%3CSPAN%20class%3D%22hljs-string%22%3E%22deviceName%22%3C%2FSPAN%3E%2C%20%3CSPAN%20class%3D%22hljs-string%22%3E%22WindowsPC%22%3C%2FSPAN%3E)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20DesktopSession%20%3D%20%3CSPAN%20class%3D%22hljs-literal%22%3Enull%3C%2FSPAN%3E%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Etry%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Console.WriteLine(%3CSPAN%20class%3D%22hljs-string%22%3E%22Trying%20to%20Launch%20App%22%3C%2FSPAN%3E)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20DesktopSession%20%3D%20%3CSPAN%20class%3D%22hljs-keyword%22%3Enew%3C%2FSPAN%3E%20WindowsDriver%3CWINDOWSELEMENT%3E(%3CSPAN%20class%3D%22hljs-keyword%22%3Enew%3C%2FSPAN%3E%20Uri(WindowsApplicationDriverUrl)%2C%20appiumOptions)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Ecatch%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Console.WriteLine(%3CSPAN%20class%3D%22hljs-string%22%3E%22Failed%20to%20attach%20to%20app%20session%20(expected).%22%3C%2FSPAN%3E)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20appiumOptions.AddAdditionalCapability(%3CSPAN%20class%3D%22hljs-string%22%3E%22app%22%3C%2FSPAN%3E%2C%20%3CSPAN%20class%3D%22hljs-string%22%3E%22Root%22%3C%2FSPAN%3E)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20DesktopSession%20%3D%20%3CSPAN%20class%3D%22hljs-keyword%22%3Enew%3C%2FSPAN%3E%20WindowsDriver%3CWINDOWSELEMENT%3E(%3CSPAN%20class%3D%22hljs-keyword%22%3Enew%3C%2FSPAN%3E%20Uri(WindowsApplicationDriverUrl)%2C%20appiumOptions)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Evar%3C%2FSPAN%3E%20mainWindow%20%3D%20DesktopSession.FindElementByAccessibilityId(%3CSPAN%20class%3D%22hljs-string%22%3E%22WpfUITestingMainWindow%22%3C%2FSPAN%3E)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%3CSPAN%20class%3D%22hljs-keyword%22%3Evar%3C%2FSPAN%3E%20mainWindowHandle%20%3D%20mainWindow.GetAttribute(%3CSPAN%20class%3D%22hljs-string%22%3E%22NativeWindowHandle%22%3C%2FSPAN%3E)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20mainWindowHandle%20%3D%20(%3CSPAN%20class%3D%22hljs-keyword%22%3Eint%3C%2FSPAN%3E.Parse(mainWindowHandle)).ToString(%3CSPAN%20class%3D%22hljs-string%22%3E%22x%22%3C%2FSPAN%3E)%3B%20%3CSPAN%20class%3D%22hljs-comment%22%3E%2F%2F%20Convert%20to%20Hex%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%20%20%20%20%20appiumOptions%20%3D%20%3CSPAN%20class%3D%22hljs-keyword%22%3Enew%3C%2FSPAN%3E%20AppiumOptions()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20appiumOptions.AddAdditionalCapability(%3CSPAN%20class%3D%22hljs-string%22%3E%22appTopLevelWindow%22%3C%2FSPAN%3E%2C%20mainWindowHandle)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20session%20%3D%20%3CSPAN%20class%3D%22hljs-keyword%22%3Enew%3C%2FSPAN%3E%20WindowsDriver%3CWINDOWSELEMENT%3E(%3CSPAN%20class%3D%22hljs-keyword%22%3Enew%3C%2FSPAN%3E%20Uri(WindowsApplicationDriverUrl)%2C%20appiumOptions)%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%7D%0A%3C%2FWINDOWSELEMENT%3E%3C%2FWINDOWSELEMENT%3E%3C%2FWINDOWSELEMENT%3E%3C%2FWINDOWSELEMENT%3E%3C%2FWINDOWSELEMENT%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3EOther%20than%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EWindowsDriver%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Estatic%20object%20we%20were%20using%20to%20store%20the%20session%2C%20we%20create%20another%20one%20with%20the%20same%20type%20specific%20for%20the%20desktop%20session.%20The%20initialization%20code%20is%20the%20same%20as%20before%2C%20however%20we%20include%20the%20code%20to%20create%20the%20app%20session%20inside%20a%20try%20%2F%20catch%20statement%2C%20since%20it's%20likely%20going%20to%20fail.%20If%20that's%20the%20case%2C%20we%20need%20to%20fallback%20and%20use%20the%20desktop%20session.%20The%20way%20we%20initialize%20it%20is%20by%20setting%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3Eapp%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Ecapability%20to%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3Eroot%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Einstead%20of%20the%20path%20%2F%20Package%20Family%20Name%20of%20the%20application%20we%20want%20to%20test.%20This%20way%2C%20we'll%20be%20able%20to%20access%20to%20the%20visual%20tree%20of%20all%20the%20UI%20elements%20on%20the%20desktop.%20Then%20we%20look%20for%20the%20window%20of%20our%20application%20by%20using%20the%20familiar%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EFindelementByAccessibilityId()%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Emethod%20passing%20the%20identifier%20of%20the%20window%20as%20parameter.%3C%2FP%3E%0A%3CP%3EOnce%20we%20have%20identified%20the%20window%2C%20we%20get%20its%20handle%2C%20which%20we%20can%20use%20to%20create%20the%20specific%20UI%20session%20for%20our%20application.%20Appium%2C%20in%20fact%2C%20supports%20this%20approach%20by%20adding%20a%20new%20capability%20called%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CSTRONG%3EappTopLevelWindow%3C%2FSTRONG%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Ewith%2C%20as%20parameter%2C%20the%20handle%20we%20have%20just%20retrieved.%20In%20the%20end%2C%20we%20finally%20create%20and%20store%20our%20session%2C%20which%20we're%20going%20to%20use%20to%20perform%20all%20the%20tests.%20That's%20it!%20Using%20MSIX%20as%20a%20packaging%20technology%20changes%20the%20way%20we%20initialize%20the%20test%20suite%2C%20but%20it%20doesn't%20change%20the%20way%20the%20actual%20tests%20are%20executed.%20As%20such%2C%20all%20the%20rest%20of%20the%20code%20we%20have%20seen%20(the%20tests%2C%20the%20cleanup%2C%20etc.%20)%20will%20continue%20to%20work%20without%20changes.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22wrapping-up%22%20id%3D%22toc-hId--1615544915%22%20id%3D%22toc-hId--1615544915%22%3EWrapping%20up%3C%2FH3%3E%0A%3CP%3EIn%20this%20post%20we%20have%20seen%20the%20basic%20concepts%20around%20UI%20testing%20in%20Windows%20apps%20using%20WinAppDriver%20and%20Appium.%20Thanks%20to%20these%20tools%2C%20you're%20able%20to%20write%20tests%20which%20actually%20simulates%20the%20user%20interacting%20with%20the%20user%20interface%20of%20the%20application%2C%20so%20that%20you%20can%20be%20sure%20that%2C%20regardless%20of%20the%20screen%2C%20resolution%20and%20configuration%20of%20the%20user%2C%20he%20will%20always%20be%20able%20to%20interact%20with%20the%20application%20and%20perform%20the%20various%20tasks.%20The%20approach%20is%20quite%20straightforward%2C%20thanks%20to%20the%20Appium%20SDK.%20Once%20you%20have%20created%20a%20UI%20session%2C%20powered%20by%20WinAppDriver%2C%20you%20will%20be%20able%20to%20scan%20the%20visual%20tree%20of%20your%20application%2C%20get%20a%20reference%20to%20the%20UI%20elements%20you%20need%20and%20simulate%20the%20user%20interaction.%20In%20this%20post%2C%20we%20have%20seen%20the%20implementation%20of%20this%20approach%20both%20for%20a%20traditional%20WPF%20application%20and%20for%20its%20MSIX%20packaged%20version.%20In%20the%20next%20post%20we're%20going%20to%20see%20how%20we%20can%20integrate%20everything%20we%20did%20in%20this%20post%20in%20a%20CI%2FCD%20pipeline%2C%20by%20leveraging%20the%20recently%20announced%20WinAppDriver%20integration%20in%20Azure%20DevOps.%3C%2FP%3E%0A%3CP%3EIn%20the%20meantime%2C%20you%20can%20explore%20the%20source%20code%20of%20the%20testing%20project%20we%20have%20created%20in%20this%20post%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2Fqmatteoq%2FWpfUiTesting%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3Eon%20GitHub%3C%2FA%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EStay%20tuned!%3C%2FP%3E%3C%2FLINGO-BODY%3E
Microsoft

In another post on this blog we have seen how telemetry is important to implement a good DevOps story for your software projects.

Another important piece of the puzzle is testing. In order to be agile and deliver fast, we need to make sure that the quality bar is always met. This is where automated testing comes in. When we commit new code to our repository, we need to make sure we haven't broken anything before releasing a new version of our application. When we implement testing, typically we focus only on the business logic side. This is why we often adopt unit testing, which takes our logic and split it into small pieces, which are tested separately and independently. However, client applications aren't made only by business logic, but also the user interface plays an important role. The business logic of our application, in fact, can be bug free, but there may be visual issues that we have missed because they happen only on a specific combination of resolution, screen size, etc. For example, if our UI doesn't fully support devices with high DPIs, an important button or another actionable item might not be visible, preventing our customers to use the application.

 

However, UI testing isn't "easy" to do as unit testing. The full application must be up & running, while unit testing takes care of performing small tasks, which should be as much isolated and independent from each other. Additionally, unit testing tests only code, so to trigger the execution you just need to invoke that code (a method, a class, etc.). In UI testing, instead, you need to actually interact with the application, in order to trigger events.

 

Please welcome Windows Application Driver (from now on, WinAppDriver), which is an open source service which supports performing UI automation against Windows applications, regardless of the technology used to build it (UWP, WPF, Windows Forms, Win32, etc.). Under the hood, WinAppDriver is nothing more than a REST service, which supports performing commands against a Windows application by leveraging HTTP methods and JSON payloads. However, thanks to a testing framework called Appium, we can use a dedicated SDK to simulate commands and actions executed by our application. We can simulate the pressing of a button; or the typing of a text inside a TextBox; etc. This way, we can simulate the most common flow of actions and interactions performed by the user and make sure that everything works as expected.

 

Recently, WinAppDriver has also been added to the Azure DevOps offering. The service, in fact, comes pre-installed on every Windows hosted agent and, with a dedicated task, we can simply start and stop it whenever we need it. This way, we can include UI testing as part of our CI/CD pipeline and make sure that all the UI tests are executed whenever we commit new code to our repository.

 

In this post we're going to learn the basics of adding UI tests to a WPF application, by leveraging WinAppDriver and Appium. In the next post, instead, we're going to learn how to integrate this flow into our CI/CD pipeline.

 

Let's build a WPF app!

As starting point, we're going to build a simple WPF app, which we're going to put under UI testing. The choice between the full .NET Framework or .NET Core 3.0 is up to you: Appium relies on the visual elements of the application, not on the technology leveraged to build it. The purpose of the app is really simple: in a TextBox we're going to write our name; by pressing a button, we're going to display a greeting message in the form of "Hello" followed by the name you've typed. Here is the UI:

<Window x:Class="WpfUiTesting.Wpf.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfUiTesting.Wpf"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    
    <StackPanel x:Name="StackRoot">
        <TextBlock Text="Enter your name:" />
        <TextBox x:Name="txtName" >
        <Button Click="OnSayHello" x:Name="sayHelloButton" Content="Say hello" />
        <TextBlock x:Name="txtResult" />
    </StackPanel>
</Window>

Nothing fancy: a TextBox where to type the name; a button to trigger the action; a TextBlock to display the greeting message.

 

MainApp.png

As you can guess, also the event handler OnSayHello, which is executed when the button is pressed, is simple as well:

private void OnSayHello(object sender, RoutedEventArgs e)
{
    txtResult.Text = $"Hello {txtName.Text}";
}

Let's add some UI testing

Now let's add some testing. We want to make sure that, whenever the user presses the "Say hello" button, the TextBlock named txtResult contains effectively the string "Hello" followed by the value typed by the user in the TextBox named txtName. The first step is to create a testing project. Appium offers a SDK which allows to identify UI elements on the screen and to interact with them, leveraging WinAppDriver. As such, it isn't tight to a specific testing framework, but we can use the one we prefer most. In my case, I'm going to use the inbox Visual Studio solution, which is MSTest. Right click on your solution and a new project which type is Unit Test Project (.NET Framework):

 

unittestproject.png

 

Now we need to install the Appium SDK, which will give us classes and methods to interact with the UI of the application. Right click on the test project, choose Manage NuGet packages, look for and install the package called Appium.WebDriver. Before starting to actually write some tests, we need to do some preparation! First, we need WinAppDriver up & running, since it acts as a middle man between our tests and the WPF application. We can download it from the Releases section of the GitHub project. Be aware, you need to download the service, not WinAppDriver UI Recorder, which is a different tool. At the time of writing, 1.1.1 is the latest release.

 

winappdriverdownloaad.png

After you have downloaded the MSI and executed it, you will find the service installed at the location c:\Program Files (x86)\Windows Application Driver. Go there and double click on the WinAppDriver.exe file to launch it. The service is a command line tool, so you will see a command prompt appearing with the information about the URL and the port which the service is listening to. The default one is http://127.0.0.1:4723/. The command prompt will act also as a logger: every time the Appium SDK will perform any interaction, we will see the underline HTTP requests and JSON payloads being displayed in the window.

Initialize the session

Before starting a test, we need to initialize an Appium session: we need to specify which is the application we want to launch, the target platform, the WinAppDriver URL, etc. We do this inside a method decorated with the [ClassInitialize] attribute, which is the one used by MSTest to define a method that must be invoked every time we start a new testing session. Let's take a look at the code, which we'll comment later:

[TestClass]
public class WpfTest
{
  protected const string WindowsApplicationDriverUrl = "http://127.0.0.1:4723";
  private const string WpfAppId = @"C:\Users\mpagani\Source\Samples\WpfUiTesting\WpfUiTesting.Wpf\bin\Debug\WpfUiTesting.Wpf.exe";

  protected static WindowsDriver<WindowsElement> session;

  [ClassInitialize]
  public static void Setup(TestContext context)
  {
      if (session == null)
      {
          var appiumOptions = new AppiumOptions();
          appiumOptions.AddAdditionalCapability("app", WpfAppId);
          appiumOptions.AddAdditionalCapability("deviceName", "WindowsPC");
          session = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), appiumOptions);

      }
  }
}

We define a couple of constants to hold some information. The first one, called WindowsApplicationDriverUrl, is the URL where the WinAppDriver service is available (do you remember the URL we have seen when we have launched the service in the previous section?). The second one, called WpfAppId, is the identifier of the application which, in case of a Win32 one, is the full path of the executable. In this case, it's the folder which contains the build output of the WPF application we have previously created. Lastly, we define a static property to store our session, which type is WindowsDriver. These are actually classes coming from the Appium SDK: we are using the driver for Windows to connect to one or more application windows (sorry if it sounds like a bad joke =)).

The Setup() method is the one decorated with the [ClassInitialize] attribute, which means that it will be executed before each test session. To bootstrap a session, Appium supports multiple parameters, which are handled with the collection AppiumOptions. The only required one is app, which must contain the identifier of the application we want to test. In case of a Win32 application, it's the full path of the executable. For clarity, we also specify the deviceName one, which tells to the framework which platform our application is targeting (in our case, WindowsPC). Appium, in fact, isn't a testing framework only for Windows desktop apps, but it fully supports also mobile apps and web apps. In the end, we start a new session by creating a new instance of the WindowsDriver object, passing a parameter the WinAppDriver url and the options we have just created.

 

Making our app testable

As already mentioned, the way Appium works is that, through WinAppDriver, is able to walk through the tree of visual object which compose the window of our application and interact with the various controls in an automated way, without any user interaction. Let's take a look at the window of the application we have built to better understand the scenario.

 

TestingApp.png

 

There are 3 controls we need to interact with:

  • The TextBox where to type the name
  • The Button to trigger the interaction
  • The TextBlock where the result will appear

We don't need, for example, to interact with the first TextBlock, which is just a header with an informative text. Or with the window itself. As such, the first problem which arises is finding the right way to identify which objects we are interested to.

The session object we have built in the previous section provides multiple methods to find elements in the window, suitable for different type of applications. For example, the FindElementByTagName() and FindElementByCssSelector() methods are dedicated to web applications. The one that we can leverage for a desktop application is called FindElementByAccessibilityId(), which uses the automation properties supported by Windows to identify a specific element for accessibility purposes. So, before writing the tests, let's wire some of these properties.

<Window x:Class="WpfUiTesting.Wpf.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfUiTesting.Wpf"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    
    <StackPanel x:Name="StackRoot">
        <TextBlock Text="Enter your name:" />
        <TextBox x:Name="txtName" AutomationProperties.AutomationId="txtName"/>
        <Button Click="OnSayHello" AutomationProperties.AutomationId="sayHelloButton" Content="Say hello"  />
        <TextBlock x:Name="txtResult" AutomationProperties.AutomationId="txtResult" />
    </StackPanel>
</Window>

As you can see, we have assigned an identifier to the controls we need to interact with, using the attached property AutomationProperties.AutomationId. Now we can reference with the controls from the Appium SDK using these identifiers. Let's create a first simple test:

[TestMethod]
public void AddNameToTextBox()
{
    var txtName = session.FindElementByAccessibilityId("txtName");
    txtName.SendKeys("Matteo");
    session.FindElementByAccessibilityId("sayHelloButton").Click();
    var txtResult = session.FindElementByAccessibilityId("txtResult");
    Assert.AreEqual(txtResult.Text, $"Hello {txtName.Text}");
}

The purpose of this test is to verify that the basic UI interaction of the application works well. We want to type something in the TextBox, press the button and make sure that the TextBlock will display the message "Hello" followed by what we have typed.

You can assign to the method the name you prefer. The important part is that you mark it with the [TestMethod] attribute, which is the one used by MSTest to identify which are the actual tests to execute. Inside the test, we leverage the session we have previously created and the identifiers we have previously assigned in XAML to get a reference to the controls we want to interact with. We just need to invoke the FindElementByAccesibilityId() method, followed by its identifier. Once we have the reference, the SDK offers many methods to simulate the most common type of interactions. You can see two of them in the code:

  • We simulate the user typing something, by calling the SendKeys() method on the TextBlock. As parameter, we pass the text we want to type, in my case Matteo.
  • We simulate the button click, by calling the Click() method on the Button.

In the end, we use the Assert.AreEqual() method of MSTest to check if the string stored in the Text property of the TextBlock actually matches the string "Hello" followed by the content of the TextBox.

 

Testing the test =)

Before running the test, we need to make sure:

  • WinAppDriver is up & running. If not, make sure to double click on WinAppDriver.exe in the C:\Program Files (x86)\Windows Application Driver folder.
  • We have compiled at least once the WPF application. Building the tests, in fact, doesn't automatically trigger the compilation of the application. Make sure that the executable is stored in the same path you have specified in the WpfAppId constant declared in the test class.

Now right click on the test project and choose Run Tests. If you have done everything properly, you should see some voodoo magic happening =) The WPF application will start, then you will see the text "Matteo" being automatically typed inside the TextBox and the button being clicked. If you now open the Test Explorer tab in Visual Studio (you can find it under the View menu), you should see that the test is passing:

 

TestExplorer.png

 

If you open the command prompt where WinAppDriver is running, you will see the HTTP calls that have been performed under the hood. For example, the following one happens when the test suite tries to identify the element with id txtName:

 

WinAppDriver.png

 

Improving our test class

Everything went well, but if we try to repeat the test plan or to add another test, we will notice some not so great behaviors:

  • When the test session is completed, the WPF application is left open. As such, every test plan will launch a new instance of the WPF application, leading to have lot of instances of the application running at the same time.
  • Let's say we want to run another test which involves typing something else in the TextBox. The test outcome will be compromised by the fact that the TextBox already includes a text, which was the input of the first test.

To make our test suite better and to solve these problems, we can leverage two features of MSTest (which are offered as well by every major testing suite). The first one is the ability to define a test initialization method, which will be invoked before every test (which shouldn't be confused with the one we already have decorated with the [ClassInitialize] attribute, which instead is global and it's invoked once before running all the tests in the suite). We can easily define a method that will clear the content of the TextBox, so that every test will start from a clean situation. Here is how to define it:

[TestInitialize]
public void Clear()
{
    var txtName = session.FindElementByAccessibilityId("txtName");
    txtName.Clear();
}

All we need to do is to mark it with the [TestInitialize] attribute. Inside the method, we just use the familiar FindElementByAccessibilityId() method exposed by the session to find the TextBox and clear all the text, by using the Clear() method.

The second feature is the ability to define a cleanup method, which will be invoked when all the tests have been executed. We're going to use it to close the application:

[ClassCleanup]
public static void Cleanup()
{
    if (session != null)
    {
        session.Close();
        session.Quit();
    }
}

This method is defined as static and it's marked with the [ClassCleanup] attribute. Inside it, we close the session by calling the Close() method and then we quit the app, by invoking the Quit() method.

To make sure everything is working, let's add another test. This time, we're going to add one that will lead to a failure:

[TestMethod]
public void AddWrongNameToTextBox()
{
    var txtName = session.FindElementByAccessibilityId("txtName");
    txtName.SendKeys("Matteo");
    session.FindElementByAccessibilityId("sayHelloButton").Click();
    var txtResult = session.FindElementByAccessibilityId("txtResult");
    Assert.AreEqual(txtResult.Text, $"Hello Matt");
}

This test is similar to the previous one. The only difference is that it will check the result against the fixed string Hello Matt which, of course, will cause a failure because the test actually types the name Matteo in the TextBox. Now run again the tests and you will notice two things:

  • Between the execution of the first and the second test, the TextBox will be cleared.
  • At the end of the test, the application will be closed.

As a bonus, we can use the failed test to see what happens in such a scenario. The Test Explorer tool will clearly display the exception that has been raised:

 

FailedTest.png

As you can see, the message is pretty clear: the TextBlock contains the text Hello Matteo, while the string we have used for comparison is Hello Matt. What if was another kind of exception, not related to a failed assert? Tests are, in the end, code, so they can include bugs as well! In this case, you can right click on the test project and choose Debug Tests. A real debugger will be attached, allowing you to set breakpoints or to analyze the code in case of exceptions, like you would do with the real application.

 

Testing a packaged application

Which is the best way to deploy and distribute our WPF application? MSIX, of course! Let's package our application then! I won't go into the details, since we have talked about MSIX packaging multiple times on this blog. If you need some guidance, you can refer to the official documentation. Right click on your solution, choose Add → New Project and choose the Windows Application Packaging Project template. Once it has been created, right click on Applications, choose Add reference and select the WPF project. That's it!

If we stop for a minute to think, however, we will realize that the current test suite isn't suitable for a packaged application. We have set, as entry point, the full executable deployed by Visual Studio, but this isn't how a MSIX package works. You don't manually launch an executable, Windows does that for you based on the entry point defined in the manifest. And it could not even be a standard Win32 executable, since WinAppDriver supports testing also UWP application. We need to change the way we define our entry point. Luckily, it's quite easy:

[TestClass]
public class WpfTest
{
    protected const string WindowsApplicationDriverUrl = "http://127.0.0.1:4723";
    private const string WpfAppId = @"WpfUiTesting_e627vcndsd2rc!App";

    protected static WindowsDriver<WindowsElement> session;

    [ClassInitialize]
    public static void Setup(TestContext context)
    {
        if (session == null)
        {
            var appiumOptions = new AppiumOptions();
            appiumOptions.AddAdditionalCapability("app", WpfAppId);
            appiumOptions.AddAdditionalCapability("deviceName", "WindowsPC");
            session = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), appiumOptions);
        }
    }
}

The initialization code is the same, except for the identifier of the app, which isn't anymore the full path of the executable, but it's the Package Family Name followed by the !App suffix. We can find the Package Family Name by double clicking on the Package.appxmanifest file in the Windows Application Packaging Project and moving to the Packaging tab:

 

PFN.png

 

It's a unique value, generated based on the package name and a hash of your publisher name. That's it. Now deploy the packaged application at least once (right click on the Windows Application Packaging Project and choose Deploy), then run the tests again. You will see the packaged version of the app starting automatically, ready to receive inputs from WinAppDriver. Great job, everything is goo.. well , not really.

 

MSIXerror.png

 

All the tests are now failing with the following error:

Class Initialization method WpfUiTesting.Tests.WpfTest.Setup threw exception. OpenQA.Selenium.WebDriverException: OpenQA.Selenium.WebDriverException: Failed to locate opened application window with appId: WpfUiTesting_e627vcndsd2rc!App, and processId: 26244

The MSIX packaging, unfortunately, messed up a bit our testing and, despite the appId and the processId are correct, WinAppDriver isn't able to locate anymore the main window of our application. Are we doomed? Of course not! The Appium SDK offers an option to create a desktop session instead of an app session which, instead of giving us access to single UI elements of an application, it gives us access to all the UI elements inside Windows. This option is useful when we need to test, for example, special activation scenarios, like Cortana or a protocol, in which the starting point isn't the application traditionally opened because the user has double clicked on an icon. This option allows us also to implement a workaround for our blocker. Thanks to a desktop session, we can manually look for the window of our application and get a reference to it. Using this approach will make the test suite initialization a little bit more verbose, but it will make things working again. It sounds like a reasonable trade-off =)

As first step, we need to give a unique identifier to our window. We can use, again, the AutomationProperties.AutomationId:

<Window x:Class="WpfUiTesting.Wpf.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfUiTesting.Wpf"
        mc:Ignorable="d"
        AutomationProperties.AutomationId="WpfUITestingMainWindow"
        Title="MainWindow" Height="450" Width="800"
        >
    
    <StackPanel Margin="12">
        <TextBlock Text="Enter your name:" />
        <TextBox x:Name="txtName" AutomationProperties.AutomationId="txtName"/>
        <Button Click="OnSayHello" AutomationProperties.AutomationId="sayHelloButton" Content="Say hello" />
        <TextBlock x:Name="txtResult" AutomationProperties.AutomationId="txtResult" />
    </StackPanel>
</Window>

We have assigned to the Window the identifier WpfUITestingMainWindow, which we're going to use later. Let's take a look at the updated test initialization code:

[TestClass]
public class WpfTest
{
   protected const string WindowsApplicationDriverUrl = "http://127.0.0.1:4723";
   private const string WpfAppId = @"WpfUiTesting_e627vcndsd2rc!App";

   protected static WindowsDriver<WindowsElement> session;
   protected static WindowsDriver<WindowsElement> DesktopSession;

    public static void Setup(TestContext context)
    {
        if (session == null)
        {
            var appiumOptions = new AppiumOptions();
            appiumOptions.AddAdditionalCapability("app", WpfAppId);
            appiumOptions.AddAdditionalCapability("deviceName", "WindowsPC");
            DesktopSession = null;
            try
            {
                Console.WriteLine("Trying to Launch App");
                DesktopSession = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), appiumOptions);
            }
            catch
            {
                Console.WriteLine("Failed to attach to app session (expected).");
            }

            appiumOptions.AddAdditionalCapability("app", "Root");
            DesktopSession = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), appiumOptions);
            var mainWindow = DesktopSession.FindElementByAccessibilityId("WpfUITestingMainWindow");
            var mainWindowHandle = mainWindow.GetAttribute("NativeWindowHandle");
            mainWindowHandle = (int.Parse(mainWindowHandle)).ToString("x"); // Convert to Hex
            appiumOptions = new AppiumOptions();
            appiumOptions.AddAdditionalCapability("appTopLevelWindow", mainWindowHandle);
            session = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), appiumOptions);
        }
    }
}

Other than the WindowsDriver static object we were using to store the session, we create another one with the same type specific for the desktop session. The initialization code is the same as before, however we include the code to create the app session inside a try / catch statement, since it's likely going to fail. If that's the case, we need to fallback and use the desktop session. The way we initialize it is by setting the app capability to root instead of the path / Package Family Name of the application we want to test. This way, we'll be able to access to the visual tree of all the UI elements on the desktop. Then we look for the window of our application by using the familiar FindelementByAccessibilityId() method passing the identifier of the window as parameter.

Once we have identified the window, we get its handle, which we can use to create the specific UI session for our application. Appium, in fact, supports this approach by adding a new capability called appTopLevelWindow with, as parameter, the handle we have just retrieved. In the end, we finally create and store our session, which we're going to use to perform all the tests. That's it! Using MSIX as a packaging technology changes the way we initialize the test suite, but it doesn't change the way the actual tests are executed. As such, all the rest of the code we have seen (the tests, the cleanup, etc. ) will continue to work without changes.

 

Wrapping up

In this post we have seen the basic concepts around UI testing in Windows apps using WinAppDriver and Appium. Thanks to these tools, you're able to write tests which actually simulates the user interacting with the user interface of the application, so that you can be sure that, regardless of the screen, resolution and configuration of the user, he will always be able to interact with the application and perform the various tasks. The approach is quite straightforward, thanks to the Appium SDK. Once you have created a UI session, powered by WinAppDriver, you will be able to scan the visual tree of your application, get a reference to the UI elements you need and simulate the user interaction. In this post, we have seen the implementation of this approach both for a traditional WPF application and for its MSIX packaged version. In the next post we're going to see how we can integrate everything we did in this post in a CI/CD pipeline, by leveraging the recently announced WinAppDriver integration in Azure DevOps.

In the meantime, you can explore the source code of the testing project we have created in this post on GitHub.

 

Stay tuned!