{"id":4080,"date":"2016-10-16T09:28:32","date_gmt":"2016-10-16T09:28:32","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=4080"},"modified":"2016-10-16T09:28:32","modified_gmt":"2016-10-16T09:28:32","slug":"ui-automation-testing-with-teststack-white","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/ui-automation-testing-with-teststack-white\/","title":{"rendered":"UI Automation Testing with TestStack.White"},"content":{"rendered":"<p>TestStack.White is based on the UI Automation libraries (see <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/ee684009.aspx\" target=\"_blank\">UI Automation<\/a>), offering a simplification of such methods for automating a UI and allowing us to write unit tests against such UI automation.<\/p>\n<p><strong>Getting Started<\/strong><\/p>\n<p>Let&#8217;s jump straight in and write a simply UI automation unit test around the Calc.exe application.<\/p>\n<ul>\n<li>Create a new C# Unit Test project (or class library, adding your favoured unit testing framework)<\/li>\n<li>Install the TestStack.White nuget package<\/li>\n<\/ul>\n<p>Let&#8217;s begin by creating a simple test method which starts the Calc.exe application, get&#8217;s access to the calculator window and then disposes of it, we&#8217;ll obviously insert code into this test to do something of value soon, but for now, here&#8217;s the basics<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n&#x5B;TestMethod]\r\npublic void TestMethod1()\r\n{\r\n   using(var application = Application.Launch(&quot;Calc.exe&quot;))\r\n   {\r\n      var calculator = application.GetWindow(&quot;Calculator&quot;, InitializeOption.NoCache);\r\n\r\n      \/\/ do something with the application\r\n\r\n      application.Close();\r\n   }\r\n}\r\n<\/pre>\n<p>Well that doesn&#8217;t do anything too exciting, it runs Calc.exe and then closes it, but now we can start interacting with an instance of the calculator&#8217;s UI using TestStack.White.<\/p>\n<p>Let&#8217;s start by getting the button with the number 7 and click\/press it. <\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar b7 = calculator.Get&lt;Button&gt;(SearchCriteria.ByText(&quot;7&quot;));\r\nb7.Click();\r\n<\/pre>\n<p>By using the Get method with the generic parameter Button, we get back a button object which we can interact directly with. The SearchCriteria allows us to try to find UI control in the Calculator with the text (in this case) 7. As is probably quite obvious, we call the Click method on this button object to simulate a button click event.<\/p>\n<p>We can&#8217;t always get as controls by their text so using Spy++ and using the cross-hair\/find window tool we can find the &#8220;Control ID&#8221; (which is in hex.) and we can instead find a control via this id (White calls this the automation id) hence<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar plus = calculator.Get&lt;Button&gt;(\r\n       SearchCriteria.ByAutomationId(\r\n           0x5D.ToString()));\r\nplus.Click();\r\n<\/pre>\n<p>So let&#8217;s look at a completed and very simply unit test to see that we can add two numbers and the output (on the screen) is expected<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar b7 = calculator.Get&lt;Button&gt;(\r\n   SearchCriteria.ByText(&quot;7&quot;));\r\nb7.Click();\r\n\r\nvar plus = calculator.Get&lt;Button&gt;(\r\n   SearchCriteria.ByAutomationId(\r\n      0x5D.ToString()));\r\nplus.Click();\r\n\r\nvar b3 = calculator.Get&lt;Button&gt;(\r\n   SearchCriteria.ByText(&quot;5&quot;));\r\nb3.Click();\r\n\r\nvar eq = calculator.Get&lt;Button&gt;(\r\n   SearchCriteria.ByAutomationId(\r\n      0x79.ToString()));\r\neq.Click();\r\n\r\nvar a = calculator.Get(\r\n   SearchCriteria.ByAutomationId(\r\n      0x96.ToString()));\r\n\r\nvar r = a.Name;\r\nAssert.AreEqual(&quot;12&quot;, r);\r\n<\/pre>\n<p><strong>Managed applications<\/strong><\/p>\n<p>In the above example we uses Spy++ to get control id&#8217;s etc. for WPF we can use the utility, Snoop and for the automation id use the name of the control, for example<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\nvar searchBox = pf.Get&lt;TextBox&gt;(\r\n   SearchCriteria.ByAutomationId(&quot;SearchBox&quot;));\r\n<\/pre>\n<p>where SearchBox is the name associated with the control.<\/p>\n<p><strong>References<\/strong><\/p>\n<p>http:\/\/teststackwhite.readthedocs.io\/en\/latest\/<br \/>\nhttps:\/\/github.com\/TestStack\/White<\/p>\n","protected":false},"excerpt":{"rendered":"<p>TestStack.White is based on the UI Automation libraries (see UI Automation), offering a simplification of such methods for automating a UI and allowing us to write unit tests against such UI automation. Getting Started Let&#8217;s jump straight in and write a simply UI automation unit test around the Calc.exe application. Create a new C# Unit [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[135,134],"tags":[],"class_list":["post-4080","post","type-post","status-publish","format-standard","hentry","category-teststack-white","category-ui-testing"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4080","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=4080"}],"version-history":[{"count":9,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4080\/revisions"}],"predecessor-version":[{"id":4318,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/4080\/revisions\/4318"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=4080"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=4080"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=4080"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}