首页 > 解决方案 > 如何从表中获取最近添加的日期

问题描述

设想:

我有一个屏幕,我需要通过将日期增加 10 天来输入日期.... 输入日期并保存输入的日期后,可能会出现在表中的任何位置。我的问题是我如何每次从表中获取新输入的日期并将其存储,以便在该日期我可以按日期递增 10。

任何潜在客户都可以添加更多信息

这是我的代码:

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

如果你上面的代码 - 我最初总是获取最后一行日期,因为所有新添加的行都会出现最后

然后 - 我得到了

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

然后我导航到创建屏幕:

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

导航到创建屏幕后 - 我输入以下代码以增加日期:

    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);

在对代码进行如下修改后,它是这样的:

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();

标签: javaselenium

解决方案


假设您有一个List<WebElement> getRows();返回表行的方法和一个从特定表行String getDateAsText(WebElement row)返回日期值的方法。String还说您的日期模式是year-month-day. 然后你获取最大日期为:

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)));

如果您已经获取了日期,List<WebElement> dates = ...那么您将使用以下方法:

LocalDate maxDate = dates
        .stream()
        .map(
                webElement -> LocalDate.parse(
                        webElement.getText(),
                        DateTimeFormatter.ofPattern(DATE_PATTERN)
                )
        )
        .max(LocalDate::compareTo).get();

推荐阅读