1

I have a problem in setting the date from a date picker in Appium. I'm trying to set the date by sending keys in the fields in this way:

List<WebElement> pick = driver.findElements(By.className("android.widget.EditText"));           

        pick.get(0).sendKeys("21");
        pick.get(1).sendKeys("Mar");
        pick.get(2).sendKeys("1989");

This works fine in previous versions of API but since I'm testing in a different device now appium seems not to finding my elements correctly. Here is a photo from the inspector window that shows that I'm using the correct class to find the fields.enter image description here

Any ideas?Thanks!!

4 Answers4

2

Not sure what's the problem because some information is missing, but maybe try using swipe instead of sendKeys, it doesn't look like a sendKeys field.

Anyway I can recommend a workaround that I'm using when I need to change the date while testing, you can use the following adb command:

adb shell date -s YYYYMMdd.HHmmss

It's much faster and reliable, hope this helps.

nirsky
  • 2,955
  • 3
  • 22
  • 35
  • Make sure you have adb installed on your PC and usb debugging enabled on your device and just run the command via command line, this [link](http://stackoverflow.com/questions/8496494/running-command-line-in-java) will help you. – nirsky Jan 29 '15 at 07:58
  • But how does that help me automate a test? Basically every time that I will have to run my test I will have to be sending a date while the test will be running. Is there any way to send this adb command programmatically? –  Jan 30 '15 at 09:38
  • 1
    Yeah, just run in Java: `Runtime.getRuntime().exec("adb shell date -s YYYYMMdd.HHmmss");` – nirsky Feb 01 '15 at 07:01
0

Perform action with xpath

// Select month name in datepicker
driver.findElement(By.xpath("//android.widget.NumberPicker[@index='0']")).sendKeys("Jan");

// Select day in datepicker
driver.findElement(By.xpath("//android.widget.NumberPicker[@index='1']")).sendKeys("24");

// Select year in datepicker
driver.findElement(By.xpath("//android.widget.NumberPicker[@index='2']")).sendKeys("1987");
Rohit Doraya
  • 119
  • 1
  • 1
  • 16
0

Perform This way

    List<WebElement> textFieldsList = driver.findElements(By.className("android.widget.EditText")); 
    int size = textFieldsList.size();
    textFieldsList.get(0).sendKeys("test");
    textFieldsList.get(1).sendKeys("test");
    textFieldsList.get(2).sendKeys("test@gmail.com");
0

I resolved this as shown below in the appium-java client as none of the above worked for me:

Android emulator API level: 28

Android emulator version: 9

Appium java client version: 5.0.4

currentMonthElement in the code below is the WebElement that represents the month field in the datepicker. You can use find by xpath or whichever way you like to find this element using driver.findElement. The below code can be reused to enter value for date and year elements. In the place of currentMonthElement variable, please pass the date and year elements.

touchAction.longPress(LongPressOptions.longPressOptions().withElement(ElementOption.element(currentMonthElement))).release().perform();
driver.getKeyboard().sendKeys(Keys.DELETE);
String monthstr = "Aug";
while(!currentMonthElement.getText().equals(monthstr)){
    driver.getKeyboard().sendKeys("Aug");
}
//click on ok button after setting the date, month & year

driver.findElement(By.id("button1")).click();
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Rama
  • 43
  • 1
  • 2
  • 10