Wednesday, March 12, 2025

automated testing – How you can deal with this Explicit date picker state of affairs the place i need to traverse again in selenium


Your concern is your month comparator, the road that claims

month1.compareTo(desiredMonth) < 0)

desiredMonth is a string, not a quantity. It’s good to use Java’s date compareTo class, not its string compareTo.

In case your date picker solely must go backwards in time, it’s adequate to get your code to run to only change the compareTo assertion to month1.equals(desiredMonth). This retains it from going into the second else situation when just one merchandise matches.

In the event you want the date picker to have the ability to intelligently discover a desired date in both course, you have to to verify it may well inform what month names are earlier than which different month names, ideally utilizing a datetime utility. You are able to do this comparability manually, with out datetime, as properly, like so:

  1. add an array of months close to your desiredMonth/desiredYear variables

String[] monthsOrder = {“January”, “February”, “March”, “April”,
“Might”, “June”, “July”, “August”, “September”, “October”, “November”,
“December”};

  1. write a way that returns the index of a month’s string from that date record

     public static int findMonthIndex(String[] months, String targetMonth) {
     for (int i = 0; i < months.size; i++) {
         if (months[i].equals(targetMonth)) {
             return i;
         }
     }
     //If month not in record simply crash
     throw new IllegalArgumentException("Dangerous month");
     }
    
  2. as an alternative of checking if the distinction between two month strings is zero, examine the outcomes of two calls to your month conversion operate:

findMonthIndex(monthsOrder, month1) < findMonthIndex(monthsOrder, desiredMonth))

  1. Your 12 months comparability can have the identical drawback. It’s good to examine the years as numbers slightly than as strings, and have instances dealing with every mixture of 12 months and month being higher or lower than the specified month.

There’s nonetheless plenty of room for simplification and exception catching however here’s what a completed and dealing code appears to be like like:

        String[] monthsOrder = {"January", "February", "March", "April", "Might", "June", "July", "August", "September", "October", "November", "December"};
    whereas (true) {
        String year1 = driver.findElement(By.className("ui-datepicker-year")).getText();
        String month1 = driver.findElement(By.className("ui-datepicker-month")).getText();
        if (year1.equals(desiredYear) && month1.equalsIgnoreCase(desiredMonth)) {
            System.out.println("Desired date reached: the 12 months is " + year1 + " and the month is " + month1);
            break;
        } else if (Integer.parseInt(year1) > Integer.parseInt(desiredYear)){
            driver.findElement(By.className("ui-icon-circle-triangle-w")).click on();
        } else if (Integer.parseInt(year1) == Integer.parseInt(desiredYear) && findMonthIndex(monthsOrder, month1) < findMonthIndex(monthsOrder, desiredMonth)) {
            driver.findElement(By.className("ui-icon-circle-triangle-e")).click on();
        } else if (Integer.parseInt(year1) == Integer.parseInt(desiredYear) && findMonthIndex(monthsOrder, month1) > findMonthIndex(monthsOrder, desiredMonth)) {
            driver.findElement(By.className("ui-icon-circle-triangle-w")).click on();
        } else {
            driver.findElement(By.className("ui-icon-circle-triangle-e")).click on();
        }
    }

    driver.give up();
}
public static int findMonthIndex(String[] months, String targetMonth) {
    for (int i = 0; i < months.size; i++) {
        if (months[i].equals(targetMonth)) {
            return i;
        }
    }
    //If month not in record simply crash
    throw new IllegalArgumentException("Dangerous month");
}

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles

PHP Code Snippets Powered By : XYZScripts.com