Skip to content
Advertisement

How to fetch the most recently added Date from the table

Scenario:

I have a Screen where i need to enter the Date by incrementing the Date by 10 days …. After entering the Date and saving the entered Date could come anywhere in the table. My Question is how do i fetch the newly entered date everytime from the table and store it so that with that date i can increment by date by 10.

Any leads can add more info

This is my Code:

List<WebElement> Date = driver.findElements(By.xpath("//table//tr/td[contains(@class,'mat-column-lastValidityDate')last()]")); = This will read all the rows of the Column Last Validity Date

If you above code – I initially fetched the Last row Date always as all the Newly Added rows would appear Last

and then – I get the

Datetext - String datetext = Date.get(i).getText();

And then i navigate to the Create Screen:

String NewbtElem = AppXPathsConstants.buttonXpath_replace.replace("XXXX", "New");
                    clickOnButton(driver, NewbtElem);

After Naigating to the Create Screen – I enter the below code in order to Increment the Date:

    System.out.println("Date before Addition: "+datetext);

                    //Specifying date format that matches the given date
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    SimpleDateFormat outputDateFormat = new SimpleDateFormat("MM/dd/yyyy");

                    Calendar c = Calendar.getInstance();

                    try{

                        //Setting the date to the given date

                        c.setTime(sdf.parse(datetext));



                    }catch(ParseException e){
                        e.printStackTrace();
                    }

                    //Number of Days to add
                    int daysToIncrement = 10;
                    c.add(Calendar.DATE, daysToIncrement);  
                    //Date after adding the days to the given date

                    //String newDate = sdf.format(c.getTime());
                    //System.out.println("Get the New Date:"+newDate);


                    String newDate = outputDateFormat.format(c.getTime());

                    System.out.println("Print the latest date:"+newDate);
After which i enter the new date as seen in the below code:
inputEntry(driver, columnInputEntryXpath_replace.replace("XXXX","Last Validity Date"), newDate);

After Making Modification to the Code as given below this is how it is :

List<WebElement> Date = driver.findElements(By.xpath("//table//tr/td[contains(@class,'mat-column-lastValidityDate')]"));
                            
                    final String DATE_PATTERN = "yyyy-MM-dd";
                    LocalDate maxDate = getRows()
                            .stream()
                            .map(
                                    webElement -> LocalDate.parse(
                                            getDateAsText(WebElement),
                                            DateTimeFormatter.ofPattern("yyyy-MM-dd")
                                    )
                            )
                    .max(LocalDate::compareTo).get();
                    
                    LocalDate addedDate = maxDate.plusDays(10);
                    System.out.println(addedDate.format(DateTimeFormatter.ofPattern(DATE_PATTERN)));

Question is - List<WebElement> getRows(); i actually fetch the rows like int rows = Date.size(); so this being the case how do i getRows();

Advertisement

Answer

Say you have a method List<WebElement> getRows(); that returns rows of your table and method String getDateAsText(WebElement row) that returns date value as a String from a particular table row. Say also your date pattern is year-month-day. Then you fetch max date as:

final String DATE_PATTERN = "yyyy-MM-dd";
LocalDate maxDate = getRows()
        .stream()
        .map(
                webElement -> LocalDate.parse(
                        getDateAsText(webElement),
                        DateTimeFormatter.ofPattern(DATE_PATTERN)
                )
        )
.max(LocalDate::compareTo).get();
LocalDate addedDate = maxDate.plusDays(10);
System.out.println(addedDate.format(DateTimeFormatter.ofPattern(DATE_PATTERN)));

If you have already fetched dates like List<WebElement> dates = ... then you would use this:

LocalDate maxDate = dates
        .stream()
        .map(
                webElement -> LocalDate.parse(
                        webElement.getText(),
                        DateTimeFormatter.ofPattern(DATE_PATTERN)
                )
        )
        .max(LocalDate::compareTo).get();
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement