How to dynamically create URL having path in between URL using Karate framework - karate

I want to create a dynamic url using karate framework. lets assume URL I want to create is :
https://www.mars.com/mars/profile/{profileID}/line
In above URL {profileID} is path.
Currently I have written below feature file which is able to create the url however due using path keyword it encodes the url and add %0A after profile id.
https://www.mars.com/mars/profile/264%0A/line
Feature File:
#smoke
Scenario: Create a line score in existing profile
And def urlname = marsuri+ '/mars/profile/'
Given url urlname
Given path id + '/line'
Please let me know how can I create a URL with path in between URL without encoding it.

You are not using the path syntax correctly. Please read the documentation: https://github.com/intuit/karate#path
Make this change:
Given path id, 'line'
EDIT: please also see this answer: https://stackoverflow.com/a/54477346/143475

Actually that id variable wherever you are getting it from is having a new line at the end of the string, something like this "264\n" that is why it is getting encoded to 264%0A
If all you wanted to pass is "264" you have to remove the unwanted values before adding it to the path
Background:
* def removeNewLine = function(x){return x.replace("\n","")}
Scenario: Create a line score in existing profile
And def urlname = marsuri+ '/mars/profile/'
Given url urlname
* def id = removeNewLine(id)
Given path id + '/line'
If you can modify the data directly from the source where you are getting the id that would be great.

Related

Values of a particular column of CSV file is not fetched properly in Json payload (Karate Framework)

I am working on specified scenario too.
My .csv file is as follows:
And test steps are:
Scenario Outline: Create new content value for movie: '<Movie>'
When json payload = {'attributes':[{'entity_attribute_id': 41,'value': '<Genre>'},{'entity_attribute_id': 42, 'value': '<Language>'},{'entity_attribute_id': 39,'value': '<Movie>'},{'entity_attribute_id': 101,'value': '2020-12-03'}],'entity_type_id': '10'}
Given url baseUrl + postContentValues
And request payload
When method post
Then status 200
And print response
And def contentId = response.id
Examples:
|read('RoughTable.csv') |
On test execution, "Movie" are not fetched while "Genre" & "Language" are obtained as expected from .csv file
Please guide me for solving this issue.
I think there may be an issue with CSV loading. Can you add an extra dummy column before Movie.
Either way, please submit a way to replicate, this will help us fix it: https://github.com/intuit/karate/wiki/How-to-Submit-an-Issue

Karate Multi-url access in a single scenario

I want to access multiple url in a single scenario.
When the url is defined in Background and the another url is used in Scenario, the url is changed.
If I use path, the behavior is not expected.
Can it fix url in Background?
Feature: examples
Background:
* url 'https://jsonplaceholder.typicode.com'
Scenario: get all users and then get the first user by id
Given path 'users'
When method get
Then status 200
Given url 'https://api.github.com/search/repositories'
And param q = 'intuit/karate'
When method get
Then status 200
# The expected behavior is accessed to 'https://jsonplaceholder.typicode.com/users'.
# But the accual behavior is accessed to 'https://api.github.com/search/repositories/users'.
Given path 'users'
When method get
Then status 200
No, but if you move Given url 'https://api.github.com/search/repositories' to a second Scenario: it will work fine.
This is a deliberate design. Look at the hello world example. It makes 2 calls, but the url is mentioned only once, because the second call is just a path addition. This is the typical REST pattern.
So if you need to really do a different API call, you have to use the full url:
Background:
* def baseUrl = 'https://jsonplaceholder.typicode.com'
Scenario: get all users and then get the first user by id
Given url baseUrl
And path 'users'
When method get
Then status 200
Given url 'https://api.github.com/search/repositories'
And param q = 'intuit/karate'
When method get
Then status 200
Given url baseUrl
And path 'users'
When method get
Then status 200

How to parse data from jason and populate in REST Url

I can't provide data from json file to Rest URL in karate. My feature file structure is as below; Please help out.
Scenario Outline: Get response
Given def data = read('classpath:JsonFilePath/data.json')
And def data1 = someJson.data[0].data1
And def data2 = someJson.data[0].data2
And path = 'https://myurl/account/#(data1)/phoneno/#(data2)/someoperationname'
When method get
Then status success
In this case, I get invalid account and invalid phoneno error, which means the URL is not getting data from json file.
Please refer the docs: https://github.com/intuit/karate#rules-for-embedded-expressions
Also you have mixed up url and path refer the docs for this also.
Change to:
And url 'https://myurl'
And path 'account', data1, 'phoneno', data2, 'someoperationname'
Note how path can be comma delimited and variables can be mixed.
I repeat - please read the docs and examples carefully.

Yii transform url from param/param/param/ to param/param?param

Suppose I have a url like:
site.com/param1/value1/param2/value2/param3/value3/param4/value4
I need to convert this url when a user writes it in url line to:
site.com/param1/value1/param2/value2?param3=value3&param4=value4
P.S. - the number of parameters is variable.
How can I do it?
You need to change the UrlManager Rules according to the situation.
You need to configure the Url manager to handle the first two params as path url and let the rest

bazaar auto tag

I want to use the automatic_tag_name hook to automatic create tag name without the need of manually typing
I tried to write it like it : automatic_tag_name(branch name,10)= "GIL"
Is it the correct syntax? (i found iittle information on it in the documents)
Is it possible to create tag name from a file? this file will contains only the tag name
See this example pahe here:
http://doc.bazaar.canonical.com/latest/en/user-guide/hooks.html
SO correct call should be:
def post_push_autotag(push_result):
automatic_tag_name(push_result.new_revno)
branch.Branch.hooks.install_named_hook('post_push_autotag', post_push_autotag, 'My autotag')