In the Android app that I am testing, I am trying to click/tap the time picker. For example by default the time picker shows 08:00AM. I want to change the hour from 8 to 7 & then later on change the minutes but the xpath doesn't seem to be working. Its clicking something but the time still shows 08:00AM when I click the OK button via the code. If someone could help me then I will be able to change minutes & PM to AM as well.
Here's what I am using for tapping the hour picker so that 7 shows up selected:
driver.findElement(By.xpath("//android.widget.NumberPicker[0]/android.widget.Button[0]")).click();
Image:
解决方案
Here's the solution that worked for me:
Use MobileElement. I am using AndroidDriver also instead of RemoteWebDriver so I do not know whether that will work or not.
Forget click(). Use tap(int fingers, int duration) method of MobileElement. I have not found on the internet what duration is (seconds, milliseconds, etc so these values are just hit & trial).
I have posted the code below & it worked. It may need refinement but for now I am happy as I am not a hardcore programmer.
Now assume that hour is variable depending upon your test
// change hour
@SuppressWarnings("unchecked")
List buttons = driver.findElementsByClassName("android.widget.Button");
MobileElement hour = (MobileElement) buttons.get(0);
List highlights = driver.findElementsByClassName("android.widget.EditText");
MobileElement hourHighlight = (MobileElement) highlights.get(0); // highlighted hour
// lets say we need hour = 4
while (!hourHighlight.getText().equalsIgnoreCase("4"))
{
Thread.sleep(100);
if (hourHighlight.getText().equalsIgnoreCase("4"))
{
break;
}
hour.tap(1, 10);
}