Forum Discussion

nastester's avatar
nastester
Regular Contributor
5 months ago

How to validate current date in a table cell

I am trying to verify that a cell in a data table is the current date. It will always be the date of the test run but the object maps like this:

 

I am aware of using code snippets like aqDateTime.AddTime(aqDateTime.Today(), 0, 0, 0, 0) but how can I use this for the object recognition?

Is there a way to set the name mapping for that object to be current date?

 

2 Replies

  • AlexKaras's avatar
    AlexKaras
    Champion Level 3

    Hi,

     

    Short answer:
    I would use a code like this:

    var strXPath = aqString.Format("//td[.=%s]", aqString.Quote(aqConvert.DateTimeToFormatStr(aqDateTime.Today(), "%m/%d/%Y")));

    var anElement = <page>.FindElement(strXPath);

    if (anElement)

      // found

    else

      // not found

     

    Long answer:

    XPath provided on your screenshot basically says this: "look on the page for any table cell that contains current date".

    This is fine if the tested web page can contain not more that only one such cell.

    If more than one cell with current date can exist on the page then you must first get parent container that can contain not more than one such cell (this may be table, table row, table column, etc.) and search from this parent. In this case (if I remember XPath syntax correctly) you must use not a double slash at the start of XPath expression (//), but dot and slash (./).

    I.e.:

    var strXPath = aqString.Format("./td[.=%s]", aqString.Quote(aqConvert.DateTimeToFormatStr(aqDateTime.Today(), "%m/%d/%Y")));

    var aContainer = <page>.<parentContainer>;

    var anElement = aContainer.FindElement(strXPath);

    if (anElement)

      // found

    else

      // not found

     

  • rraghvani's avatar
    rraghvani
    Champion Level 3

    Use the Find method instead of name mapping, in this instance. Create your date string, concatenate this with your XPath query and pass it into the Find method. This will retrieve the object.