Unable to clear complete text from the User name field

There is a scenario where i have to edit user profile by changing it User name. So the already entered text of 35 letters is need to be removed first from that field for which i am swiping the mouse across the field, which is not at all useful during script playback.
driver.swipe(340, 1336, 340, -5573, 2083);

Is there any command to clear all the entered text in that field?

I used the below mention command twice in order to clear the text in the User name field, is there any other way?
driver.findElement(By.xpath("//[@id=‘last_name_textEdit’]")).clear();
driver.findElement(By.xpath("//
[@id=‘last_name_textEdit’]")).clear();

This looks like a good approach :slight_smile:

why did you need to it twice ?? It would seem that a single clear should suffice ?

Its not clearing the complete text field using the commend once so i used it twice.

Hmmm…this sounds like a bug. Tom, what do you think ? Is there an explanation as to why 2 clears are needed to clear out a single textfield ?

I tried the .clear() and it doesn’t seem to clear. I tried back-to-back clear. It doesn’t seem as if the clear works for native apps.

Hi

I retried it, it seems to be working for me on iOS non-instrumented and instrumented

I need some more info on your scenarios

please add a description of your:

  1. Mobile OS and version
  2. Application type (Hybrid \ native)
  3. Context executed
  4. Element type that you are trying to clear

An image from the object spy highlighting the element along with the support data will help me understand your scenario

To download the latest Appium Studio :

Hi Tom;

I did some further homework is well. I am using:

  • iOS 11.1.2
  • native app
  • native instrumented context
  • Element type if UITextField
  • Appium Studio 11.3.28

When the clear() is executed, a UICalloutBar appears on the screen providing me Select, Select All or Paste options.

I did the following and was able to effect a true clear():

public void clearText(WebElement element, AppiumDriver driver) throws RuntimeException{
if (driver instanceof IOSDriver){
if (element.getText().length() > 0) { //make sure the element has text in it
element.clear();
driver.findElement(By.xpath("//[@accessibilityLabel=‘Select All’]")).click();
driver.findElement(By.xpath("//
[@accessibilityLabel=‘Cut’]")).click();
}
} else if (driver instanceof AndroidDriver){
//fill this in when I get to android
} else {
throw new RuntimeException(“Don’t know what type of mobile device driver we’re connected to”);
}
}

Your thoughts on the behavior of clear() ??

thanks
walter

The clear option in iOS first selects the element, it clicks on the center of the element in-order to allow the element to be editable (iOS changes the edit text elements when the user click on it and adds the edit option)

my guess is that since your element is full of text, once clicked the UICalloutBar is invoked, and then the clear function fails

I’m not able to reproduce this over here - which is odd…

Can you switch context before you execute?

e.g driver.context(“NATIVE_APP”)

it is the clear instruction that we executed, results in the callBar showing up (Select, Select All and Paste).

I tried “native” and remember it making no difference, the callBar still appeared. I’ll try it again tomorrow.

I had the same problem in android and used the following approach. I hope it could be helpful.

//select entire text driver.findElement(By.className(“android.widget.EditText”)).sendKeys(Keys.HOME,Keys.chord(Keys.SHIFT,Keys.END));

// enter username.
driver.findElement(By.className(“android.widget.EditText”)).sendKeys(“username”);

Went back and tried Tom’s request of using NATIVE_APP instead of instrumented. The clear will still bring up the calloutBar. I went back to Instrumented and am providing the following spy shot.

@wakelt- it seems like your application have implemented a custom UITextView

  1. Can you open the object spy and highlight the text view itself?
  2. Which version of Appium studio are you running?

Regards,

Tom

@Tom, I’m using Appium Studio 11.3.83…

@Tom, your thoughts on this issue. How do we resolve ?

My best estimation is that your developers have implemented a custom UITextField
Thus, it behaves differently than with Apple standard element (this is not proven)

I don’t have any workaround that I can offer, and from my experience, in order for us to resolve this issue, we’ll need to have your IPA - so we could reproduce it over here and modify the behaivor

If you could share with me the IPA, it could facilitate this issue, and ones to come

Otherwise, your best effort will be to use the code you’ve shared previously on the thread

regards

Tom

Development claims the element isn’t customized. Anyway, not to worry, I can handle it in code.

My code will look as follows. It should handle elements that clear as Tom believes they should
as well as those that don’t but have Select ALL available.

private void clearText(WebElement element) throws RuntimeException {
element.clear();
if (element.getText().length() > 0) {
driver.findElement(By.xpath("//[@accessibilityLabel=‘Select All’]")).click();
driver.findElement(By.xpath("//
[@accessibilityLabel=‘Cut’]")).click();
}
}

For my case below-given code is working:

IOSElement emailAddressInput=driver.findElement(By.xpath(“THE XPATH”));
emailAddressInput.clear();
emailAddressInput.click();
emailAddressInput.click();
new WebDriverWait(driver, 30).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//[@text=‘Select All’]")));
driver.findElement(By.xpath("//
[@text=‘Select All’]")).click();
driver.findElement(By.xpath("//*[@text=‘delete’]")).click();

Note: Please make sure to call the click() function twice.