Karate Framework - Why Javascript function returns an array with NaN values?

拟墨画扇 提交于 2019-12-29 09:27:23

问题


I wrote javascript function in Karate scenario and the function takes in current date in argument and gets date, year, month and adds them in a array. But for some unknown reason I get NaN values. Please see below karate steps that I have been using.

    * def dateArr2 = []
    * def dateParse =
        """
            function(myOrderDate)
            {
              dateArr2.add(myOrderDate); // this is for test purpose

              var today = new Date(myOrderDate);
              var dd = today.getDate();     
              var mm = today.getMonth()+1; //January is 0!
              var yyyy = today.getFullYear();
              dateArr2.add(yyyy);
              dateArr2.add(mm);
              dateArr2.add(dd);               
            }
        """ 
   * def ongoingDateTime = "2018-10-19T11:53:39.8795965Z"
   * eval dateParse(ongoingDateTime)     

Note, the similar javascript code works for me if I am executing in js execution environment such as sublime-text.


回答1:


Just keep it simple and use Java please. There are examples in the doc: https://github.com/intuit/karate#java-interop

If it is too troubling, write Java utilities.

* def toDate =
    """
    function(s) {
      var SimpleDateFormat = Java.type('java.text.SimpleDateFormat');
      var sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS");
      return sdf.parse(s)           
    }
    """ 
* def raw = "2018-10-19T11:53:39.8795965Z"
* def date = toDate(raw)
* print date.day, date.month, date.year

Just look at the API for java.util.Date and you have all of that now.



来源:https://stackoverflow.com/questions/52892338/karate-framework-why-javascript-function-returns-an-array-with-nan-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!