TruClient has no built-in support for adding custom headers, how can you do this?
Question ID: 104333
5
0

I want to do something like the web_add_auto_header() function does in a web script.

Marked as spam
Posted by (Questions: 6, Answers: 167)
Asked on March 1, 2011 6:03 pm
591 views
Answers (2)
7
Private answer

While you can't modify any old header in the Truclient script, you can modify the user-agent in the header. This function will modify the user-agent string programatically. It accesses the property that is available in the Runtime settings, however you can use this function to change the value dynamically in the script.

function setUserAgent(value) {
  var prefs = Components.classes[''@mozilla.org/preferences-service;1''].getService(Components.interfaces.nsIPrefService);
  var uaPref = prefs.getBranch(''general.useragent.'');
  if (value) 
         uaPref.setCharPref(''override'', value);
  else 
         uaPref.clearUserPref(''override'');
}

How do you call the function:

Add an Eval JS step to your script in Develop mode. In the step argument add this code:

setUserAgent(''**HELLO-WORLD**'');

The result is:

Running Vuser...
Starting iteration 1.
Starting action Action.
t=00009157ms: New User Started    [MsgId: MMSG-205200]
t=00009634ms: ** 1: Evaluate JS setUserAgent(''HELLO-WORLD''); ** started    [MsgId: MMSG-204251]
t=00009804ms: ** 1: Evaluate JS setUserAgent(''HELLO-WORLD''); ** successfully completed    [MsgId: MMSG-204250]
t=00010268ms: ** 2: Navigate to ''www.amazon.com'' ** started    [MsgId: MMSG-204251]
t=00010652ms: Request headers for ''http://www.amazon.com/'' (internal ID=1, 291 byte(s))    [MsgId: MMSG-204899]
          GET / HTTP/1.1
          Host: www.amazon.com
          User-Agent: **HELLO-WORLD**
          Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
          Accept-Language: en-us,en;q=0.5
          Accept-Encoding: gzip,deflate
          Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
          Keep-Alive: 300
          Connection: keep-alive

Where do I put the function definition?
Paste the code just as it appears above into the JS-functions.js file in the main VUGEN window.

Marked as spam
Posted by (Questions: 6, Answers: 167)
Answered on March 1, 2011 7:40 pm
1
Private answer

Thank you a lot,
I'm using your code for easier identification and to follow the path through our Backends.

Though I use different engines I need to evaluate JS in a If-Browser/Firefox-Step.

var UUID = ''vu'' + TC.getParam(''vuserid'') + ''_it'' + TC.getParam(''iteration'') + ''_OKN_'' + row.OKN;
//window.alert(UUID);
setUserAgent(UUID);

Works perfectly.

Do you now a way for changing userAgent for Chromium or IE via JS?
Thanks
P

Marked as spam
Posted by (Questions: 0, Answers: 2)
Answered on July 22, 2016 1:14 pm
EyeOnTesting

Welcome back to "EyeOnTesting" brought to you by Orasi Software, Inc.

X
Scroll to Top