Search code examples
androidandroid-edittextautomated-testsappium

Appium getText() returns android:hint value from EditText?


I just started investigating the use of Appium for test automation of a native Android app. In this app, the XML layout for the UI contains the following declaration for an EditText control:

<EditText
    android:id="@+id/inputText"
    android:layout_width="fill_parent"
    android:layout_height="0px"
    android:layout_weight="0.5"
    android:inputType="text|textMultiLine"
    android:hint="Type or paste text here"
    android:text="" />

When the app runs, the text value of the EditText is obviously empty so it displays the hint value ("Type or paste text here"). In my test case, if I call getText() on this EditText, the return value I get back is not an empty string as I'd expect. Instead, I get back the hint value of the EditText ("Type or paste text here"). This is the first time I've used Appium so it's possible that this is the expected behavior. But if that's the case, how do I assert that the text value of the EditText is, in fact, empty if getText() returns the value of the hint? I suppose I could assert that the value returned by getText() is equal to "Type or paste text here" but then if the user actually entered that exact text, how would I know the difference?


Solution

  • getText() returns whatever value is assigned to an element (in your case android:hint) to be displayed at the time it is rendered by the UI thread.

    To Assert in your case, hint is a text which is not dynamically configured, so you can verify the text as

    if(element.getText().equals("Type or paste text here"))
      // hint being displayed
    else if(element.getText() == "")
     // the text is left empty
    

    Now if the user can configure the same name as the hint of the element. You would probably have to provide a better design to the application so as to avoid this. Appium could just be a medium to provide you back the information fed into the application UI.