how do I Impersonate using script in ATF? Service-Now Orlando release

Chandan7
Kilo Contributor

Why do the step config  'Impersonate' not work in ATF in Orlando release. I tried in some of the test, but the Client Test Runner's page become unresponsive during the run. Also, I have noticed when I use the step config 'Impersonate', there is a warning saying- Impersonating an existing user may cause unexpected behavior for this test. Avoid potential issues by adding a 'Create a User' step instead. If someone is not supposed to use this step config, then why do it exist in the list.
'Create a user' is not so flexible, while working on a instance where there are lot of customizations. 

Can somebody help me please. Its urgent. 

Also, how do I Impersonate using script in ATF?

13 REPLIES 13

The SN Nerd
Giga Sage
Giga Sage

I'm not aware of 'Impersonate' being broken in Orlando, I'd raise a defect on HI for that. Perhaps I can give some context around the behaviour you are seeing.

'Create a user' only become available in New York - before it, 'Impersonate' Test Step was the only way to impersonate a user. I'd argue that Create a User is more flexible because you don't have to rely on User data that may not be present in all environments. 

Some customers have a set of test persona users already and prefer to use those, so there is still a Use Case for 'Impersonate', as well as for legacy purposes. You've got to keep it there, as customers who upgrade may have tests that use the legacy Test Step.

As a workaround, you can impersonate in Server Script as follows:

GlideImpersonate().impersonate(userSysId);

https://developer.servicenow.com/dev.do#!/reference/api/orlando/server_legacy/GlideImpersonateAPI#GI...

I'd still expect the Impersonate Test Step to work though, definitely raise it on HI to find out.


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

Hi @Paul Morris ,

thank you so much for your reply.

I tried using GlideImpersonate() in my custom step-config, that I created to create and Impersonate a user. Though it created the user, but failed to Impersonate.  I have written the below code in the 'Step execution script' of the StepConfig- 

var first_name = inputs.u_first_name;
var last_name = inputs.u_last_name;
var email = inputs.u_email;
var manager = inputs.u_manager;
var companies = inputs.u_company;

// create user 
var gr=new GlideRecord('sys_user');
gr.initialize();
gr.setValue('first_name', first_name);
gr.setValue('last_name', last_name);
gr.setValue('email', email);
gr.setValue('user_name', last_name+' '+ first_name );
gr.setValue('company', companies, 'name');
gr.setValue('manager', manager);
gr.insert();


//Query the new user-record 
var gr1=new GlideRecord('sys_user');
gr1.addQuery('first_name', first_name);
gr1.addQuery('last_name', last_name);
gr1.addQuery('active', 'true');
gr1.query();
// set the output as user's sysId
if (gr1.next()) {
var user_sysid = gr1.getUniqueValue();
outputs.u_user = user_sysid;

//Impersonate
// gs.getSession().impersonate(outputs.u_user);
GlideImpersonate().impersonate(user_sysid);
} 

All the variables starting with 'u_' are the Input and Output variables, that I have configured in the step-config. where is it going wrong? Is there any specific way to use GlideImpersonate()? i tried with gs.getsesssion().Impersoante as well. but that also did not work.

Also, about the  'Impersonate' issue that I am having , is that wherever I use that inbuilt stepconfig, 

  1. It doesn't impersonate, and continue with already logged-in user
  2. the page stops responding on the next step(coming after Impersonate). The test continue running on that step for infinite time.

If you find any solution through the HI ticket, pls let me know.

Hi Chandan,

So are you saying create a user step is not allowing you to impersonate with the newly created user

Did you enable Impersonate this user checkbox as true

find_real_file.png

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi Ankur, 

    Create a user step does allow me to impersonate with the newly created user.  But in my test case, I am not able to use that step.


I have requirement where, user's record have some mandatory field(like company, email, manager etc.). Also the approval workflow for any requested item is dependent on the user's manager. So If I create a user, which have manager field empty, then approval workflow will not start. Also if the email field is empty, the notification will not be triggered.


 The inbuilt Step config 'Create a user' that ATF have, doesn't allow to add additional detail(like company, email, manager) for the user created through it. 

Now, If I add the other details(email etc.) of user through Record-update query, then on the next step I need to impersonate( as I can not use Create a user again) that user. And when I use the step 'Impersonate', the page stops responding on the next step(coming after the step Impersonate).