2017-10-17 82 views
-2

我想驗證顯示在字符串中的日期是否符合今天的日期。Java/Webdriver - 將字符串中指定的日期與今天的日期進行比較

爲例子: 1.導航頁 2.獲取顯示字符串文本「的數據作爲當前的2017年10月17日」 3.斷言今天的日期爲合格/不合格

+0

優秀。哪一部分會造成你的困難? –

+0

我需要從文本中檢索日期作爲日期,並與系統中的當前日期進行比較。格式應該是(「yyyy-MM-dd」); – Gamcusa

+0

您是否知道[DateTimeFormatter類](https://docs.oracle.com/javase/8/docs/api/java/time/format/DateTimeFormatter.html)並嘗試使用它? –

回答

0

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

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` 
+0

可能的重複,讓我知道如果你們中的任何人會做請改變它! – Gamcusa

相關問題