Forum Discussion

KaileeT's avatar
KaileeT
New Contributor
6 years ago

What Opeation Should I Use?

I'm trying to use the following code to get the current date to set a field in my test:

var currentDate=aqDateTime.Now();
var dateTime=aqConvert.DateTimeToFormatStr(currentDate);

 

When I try to use the Keys operation and have the value mode be Variable, it enters the code rather than the value. I have the code as the default value for the variable. I'm not sure if that's the issue or the operation. Any help to get this working would be much appreciated.

4 Replies

  • Marsha_R's avatar
    Marsha_R
    Champion Level 3

    KaileeT

     

    What you have in your variables right now isn't being interpreted as code, it's being interpreted as a string.  The quickest thing to do is:

    mydatefield = eval(currentDate)

    mytimefield = eval(dateTime)

     

    The eval command causes what's in the variable to be interpreted the way you intended.  If you want that all in one field like

     

    mydatetimefield = eval(currentDate) + eval(dateTime)

     

    then you will need to play around with the % formatting to get the display to look pretty

    https://support.smartbear.com/testcomplete/docs/reference/program-objects/aqdatetime/date-and-time-format-specifiers.html

     

     

  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor

    Can you provide a screenshot showing the parameter settings for your keys operation?  There's an option called "mode" where you would need to determine what type of mode you are using for the parameter.  For what you have below, I'd actually set that mode to "code expression".

     

    Alternatively, you could use the "Set Variable Value" operation to create a local variable to your keyword test and, in the "Value" parameter for that, again, select code expression. 

     

    In both of the above cases, your code expression should be as follows... you are implying that you're running the code and taking the result and using it as the value.

     

    aqConvert.DateTimeToFormatStr(aqDateTime.Now(), '%m/%d/%Y %H:%M')

    Note that I added another parameter to the code... the DateTimeToFormatStr requires a parameter to indicate how you're formatting the string.

    • KaileeT's avatar
      KaileeT
      New Contributor

      tristaanogre Here is what I'm using to try to set the field with the value received from the variable. I'm setting the variable in the Variables page of a keyword test.

      • tristaanogre's avatar
        tristaanogre
        Esteemed Contributor

        OK... first, you should leave the default value of your variable blank.  Code is not evaluated on that page so anything you may enter there in the way of code is simply stored as a string.  This is why it's being entered as a string in your contorl. To evaluate a code expression and assign it to a variable, you need the Set Variable Value operation

         

        When you drag that into your keyword test, you'll get a dialog that asks you to select your variable.  Select the one you created and click Next

         

         

        When you click Next, you'll be prompted to choose the Mode for your parameter.  Set it to "Code Expression"

         

         

        Once you select code expression, you'll have a field where you can enter your code expression.  You should enter it as I had above where you just enter the expression to be evaluated and don't include any "=" to assign anywhere.

         

         

        This is how you set a variable to a code expression in a keyword test.