简体   繁体   English

如何自动化 Android 6.0 日期选择器来设置任何日期?

[英]How to automate Android 6.0 Date picker to set any date?

Can anyone help me on how to automate DatePicker in Android 6.0 using Appium and Java?任何人都可以帮助我如何使用 Appium 和 Java 在 Android 6.0 中自动化 DatePicker? I was able to automate it only for the current month dates.我只能在当月日期自动执行它。 But I need to set the date for any date I am passing to the method.但是我需要为传递给该方法的任何日期设置日期。

日期选择器图像

I have finally solved this.我终于解决了这个问题。 What I did was from the date picker I have taken the current year and the month.我所做的是从我选择当前年份和月份的日期选择器。 And from that I have calculated the number of taps to go forward or number of taps for go backward to the given date.从中我计算了前进的点击次数或后退到给定日期的点击次数。 Here is the code snippet for that.这是代码片段。

public class Base {
    // Pass monthName param as "August"
    public int getMonthNumber(String monthName) throws ParseException {
        Date date = new SimpleDateFormat("MMMM").parse(monthName);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        System.out.println(calendar.get(Calendar.MONTH) + 1);
        return calendar.get(Calendar.MONTH) + 1;
    }

    // Pass date param as "Sun, Jul 1"
    public String getMonthNameInThreeChars(String date) {
        return date.substring(5, 8);
    }
}


public class CommonLocators extends Base {

public void setAndroidDatePicker(String date) throws IOException, ParseException {
        int thisYear = Integer.valueOf(androidDriver.findElement(By.id("android:id/date_picker_header_year")).getAttribute("name"));
        String today = androidDriver.findElement(By.id("android:id/date_picker_header_date")).getAttribute("name");
        int thisMonth = getMonthNumber(getMonthNameInThreeChars(today));

        // Split the given date into date, month and year
        String[] splitdate = date.split("\\s+");

        int givenDay = Integer.valueOf(splitdate[0]);
        int givenMonth = getMonthNumber(splitdate[1]);
        int givenYear = Integer.valueOf(splitdate[2]);

        int forwardTaps = 0;
        int backwardTaps = 0;
        int yearFactor = 0;

            if (givenYear == thisYear)
            {
                if (givenMonth >= thisMonth)
                {
                    forwardTaps = givenMonth - thisMonth;
                } else {
                    backwardTaps = thisMonth - givenMonth;
                }
            }
                else if (givenYear > thisYear)
                {
                    yearFactor = (givenYear - thisYear) * 12;
                        if (givenMonth >= thisMonth)
                        {
                            forwardTaps = yearFactor + (givenMonth - thisMonth);
                        } else {
                            forwardTaps = yearFactor - (thisMonth - givenMonth);
                        }
                }
                    else {
                        yearFactor = (thisYear - givenYear) * 12;
                            if (givenMonth >= thisMonth)
                            {
                                backwardTaps = yearFactor - (givenMonth - thisMonth);
                            } else {
                                backwardTaps = yearFactor + (thisMonth - givenMonth);
                            }
                    }


        for (int i=1; i<=forwardTaps; i++) {
            androidDriver.findElement(By.id("android:id/next")).click();
        }

        for (int i=1; i<=backwardTaps; i++) {
            androidDriver.findElement(By.id("android:id/prev")).click();
        }

        String xpath = "//android.view.View[@text='day']";
        androidDriver.findElement(By.xpath(xpath.replace("day", String.valueOf(givenDay)))).click();
        //Tap on OK button of the date picker
        androidDriver.findElement(By.id("android:id/button1")).click();

    }
}


public class CommonStepDefinitions {

    @Test
    public void setDate(String date) throws IOException, ParseException {
        commonLocators.setAndroidDatePicker(date);
    }
}

使用Android (Espresso only)

$driver.execute_script('mobile:setDate', { year: 2020, monthOfYear: 1, dayOfMonth: 25, element: $driver.find_element(:id, 'android:id/datePicker') })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM