Couple of times when scripting a business process, I have come across situations where I needed to submit a dollar amount. For example, I would type in a number 12345 in a textbox and the application would convert it into $12,345 before submitting the request. There are three ways to solve this problem and they are:
1: Hard code the dollar value in the request. I wouldn't suggest doing it this way. However, there might be situations where it might be OK to hard code the value.
2: Save dollar values in a text file and then use a LR parameter for your requests.
3: Another way is to write a C function which converts a number into a dollar(/currency) value, which you can later assign to an LR parameter. The code is as below:
You will require to update the above code incase the final value is bigger than 20 characters. My code assumes the inital number is not bigger than 10 characters. This allows me to assign the final value back to variable "tempCurrency".
NOTE: Make sure you are freeing the memory. This code does not free the memory as I have left it for you to add that code.
Following is a screenshot of the final result when the above code is executed in LoadRunner.
1: Hard code the dollar value in the request. I wouldn't suggest doing it this way. However, there might be situations where it might be OK to hard code the value.
2: Save dollar values in a text file and then use a LR parameter for your requests.
3: Another way is to write a C function which converts a number into a dollar(/currency) value, which you can later assign to an LR parameter. The code is as below:
char *ConvertStringToCurrency(char string[20]) { char *tempResult; //temporary result variable int counter; int i=0; tempResult= (char *)malloc(sizeof(string)+10); //dynamically allocate memory for variable tempResultmake sure there is enough space for extra characters that will be added. strcpy(tempResult,"$"); for(counter=0;counter<strlen(string);counter++) { if((strlen(string)-counter)%3==0 && counter!=0) //insert a comma every third character and not at the start. { tempResult[++i]=','; } tempResult[++i]=string[counter]; } tempResult[++i]='\0'; return tempResult; //return final currency } Action() { char *tempCurrency; tempCurrency = (char *)malloc(sizeof(lr_eval_string ("{NonCurString}"))); //dynamically create the size of tempCurrency var based on size of NonCurString parameter sprintf(tempCurrency,"%s",lr_eval_string ("{NonCurString}")); //save NonCurString paramater value in to tempCurrency var lr_output_message ("Before Conversion %s",tempCurrency); //output the currency value before conversion sprintf(tempCurrency,"%s",ConvertStringToCurrency(tempCurrency)); //save the new value into tempCurrency lr_save_string (tempCurrency,"Currency"); //save the tempCurrency value into parameter called Currency lr_output_message ("After Conversion %s",lr_eval_string ("{Currency}")); // output the Currency value return 0; }
You will require to update the above code incase the final value is bigger than 20 characters. My code assumes the inital number is not bigger than 10 characters. This allows me to assign the final value back to variable "tempCurrency".
NOTE: Make sure you are freeing the memory. This code does not free the memory as I have left it for you to add that code.
Following is a screenshot of the final result when the above code is executed in LoadRunner.
No comments:
Post a Comment