【问题标题】:How do you compare selector attributes in Testcafe?您如何比较 Testcafe 中的选择器属性?
【发布时间】:2020-01-27 20:09:44
【问题描述】:

我正在尝试将网页上的视频日期与今天的日期进行比较。如果两个日期之间的差异超过 X 天,则报告为 false。

网页上的视频有一个标签,格式为 yyyy-mm-dd

我设置了一个选择器来查找视频const videoDate = Selector('OPTA-video').withAttribute('data-secondary-time')

现在如何将变量设置为今天的日期并比较两者?我完全被卡住了!

我之前使用的是 Katalon Studio,这是完成相同工作的 groovy 脚本:


String videoDate = WebUI.getAttribute(findTestObject('OPTA-video'), 'data-secondary_time')

LocalDate todaysDate = LocalDate.now()

LocalDate videoDateParsed = LocalDate.parse(videoDate, dtf)

if (ChronoUnit.DAYS.between(videoDateParsed, todaysDate) > 1) {
    KeywordUtil.markFailed('The videos are 2+ days old.')
} else {
    KeywordUtil.logInfo('The videos are up to date.')
}

【问题讨论】:

  • 你能找到日期对象的元素吗? (xpath/css)?
  • 是的,所以元素的 Xpath 是 //*[@id="opta-app"]/ul[1]/li/a/span[1]

标签: testing attributes automated-tests selector testcafe


【解决方案1】:

您可以使用getAttributeTestCafe 方法来访问属性值。然后,将属性值解析为 JavaScript Date 对象:

String videoDate = Selector('OPTA-video').getAttribute('data-secondary-time');

Date videoDateParsed = Date.parse(videoDate);

Date todaysDate = Date.now()

...

在下面的thread 你可以找到如何比较 Date 对象。

【讨论】:

    【解决方案2】:

    这是我正在使用的脚本之一。

    //getting your XPath test value into a string
        String ann_time = 
           WebUI.getText(findTestObject("ObjectRepository/navigateTOElement/announcements_date"))
    
            //converting time to simple date format
            SimpleDateFormat sdf = new SimpleDateFormat('HH:mm')
    
            Date sdf_anntime = sdf.parse(new String(ann_time))
    
            //getting Current time
            SimpleDateFormat dateFormatGmt = new SimpleDateFormat('HH:mm')
    
            dateFormatGmt.setTimeZone(TimeZone.getTimeZone('GMT'))
    
            SimpleDateFormat dateFormatLocal = new SimpleDateFormat('HH:mm')
    
            currDate = dateFormatLocal.parse(dateFormatGmt.format(new Date()))
    
            // time gap in long format
            long duration = currDate.getTime() - sdf_anntime.getTime()
    
            //time gap to mins
            long diffInMinutes = TimeUnit.MILLISECONDS.toMinutes(duration)
    
    
            //compare time gap with globale variable 
            if (diffInMinutes < GlobalVariable.News_updated_time) {
    
    
                log.logInfo("system is getting updated,last updated "+ diffInMinutes + "min ago")
    
            } else {
    
                CustomKeywords.'errorMessage.logFailed.markStepFailed'('from 1 h, system was not updated')
    
                log.logInfo('from '+ diffInMinutes+ 'h, system was not updated')
            }
    

    【讨论】:

    • 请注意:- GlobalVariable.News_updated_time 是动态天数间隔(例如:1,2)
    猜你喜欢
    • 1970-01-01
    • 2012-01-12
    • 1970-01-01
    • 2011-05-02
    • 2013-05-19
    • 2013-09-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多