The most time-consuming way to develop mobile apps is to manually test every feature point of the app or fix bugs. It is suggested that the business logic of the app can be assisted with NUnit or Xunit test units. How will the user interface be tested? As we all know, mobile devices are diverse, number and size, the type of equipment is also various, how to ensure that the app user interface in hundreds of iOS and Android devices can display properly, no errors? This is the most annoying thing for many app developers.
Today, this article focuses on using the uitest provided by Xamarin to test the app interface.
accessing controls
When you create an application using Xamarin.Forms, the user interface is created using Xamarin.Forms controls and XAML. At runtime, Xamarin.Forms generates the corresponding native application based on the user interface that we created. Because we are developing a xamarin.forms control, you can access it by Styleid special properties in Xamarin.uitests. Styleid can be set to any string, creating any test function that can be accessed directly, and Styleid is consistent in both iOS and Android apps, which means you don't need to manage these IDs specifically.
Xaml
<button x:name= "Buttonlogin" styleid= " Buttonlogin " grid.row=" 5 " backgroundcolor=" #F44336 " Borderradius= "0" textcolor= "white" Text= "Login to Meetup" command= "{Binding Logincommand}" />
C#
New Button { "Buttonlogin", "Login to Meetup", borderradius = 0, textcolor = Color.White, backgroundcolor = Color.fromhex ("F443336") };
The last thing you need to do is add code to your iOS and Android apps and use Styleid in native controls. This makes it possible to call Xamarin.uitest. This code needs to be commented out in the final version of the app.
IOS settings
Open the iOS AppDelegate.cs file and add the following code to the Finishedlaunching method:
1: forms.viewinitialized + = (sender, e) = =
2: {
3: if (null ! = E.view.styleid)
4: {
5: e.nativeview.accessibilityidentifier = E.view.styleid;
6: }
7: };
Android settings
Open the MainActivity.cs file and add the following code to the OnCreate method:
1: Xamarin.Forms.Forms.ViewInitialized + = (sender, e) = =
2: {
3: if (! String. Isnullorwhitespace (E.view.styleid))
4: {
5: e.nativeview.contentdescription = E.view.styleid;
6: }
7: };
Now that you can run UITest and open Repl, Buttonlogin will appear:
The uitest,buttonlogin is often used to verify that the interactive function of the button is the logon screen that appears:
1: [Test]
2: publicvoid openloginscreen ()
3: {
4: app. Screenshot ("APP launches");
5: app. Tap ("Buttonlogin");
6: app. Screenshot ("When I tap Login");
7: assert.isnotnull (app. Query ("Entryusername"). single ());
8: app. Screenshot ("Login page is Visible");
9: }
Have they learned it? This article allows us to implement the perfect automated testing of the user interface of the app! This saves time and effort, and efficiently improves test efficiency!
Automated testing for mobile apps