Xamarin UITest (Calabash) Query for Non-Empty text Property

1.2k views Asked by At

In Xamarin UITest (which uses Calabash) I can query an element that has some text, like so:

app.WaitForElement(x => x.Marked("MyTextControl")
                   .Property("text")
                   .Contains("some text"));

I see in the reference docs that there is also BEGINSWITH, LIKE, and so on, but I don't see any sort of IsEmpty or IsNotEmpty. Is there a way to find an element with text that is not empty? I was expecting to see a Length method, so I could do something like:

app.WaitForElement(x => x.Marked("MyTextControl")
                   .Property("text")
                   .Length() > 0);

Is there another way to accomplish what I am after?

3

There are 3 answers

0
jWhite On BEST ANSWER

To the specific question, I do not believe it is possible to accomplish this within a specific Query in the UITest API's. The best workaround would be to what @Zil suggested and to query for all elements of a specific Identifier and then loop over them to find what you are looking for, or something similar to that idea.

3
Gil Sand On

I'm doing it like so, for UILabels. I don't think it works for UIButtons, for buttons you have to dig a bit deeper in the AppResult[], but it should be doable alone, I've just never had the chance to have text in my buttons when I did UITests.

AppResult[] labelContainer = app.WaitForElement(x => x.Marked("MyTextControl");
AppResult label = labelContainer[0]; 

You always have at least one element in the array, otherwise the WaitForElement fails. So no risk of out-of-bounds here.

Now we're making sure there is text, that should be answering your question specifically.

Assert.IsFalse(string.IsNullOrEmpty(label.Text));

If you wanna be thourough you can apply the next line in a for loop to test each element of the given array of items.

for (int i = 0; i < labelContainer.Length ; i++)
{
    AppResult label = labelContainer[i];
    Assert.IsFalse(string.IsNullOrEmpty(label.Text)); 
}

Of course you could make an extension method that does all this for a given Mark, or one-line it like so :

  Assert.IsFalse(string.IsNullOrEmpty(app.WaitForElement(x => x.Marked("MyTextControl")[0].Text)); 

And with all this you should be a happy man for the next few minutes :)

0
RockLaam On

Or you can just simply do like that:

app.WaitForElement(x => x.Marked("MyTextControl")
                         .Property("text")
                         .Like(*));

If you want to get not null text elements!

I tried but I'm not sure it's correct for all cases or not! If you find some problem with my answer, please fix me! Thanks a lot! :D