I have a list of 500 elements and when I use app.Query
on the page, Xamarin.UITest gives me only 6 elements as only 6 elements are visible in UI.
How can I retrieve all 500 elements from the list inside of my UITest?
I have a list of 500 elements and when I use app.Query
on the page, Xamarin.UITest gives me only 6 elements as only 6 elements are visible in UI.
How can I retrieve all 500 elements from the list inside of my UITest?
As described above, the expected behavior of app.Query
will only return the results of all visible controls on the page. Thus, if a control is not visible, app.Query
will not return it.
The way to retrieve all of the data in a list is to use a Backdoor Method.
Xamarin has additional documentation on how to use backdoors in UITest.
This sample app implements the snippets from the tutorial: https://github.com/brminnick/UITestSampleApp
Because Backdoor Methods are limited to return a string, we will need to be able to serialize our object.
You will need to add the Newtonsoft.Json NuGet package to each of your projects; i.e. add the Newtonsoft.Json NuGet to the .NET Standard project, the iOS Project, the Android Project and the UITest Project.
These methods will be used to serialize and deserialize the object.
using Newtonsoft.Json;
public static class ConverterHelpers
{
public static string SerializeObject(object value)
{
return JsonConvert.SerializeObject(value);
}
public static T DeserializeObject<T>(string value)
{
return JsonConvert.DeserializeObject<T>(value);
}
}
This method in the AppDelegate
will expose a backdoor from your iOS app that the UITest can utilize.
If you do not have an iOS app, skip this step.
[Export("getDataAsString:")]
public NSString GetDataAsString(NSString noValue)
{
var data = [Add code here to retrieve the data from your app]
var dataAsString = ConverterHelpers.SerializeObject(data);
return new NSString(dataAsString);
}
This method in the MainActivity
(or the Application
class, if you have one) will expose a backdoor from your Android app that the UITest can utilize.
If you do not have an Android app, skip this step.
[Export("GetDataAsString")]
public string GetDataAsString()
{
var data = [Add code here to retrieve the data from your app]
var dataAsBase64String = ConverterHelpers.SerializeObject(data);
return dataAsBase64String;
}
Create a static method in the UITest project to invoke backdoor methods from UITest.
internal static List<DataModel> GetListData(IApp app)
{
string dataAsString;
if (app is iOSApp)
dataAsString = app.Invoke("getDataAsString:", "").ToString();
else
dataAsString = app.Invoke("GetDataAsString").ToString();
return ConverterHelpers.DeserializeObject<List<DataModel>>(dataAsString);
}
In the UITest test method, implement the static method to retrieve the data.
[Test]
public void VerifyData()
{
Assert.IsTrue(GetListData(app).Count == 500);
}