java - 如何从表中获取最近添加的日期
问题描述
设想:
我有一个屏幕,我需要通过将日期增加 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();
解决方案
假设您有一个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();
推荐阅读
- javascript - 基于第一个可观察数据调用其他服务并等待服务给出结果
- javascript - 如何通过 discord.js 中的反应来扮演角色?
- git - 更新 git 子模块
- android - 启用 Progaurd 时改造中的无效请求正文
- python - ImportError:在运行代码时尝试在没有已知父包的情况下进行相对导入
- python - Tkinter:如何将文本插入到属于 Frame 的文本小部件中?
- javascript - 将页面上的所有英文文本自动翻译成阿拉伯语
- javascript - 单击删除按钮时如何删除数组的卡片和索引?
- python - 使用 python 列显示表格数据
- huawei-mobile-services - 华为 In App 购买令牌验证 - 意外错误“权利无效”