0

Actually I want to delete all text in a text field, and I am using a loop calling device.press('KEYCODE_DEL') to achieve this.

But there are two disadvantages:

  1. Not efficient enough, especially when I don't know how many characters in the text field so I need set a large enough loop
  2. Need to move the cursor to the end before deleting

So I am trying to accomplish this by two steps:

  1. select all text
  2. press delete button

I found an similar question here which is not solved yet.

And there is an answer for how to select all text, but I think it has the same issues as my loop delete way.

I did several tests, and found a way close to it:

device.press('KEYCODE_MENU', 'MonkeyDevice.DOWN', '')
device.press('KEYCODE_A')
device.press('KEYCODE_MENU', 'MonkeyDevice.UP', '')

I thought these three steps accomplish a MENU+A operation. But it did not work every time. I executed this code for 20 times(in a loop) and found it only took effect for about 5-8 times.

Besides, I found these three steps will move the cursor to the first place most of the time.

Did anyone know why is this operation not reliable? Or any other suggestions to select all text?

Appreciate for any suggestions!

Community
  • 1
  • 1
WKPlus
  • 6,955
  • 2
  • 35
  • 53

2 Answers2

0

AndroidViewClient's EditText has a method for that:

def setText(self, text):
    """
    This function makes sure that any previously entered text is deleted before
    setting the value of the field.
    """
    if self.text() == text:
        return
    self.touch()
    guardrail = 0
    maxSize = len(self.text()) + 1
    while maxSize > guardrail:
        guardrail += 1
        self.device.press('KEYCODE_DEL', adbclient.DOWN_AND_UP)
        self.device.press('KEYCODE_FORWARD_DEL', adbclient.DOWN_AND_UP)
    self.type(text, alreadyTouched=True)
Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
0

Using AndroidViewClient its pretty easy. Try this code-

editText= vc.findViewByIdOrRaise(EDITTEXT ID in quotes)
(x,y) = editText.getXY()
editText.device.drag((x,y), (x,y), 2000, 1)
vc.dump()
device.press('KEYCODE_DEL')
hemdroid
  • 61
  • 1
  • 7