Appium: Change Android DatePicker value Javascript/NodeJS

1k views Asked by At

We are trying to automate testing for a hybrid Android app but we are stuck on the date picker. We want to change the value within the date picker to a specific date (Jun. -> May). Therefore we have retrieved the EditText element and tried to change the text.

We have basically tried 2 possibilities:

  1. Setting the text directly by using sendKeys();
  2. And first selecting all the text and then setting the text

Using the first option appium told us that the text could not completely removed (Using appium 1.4.0, sendKeys should clear the edit text). Instead the first 2 characters where removed and we were stuck with the characters "n.". We have also tried setting the text (by using sendKeys()) on the number picker (changed android.widget.EditText -> android.widget.NumberPicker).

driver.elementsByClassName("android.widget.EditText").then(function (promisses) {
    var promise = promisses[0];
    return promise.sendKeys('May').setImplicitWaitTimeout(3000);
});

With the second option we tried using a touch action (wd.TouchAction) to long press the input field and selecting the whole text. Then we used sendKeys to overwrite the selected text. The problem with this option was that using the long press didn't work.

driver.elementsByClassName("android.widget.EditText").then(function (elements) {
    var promise = elements[0];

    var action = new wd.TouchAction();

    action.press({el: promise}).wait(5000).release();
    return driver.performTouchAction(action);
});

And

driver.elementsByClassName("android.widget.EditText").then(function (elements) {
var promise = elements[0];

return driver.perform(new wd.TouchAction().longPress(promise));

});

We have also tried changing the context. For retrieving the elements we needed the NATIVE context but to be sure we have also tried the WEBVIEW context and switching in between.

1

There are 1 answers

0
Suzanne On

Changing the datepicker text is done in 3 steps:

  1. Select the text with a long press (var action)
  2. Delete the text with a key event (deviceKeyEvent(67))
  3. Set the text you want (setText(text)/sendKeys(text))

Retrieving the edit text elements is done by class name which is on android 'android.widget.EditText'. In order to retrieve the elements you should also be in the Native context.

It should also be possible to do it directly on the number picker since it behaves just like an edit text. But we didn't try that.

var els;
return driver
       .elementsByClassName('android.widget.EditText')
       .then(function (_els) {
           els = _els;
           var el = els[0];

           return setDatePickerText(driver, el, month);
       })
       .then(function () {
           var el = els[1];
           return setDatePickerText(driver, el, year);
       });

function setDatePickerText(driver, el, text) {
  var pressDuration = 1000; 

  var action = (new wd.TouchAction()).press({el: el}).wait(pressDuration).release();
  return driver
       .performTouchAction(action)
       .deviceKeyEvent(67)
       .then(function () {
           return el;
       })
       .setText(text);
}