{"id":20553,"date":"2015-08-27T13:13:15","date_gmt":"2015-08-27T17:13:15","guid":{"rendered":"https:\/\/blog.xamarin.com\/?p=20553"},"modified":"2015-08-27T13:13:15","modified_gmt":"2015-08-27T17:13:15","slug":"easily-automate-your-xamarin-forms-apps","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/easily-automate-your-xamarin-forms-apps\/","title":{"rendered":"Easily Automate Your Xamarin.Forms Apps"},"content":{"rendered":"<p>\t\t\t\tWhen developing mobile applications, it&#8217;s extremely time consuming and tedious to manually test your app for every new feature added or bug fixed. Of course, it&#8217;s possible to test an app&#8217;s business logic with common unit testing practices using nUnit or xUnit, but what about the user interface? <img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/UITest.png\" alt=\"UITest\" width=\"125\" class=\"alignright size-full wp-image-20666\" \/>How can we ensure that an application&#8217;s user interface is not only behaving properly, but also looks right on the thousands of unique iOS and Android devices out in the world? <\/p>\n<p>This is where automated user interface testing with Xamarin.UITest, combined with the power and scale of Xamarin Test Cloud, comes in. In the past, we&#8217;ve looked at creating tests for traditional Xamarin iOS and Android applications by setting the Accessibility and Resource IDs to gain access to user interface controls in Xamarin.UITest, and today we&#8217;ll look at how to use UITest with Xamarin.Forms apps.<br><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/CL2GXsuUkAA257W.png\" alt=\"Meetup Manager in Test Cloud\" width=\"1023\" height=\"578\" class=\"aligncenter size-full wp-image-20554\" \/><\/p>\n<h2>Accessing Controls<\/h2>\n<p>When creating applications with Xamarin.Forms, we create our user interface with Xamarin.Forms controls in C# or XAML. At runtime, Xamarin.Forms handles laying down and displaying the native controls for the application. Since we&#8217;re developing against Xamarin.Forms controls, there&#8217;s a special way to gain access to them in our Xamarin.UITests through a special property called <code>StyleId<\/code>. The StyleId can be set to any string that can be accessed when creating your tests and will be the same between iOS and Android, which means there is no special management of Ids when developing with Xamarin.Forms.<\/p>\n<p><b>XAML<\/b><\/p>\n<pre class=\"lang:xml decode:true\">\n&lt;Button x:Name=&quot;ButtonLogin&quot; \n        StyleId=&quot;ButtonLogin&quot;\n        Grid.Row=&quot;5&quot; \n        BackgroundColor=&quot;#F44336&quot; \n        BorderRadius=&quot;0&quot; \n        TextColor=&quot;White&quot; \n        Text=&quot;Login to Meetup&quot;\n        Command=&quot;{Binding LoginCommand}&quot; \/&gt;\n<\/pre>\n<p><b>C#<\/b><\/p>\n<pre class=\"lang:csharp decode:true\">\nvar buttonLogin = new Button\n  {\n    StyleId = &quot;ButtonLogin&quot;,\n    Text = &quot;Login to Meetup&quot;,\n    BorderRadius = 0,\n    TextColor = Color.White,\n    BackgroundColor = Color.FromHex(&quot;F443336&quot;)\n  };\n<\/pre>\n<p>The last thing to do is to add a few lines of code in your iOS and Android applications to apply the StyleId to the native controls, which actually does expose them to Xamarin.UITest. These lines of code should be commented out for final app store submissions.<\/p>\n<h3>iOS Setup<\/h3>\n<p>Open the AppDelegate.cs file in your iOS project. Add the following lines in FinishedLaunching method after Xamarin.Forms&#8217; Init is called:<\/p>\n<pre class=\"lang:csharp decode:true\">\nXamarin.Forms.Forms.ViewInitialized += (sender, e) =&gt;\n{\n  if (null != e.View.StyleId) \n  {\n    e.NativeView.AccessibilityIdentifier = e.View.StyleId;\n  }\n};\n<\/pre>\n<h3>Android Setup<\/h3>\n<p>Open the MainActivity.cs file and add the following lines in OnCreate method after Xamarin.Forms&#8217; Init is called:<\/p>\n<pre class=\"lang:csharp decode:true\">\nXamarin.Forms.Forms.ViewInitialized += (sender,  e) =&gt; \n{\n  if (!string.IsNullOrWhiteSpace(e.View.StyleId)) \n  {\n    e.NativeView.ContentDescription = e.View.StyleId;\n  }\n};\n<\/pre>\n<p>Now, if I was to run a UITest and open the REPL, <b>ButtonLogin<\/b> will be visible in the tree:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/44\/2019\/03\/LoginButtonVisible-1024x533.png\" alt=\"LoginButtonVisible\" width=\"1024\" height=\"533\" class=\"aligncenter size-large wp-image-20555\" \/><\/p>\n<p>In the UITest, the <b>ButtonLogin<\/b> can be now used to interact with the button and validate so the login screen appears:<\/p>\n<pre class=\"lang:csharp decode:true\">\n[Test]\npublic void OpenLoginScreen()\n{\n  app.Screenshot(&quot;App Launches&quot;);\n\n  app.Tap(&quot;ButtonLogin&quot;);\n  app.Screenshot(&quot;When I tap Login&quot;);\n\n  Assert.IsNotNull(app.Query(&quot;EntryUserName&quot;).Single());\n  app.Screenshot(&quot;Login Page is Visible&quot;);\n}\n<\/pre>\n<h3>Learn More<\/h3>\n<p>To learn more about Xamarin.UITest and Xamarin Test Cloud, be sure to dive through the <a href=\"http:\/\/developer.xamarin.com\/testcloud\" title=\"Xamarin Test Cloud Documentations\">developer documentation<\/a> and see our <a href=\"http:\/\/developer.xamarin.com\/guides\/cross-platform\/xamarin-forms\/uitest-and-test-cloud\/\" title=\"Xamarin.Forms and UITest\">advanced guide for using Xamarin.UITest with Xamarin.Forms<\/a>. You can also download the full source code and Xamarin.UITest for Meetup Manager, which I demoed here, on <a href=\"http:\/\/www.github.com\/jamesmontemagno\/MeetupManager\" title=\"Meetup Manager on GitHub\">my GitHub<\/a>. \t\t<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When developing mobile applications, it&#8217;s extremely time consuming and tedious to manually test your app for every new feature added or bug fixed. Of course, it&#8217;s possible to test an app&#8217;s business logic with common unit testing practices using nUnit or xUnit, but what about the user interface? How can we ensure that an application&#8217;s [&hellip;]<\/p>\n","protected":false},"author":544,"featured_media":20666,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[4,15,16],"class_list":["post-20553","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers","tag-xamarin-platform","tag-xamarin-test-cloud","tag-xamarin-forms"],"acf":[],"blog_post_summary":"<p>When developing mobile applications, it&#8217;s extremely time consuming and tedious to manually test your app for every new feature added or bug fixed. Of course, it&#8217;s possible to test an app&#8217;s business logic with common unit testing practices using nUnit or xUnit, but what about the user interface? How can we ensure that an application&#8217;s [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/20553","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/users\/544"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=20553"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/20553\/revisions"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=20553"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=20553"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=20553"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}