Skip to content

Instantly share code, notes, and snippets.

@aksinghdce
Last active August 29, 2019 17:04
Show Gist options
  • Save aksinghdce/1e3d9096f7f767dab50e3f71baa33e5a to your computer and use it in GitHub Desktop.
Save aksinghdce/1e3d9096f7f767dab50e3f71baa33e5a to your computer and use it in GitHub Desktop.
Read excel data to parameterize Jmeter Sampler
/*
Problem Statement:
Based on variable URL patterns extract parameters into Jmeter variables for use by Samplers that would execute later on.
*/
// Get the input data populated
//Type 1: ${host}/api/c/<unified-id>/_/N-<nvalue>
//Type 2: ${host}/api/c/<unified-id>/_/N-<nvalue>
//Type 3: ${host}/api/c/<unified-id>/_/N-<nvalue>?Ns=<sortParam> {The code for this one is not present
//below}
/*
The URLs can take a form with two different kinds. First with only one <unified-id> and one <nvalue> and Second with two <unified-id>s and one <nvalue>.
We need to be able to adapt to either scenario.
*/
script_path = vars.get('SCRIPT_PATH')
us_input_file = vars.get('us_input_file')
def values_read = []
String fileContents = new File(script_path+"/"+us_input_file).getText('UTF-8');
fileContents.eachLine { line ->
values_read = line.split(',');
}
if (values_read.size() > 0) {
if (values_read[-1] != "") {
vars.put('unified_id', values_read[-2])
if (values_read[-2] != "") {
vars.put('nvalue', values_read[-1])
vars.put('path', '/api/c/${unified_id}/_/N-${nvalue}')
}
}
else {
if (values_read[0] != "") {
vars.put('unified_id', values_read[0])
if (values_read[1] != "") {
vars.put('unified_id2', values_read[1])
if (values_read[2] != "") {
vars.put('nvalue', values_read[2])
vars.put('path', '/api/c/${unified_id}/${unified_id2}/_/N-${nvalue}')
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment