Testing if a Label is Present

CalebCohoonCalebCohoon USMember
edited April 2016 in Xamarin.Forms

Hello! The UI testing has been giving me some problems and I'm not sure why it's being so difficult. Right now I'm trying to just check if a label is present on the page but after trying a few different ways still nothing. Here's the code I'm working with:

<?xml version="1.0" encoding="UTF-8"?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="LogATourMobile.SignInPage"> <ContentPage.Content> <StackLayout Padding="20"> <Label Text="My App" FontSize="30" /> <Entry Placeholder="Email" Text="" Keyboard="Email" x:Name="emailField" TextChanged="OnFieldTextChanged" /> <Entry Placeholder="Password" Text="" IsPassword="true" x:Name="passwordField" TextChanged="OnFieldTextChanged" /> <Label Text="Sorry but your email or password could not be found" IsVisible="false" TextColor="Red" x:Name="errorMessage" /> <Button Text="Sign In" IsEnabled="false" x:Name="signInButton" Clicked="OnSignIn" /> <ActivityIndicator Color="White" IsRunning="false" IsVisible="false" x:Name="spinner" /> </StackLayout> </ContentPage.Content> </ContentPage>

public void When_Sign_In_Unsuccessful_Show_An_Error()
    app.EnterText("Email", "[email protected]");
    app.EnterText("Password", "wrong");
    app.Tap("Sign In");

    var result = app.WaitForElement(e => e.Raw("label {text LIKE 'Sorry but'}"));
    Assert.AreEqual(result.Length, 1);

Every time I run this test it always says it found nothing but the label is definitely there on the page. Is there any other way to achieve this? I've tried using the Marked/Class query lookup methods but those aren't working either. I even set the StyleId and ClassId and still nothing. Am I just doing everything totally wrong? :)

Thanks for any help!


Here's a screen shot of the app after the sign in fails.


  • JohnHardmanJohnHardman GBUniversity mod

    @CalebCohoon - I've never used e.Raw in that way. What I do is as follows:


    Or, where I want to get multiple matches, or confirm that one and only one match exists, I do:

                AppResult[] matches = app.WaitForElement(
                    String.Format("Control with style id = \"{0}\" not found", styleId),
  • CalebCohoonCalebCohoon USMember

    @JohnHardman thanks much for the help!

    What ended up working fine is this:

    app.WaitForElement("Sorry but your email or password could not be found");

    I was kind of hoping I didn't have to specify the entire string but using the StyleId name didn't work. I can just make a static class that contains these static strings so I can utilize them in the app and test environment without duplication.

    Thanks again :)

  • gishagisha Member
    edited August 2018

    I am also facing similar issue. But "Wait for Element" didn't work for me. For me the element is populated when i do app.Query(c=>c.All()). But when i do Query/Wait for element with automation ID or Marked Label , it is not found . Please refer the screenshot . I want to identify the element " Enter a valid email to continue" which appears once i click next after entering a wrong format. I have given Automation ID for the element.

    I would appreciate any quick response. Thank you.

Sign In or Register to comment.