簡體   English   中英

Java/Webdriver - 將字符串中指定的日期與今天的日期進行比較

[英]Java/Webdriver - Comparing date specified in the string with today's date

我想驗證字符串中出現的日期是否與今天的日期匹配。

例如: 1. 導航到頁面 2. 獲取顯示的字符串文本“Data Current as of 2017-10-17” 3. 斷言到今天的日期以確認通過/失敗

`WebElement dateText = driver.findElement(By.xpath("//div[contains(.,'Data Current as of')]"));

    String fullDateString = dateText.getText();
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");

    String patternString = "(\\d+-\\d+-\\d+)";//use of Reg Ex

    Pattern pattern = Pattern.compile(patternString);

    Matcher matcher = pattern.matcher(dateText.getText());

    Date repodate = new Date();

    while (matcher.find()) {
        repodate = df.parse(matcher.group());
    }

    Date currDate = df.parse(df.format(new Date()));

    if (currDate == repodate) ///verifing
        ;
    {
        System.out.println(currDate);
        System.out.println(repodate);
    }

    Assert.assertEquals(repodate, currDate);//asserting`

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM