Brandon Minnick
Brandon Minnick

Reputation: 15340

Create Xamarin.UITest for Android Checkbox

I have an app that uses the Android CheckBox control.

How do I create a UITest that detects whether the CheckBox is checked?

Upvotes: 0

Views: 591

Answers (1)

Brandon Minnick
Brandon Minnick

Reputation: 15340

Sample Code

bool IsCheckBoxChecked(string textBoxContentDescription)
{
    return (bool)app.Query(x => x.Marked(textBoxContentDescription).Invoke("isChecked"))?.FirstOrDefault();
}

Sample App

Here is a sample app that I've put together to demostrate how to do this: https://github.com/brminnick/AndroidCheckBoxSampleApp

Explaination

To interact with an Android CheckBox from a UITest, you must use the Invoke method to access the methods in the native Java Android API. In our Invoke statements, we can take advantage of performClick() to toggle the CheckBox, setChecked(boolean checked) to set the value of the CheckBox, and isChecked() to return a bool that is true when the CheckBox is checked and false when it is unchecked.

In the UITest project, I created a ToggleCheckBox method to toggle an individual CheckBox, a IsCheckBoxChecked method to return the CheckBox current status, and a SetCheckBox method to set the CheckBox status.

All tests were validated via Xamarin Test Cloud. The test report is viewable here.

enter image description here

Upvotes: 1

Related Questions