Karate -Generate random alphanumeric string in the current feature file

白昼怎懂夜的黑 提交于 2019-12-24 08:38:16

问题


I am trying to generate alphanumeric string of length 5 in Karate.I am trying the below code.


Feature: Test user

 Background:

   Given url AM_HOST  
"  
   * def random_string =  

    function(s) {  
      var text = "";  
      var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";  
      for (var i = 0; i < s; i++)  
        text += possible.charAt(Math.floor(Math.random() * possible.length));  
      return text;  
    }  

    * def sessionId =  random_string(5)  
    * print sessionId  
"  
   >Scenario: Verify return user  
    Given path 'user/<sessionId>'  
    When method get  
    Then status 404  
    And match response.message == "User Not Found" 

I am not able to run this.Can you please let me know where the code has issues.The sessionId is not printed in cucumber report also.Should the quotes before and after function be removed in the feature file.The function gets printed in report. Thanks


回答1:


I see syntax related issues only, Try this,

Feature: Test user

 Background:
 Given url AM_HOST
 * def random_string =
 """
 function(s) {
   var text = "";
   var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456789";
   for (var i = 0; i < s; i++)
     text += possible.charAt(Math.floor(Math.random() * possible.length));
   return text;
 }
 """
 * def sessionId =  random_string(5)

Scenario: Verify return user 
 Given path 'user/' ,sessionId 
 When method get 
 Then status 404 
 And match response.message == "User Not Found"


来源:https://stackoverflow.com/questions/55245103/karate-generate-random-alphanumeric-string-in-the-current-feature-file

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