java 如何在 Appium Android 中处理日期选择器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29982202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to deal with datepicker in Appium Android
提问by Rohit Doraya
My android application is using datepicker but i am not able to select date through datepicker. I used following code in application for datepicker but it does not work.
我的 android 应用程序正在使用 datepicker,但我无法通过 datepicker 选择日期。我在 datepicker 的应用程序中使用了以下代码,但它不起作用。
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");
回答by Helping Hands
Swipe method will help you to scroll calendar dates , Make sure that you have added Java-client JARs to your project then only swipe method will support.
Swipe 方法将帮助您滚动日历日期,确保您已将 Java 客户端 JAR 添加到您的项目中,然后只有 swipe 方法将支持。
Example :
例子 :
First click on your calendar icon and then use following code :
Thread.sleep(5000);
for(int y=0;y<3;y++)
{
driver.swipe(350,511,350,577,0);
}
Swipe Syntax :
滑动语法:
driver.swipe(startx, starty, endx, endy, duration);
driver.swipe(startx, starty, endx, endy, duration);
Note : Above in code I have used sample co-ordinates so you change it according to your need. You can get exact co-ordinates from bound values of that date picker.
注意:上面的代码中我使用了示例坐标,因此您可以根据需要进行更改。您可以从该日期选择器的绑定值中获得精确的坐标。
I have used loop in above code as I want to swipe 3 times , so it is something like if current date is 1st maythen it will swipe till 4th may.
我在上面的代码中使用了循环,因为我想滑动 3 次,所以它就像如果当前日期是1 月,那么它会滑动到4 月。
you can modify loop as per your need.
您可以根据需要修改循环。
回答by Rohit Doraya
I have used Xpath to perform Datepicker action & it is working properly.
我已经使用 Xpath 来执行 Datepicker 操作并且它工作正常。
driver.findElement(By.xpath("//android.widget.NumberPicker[@index='0']")).sendKeys("Jan"); driver.findElement(By.xpath("//android.widget.NumberPicker[@index='1']")).sendKeys("24"); driver.findElement(By.xpath("//android.widget.NumberPicker[@index='2']")).sendKeys("1987");
driver.findElement(By.xpath("//android.widget.NumberPicker[@index='0']")).sendKeys("Jan"); driver.findElement(By.xpath("//android.widget.NumberPicker[@index='1']")).sendKeys("24"); driver.findElement(By.xpath("//android.widget.NumberPicker[@index='2']")).sendKeys("1987");
回答by user6328454
Check if thishelps
检查这是否有帮助
driver.FindElement(By.Id("com.eos.eos_du_su:id/ed_manufdate")).Click();
((AndroidElement)(driver.FindElement(By.XPath("//android.widget.NumberPicker[@index='0']//android.widget.Button[@index=0]")))).Tap(1, 2);
((AndroidElement)(driver.FindElement(By.XPath("//android.widget.NumberPicker[@index='1']//android.widget.Button[@index=0]")))).Tap(1, 2);
((AndroidElement)(driver.FindElement(By.XPath("//android.widget.NumberPicker[@index='2']//android.widget.Button[@index=0]")))).Tap(1, 2);
driver.FindElement(By.Id("android:id/button1")).Click();
回答by dipak joshi
For all the user who are still finding the way to select date can use the below code. I am using this code and working perfectly for me. It will work for calendar attached.
对于仍在寻找选择日期方式的所有用户,可以使用以下代码。我正在使用此代码并且非常适合我。它将适用于附加的日历。
do {
WebElement source = driver.findElement(By.xpath("//android.view.View[@instance='0']"));
WebElement destination = driver.findElement(By.xpath("//android.view.View[@instance='22']"));
TouchAction action = new TouchAction((PerformsTouchActions)driver);
System.out.println("Dragging item");
action.longPress(source).moveTo(destination).release().perform();
boolean bul = driver.findElementsByXPath("//android.view.View[@content-desc='24 January 2018']").isEmpty();
} while(bul!=false);
driver.findElementByAccessibilityId("24 January 2018").click();
NOTE:I used drag and drop touch action to scroll and this will scroll uptill given date is not found. I just selected same years previous month date. You can use same touch action to select desired year.
注意:我使用拖放触摸操作来滚动,这将向上滚动直到找不到给定的日期。我刚刚选择了同年上个月的日期。您可以使用相同的触摸操作来选择所需的年份。
回答by Ross Adamson
I wanted to do the same thing, but for a "calendar" mode DatePicker instead of the "spinner" mode. This is my solution, which has been working fine for me.
我想做同样的事情,但是对于“日历”模式 DatePicker 而不是“微调器”模式。这是我的解决方案,对我来说效果很好。
from datetime import datetime
datePickerYearTextViewXpath = "//android.widget.TextView[@resource-id='android:id/date_picker_header_year']"
# initialize your appium driver here
driver = getAppiumDriver()
# define some screen dimensions
screenSize = driver.get_window_size()
halfScreenWidth = screenSize['width'] // 2
halfScreenHeight = screenSize['height'] // 2
def getDatePickerCurrentDate(driver):
yearTextView = driver.find_element_by_xpath(datePickerYearTextViewXpath)
yearString = yearTextView.text
dateTextView = driver.find_element_by_xpath("//android.widget.TextView[@resource-id='android:id/date_picker_header_date']")
dateString = dateTextView.text
fullDateString = '{}, {}'.format(dateString, yearString)
currentDate = datetime.strptime(fullDateString, '%a, %b %d, %Y').date()
return currentDate
def setDatePickerDate(driver, targetDate):
# driver is an appium driver
# targetDate must be a datetime.date, not a datetime
currentDate = getDatePickerCurrentDate(driver)
if targetDate.year != currentDate.year:
yearTextView = driver.find_element_by_xpath(datePickerYearTextViewXpath)
yearTextView.click()
# you may need to adjust the following numbers
# depending on your screen size
swipeAmountPerYear = 49
yearsPerScreen = 8
swipeDuration = 400
yearOffset = targetDate.year - currentDate.year
# if target year is older, swipe up (negative)
swipeDirection = -1 if yearOffset < 0 else 1
swipeVector = yearsPerScreen * swipeAmountPerYear * swipeDirection
while True:
elements = driver.find_elements_by_xpath("//android.widget.TextView[@resource-id='android:id/text1']".format(targetDate.year))
found = False
for element in elements:
if element.text == str(targetDate.year):
element.click()
found = True
break
if found:
break
else:
driver.swipe(halfScreenWidth, halfScreenHeight, halfScreenWidth, halfScreenHeight - swipeVector, swipeDuration)
currentDate = getDatePickerCurrentDate(driver)
if targetDate.month != currentDate.month:
monthOffset = targetDate.month - currentDate.month
prevOrNext = 'prev' if monthOffset < 0 else 'next'
prevOrNextButtonXpath = "//android.widget.ImageButton[@resource-id='android:id/{}']".format(prevOrNext)
for i in range(abs(monthOffset)):
driver.find_element_by_xpath(prevOrNextButtonXpath).click()
targetDateContentDescription = targetDate.strftime('%d %B %Y')
driver.find_element_by_xpath("//android.view.View[@resource-id='android:id/month_view']/android.view.View[@content-desc='{}']".format(targetDateContentDescription)).click()
currentDate = getDatePickerCurrentDate(driver)
if currentDate != targetDate:
raise ValueError('Unable to set date picker({}) to target date({})!'.format(currentDate, targetDate))