I have an application with a short setup first time you start the application. In this setup you are able to enable or disable Insights with a switch.
This choise can later be changed in a Settings menu, where the switch will have the value originaly set.
However i'm unable to read the value in my UI tests. When I query or use tree to inspect the element, it's identical if it's on or off.
Is there any way to check the value of a switch?
The Switch
control extends the CompoundButton
and it has isChecked
method that we can invoke to get the Switch
control state, Here is the example code:
//Assuming initial value is off app.Tap (c => c.Class ("Switch")); var switchValue = app.Query (c => c.Class ("Switch").Invoke ("isChecked").Value<bool> ()).First (); Assert.IsTrue (switchValue, "Switch is not on");
Answers
The
Switch
control extends theCompoundButton
and it hasisChecked
method that we can invoke to get theSwitch
control state, Here is the example code:This was very helpful. I would just like to add that you can also use
c => c.Marked("YourStyleID")
instead of
c => c.Class("Switch")
if that is more useful for you.
@prashantvc - Many thanks for that. One follow-on question - if you had multiple Switch views on a page, and wanted to get them all to a known state (let's say we want them all set to off), how would you do that?
I can identify them individually and change their state, but I feel like there must be a simpler way, using linq.
Many thanks,
John H.
It seems that using isChecked only works on Android. For iOS, use "isOn" which returns an int (1 or 0) rather than a bool.
Regards,
John H.
I use library named "FormsTest" (https://github.com/pranart/FormsTest)
This library will let you get almost every property value from Xamarin.Forms Controls.