var testUserName = "userSysIdOrName"; // change
var me = gs.getUserID();
var session = gs.getSession();
try {
// Only impersonate if testUserName is provided as an argument into the function)
if (JSUtil.notNil(testUserName)) session.impersonate(testUserName);
// what you want to run as the impersonated user - START
// e.g. GlideRecord query etc etc
// what you want to run as the impersonated user - END
} catch (e) {
gs.error(
"Error whilst impersonating and running test:\n" + e,
);
} finally {
// Only impersonate back to the logged in user if testUserName is provided as an argument into the function)
if (JSUtil.notNil(testUserName)) session.impersonate(me);
}