ATF: How to account for elapsed time in testing workflows

jmiskey
Kilo Sage

I am building some ATF Tests to test out various workflows that we have.  Some of them have Timers in them (i.e. wait 5 minutes, wait 10 days before expiration).  How can I write ATF tests for those workflows which have timers in them that cause "a pause" in time, waiting for time to pass before proceeding to the next step?  Is there some command we can add to the ATF testing to tell it that a certain amount of time has passed?  Or will it just ignore timers and go to the next step in the workflow?

Thanks

1 ACCEPTED SOLUTION

TrenaFritsche
Tera Expert

Basically, I haven't built out the Description script, I was more interested in having the execution do what I was looking to do.

Here is a screen capture of the part of the test that will move the change request forward...update the timer...

The script that I wrote inside of the Test "Update Change Request Scheduled Job..." to execute the timer activity on the workflow is the following, and it only needs the sys_id of the change request form as the input to query its way thru tables to get to the correct sys_trigger table. There are no outputs.  The step just updates the timer for 2 minutes past the time it gets when the step runs and then the step sleeps for 2 minutes to allow the trigger job to actually run.

(function executeStep(inputs, outputs, stepResult, timeout) {
 
    var cntxID = '';
    var wfVerID = '';
    var actvID = '';
    var execID = '';
    var nextAction = 'No';
 
    //----Get Current Date Time  
 
    var gdt = new GlideDateTime();
   var schdt = new GlideDateTime();
               
    gdt.addSeconds(-10680); //Convert Time to Central + 2 minutes
 
   //--------------------------------- 
 
    var wfcontext = new GlideRecord('wf_context');
    wfcontext.addQuery('id', inputs.u_request);
    wfcontext.query();
    if (wfcontext.next()) {
        cntxID = wfcontext.sys_id;
        wfVerID = wfcontext.workflow_version;
    }
 
    var wfactivity = new GlideRecord('wf_activity');
    wfactivity.addQuery('name', 'Wait for CR Planned Start Date');
                wfactivity.addQuery('workflow_version.sys_id', wfVerID);
    wfactivity.query();
    if (wfactivity.next()) {
        actvID = wfactivity.sys_id;
    }         
               
    if (cntxID != '') {
        var wfexec = new GlideRecord('wf_executing');
        wfexec.addQuery('context.sys_id', cntxID);
        wfexec.addQuery('activity', actvID);
        wfexec.query();
        if (wfexec.next()) {
            execID = wfexec.sys_id;
        }
    }
    if (execID != '') {
        var sched = new GlideRecord('sys_trigger');
        sched.addQuery('document_key', execID);
        sched.query();
        if (sched.next()) {
            schdt = sched.next_action;
            sched.next_action = gdt;
            nextAction = 'Yes';
            sched.update();
        }
    }
 
                gs.sleep('120000');  //2 Minutes
 
    if (nextAction == 'Yes') {
        stepResult.setSuccess();
        stepResult.setOutputMessage = "Original Planned Start of " + schdt + " changed to " + gdt;
    } else {
        stepResult.setFailed();
    }
 
}(inputs, outputs, stepResult, timeout));

 

 

View solution in original post

23 REPLIES 23

Mohit Kaushik
Mega Sage
Mega Sage

Hi There,

The solution to make the ATF pause is to increase the "TIMEOUT" which will make that particular test step run for the specified amount of time.

Note: The field TIMEOUT is hidden on the form layout, but it can be modified from the list layout.

OR

you can use

gs.sleep('duration in milliseconds') under server side script.

 

Please mark this answer as correct and helpful as per the impact.

 

Thanks,

Mohit Kaushik

Thanks,
Mohit Kaushik
ServiceNow MVP (2023-2025)

I am not sure if I jsut don't understand what you are saying in your reply, or you misunderstood my question.  Let me clarify it with a simple example (I know it isn't realistic, but it gets the point across).

Let's say that we have a real simple workflow, that once a RITM is created, here are steps of the workflow:

1.  Manager Approval

2.  Wait 2 Days

3.  Create Task for Security

4.  Close RITM

So, if I were to set up an ATF to test this out, I would first create the steps to test the Manager Approval.  Then I would want to test the Task.  But according to the workflow, the Task won't be created right away.  There will be a 2 day wait before it gets created.

How do I account for that in the ATF testing?  I don't want my testing to run over a period of 2 days.  I would like to test it all at once.  So I think I need to somehow either tell the testing to ignore the Waiting, or somehow tell it that 2 days have passed.

If your answers do address my question accurately, can you explain your suggestions in more detail?  Especially the first one.

For your second one, would I just put that line in a "Run Server Side Script" step for the duration of 2 days (in milliseconds)?  Is that all the code would look like?  Most "Run Server Side Scripts" I have seen require a lot more than that (i.e. output, steps, stepResult).

Hi Jmiskey,

I looked into your requirement more clearly and found something similar on community. What i feel is that ATF will simply skip the waiting part and will check for tasks which is not even created. 

I understand that you don't want your test to run for two days and i am not sure if there is any solution to that.

And i feel that only by making the test step wait you can achieve this.

so that can be done on "Run Server Side Validation Script"

(function(outputs, steps, stepResult, assertEqual) {
    gs.sleep(10000); // Sleep for 10 seconds

})(outputs, steps, stepResult, assertEqual);

 

Refer the below link as well :

https://community.servicenow.com/community?id=community_question&sys_id=233e2854db743b84fece0b55ca96...

 

And the timeout suggestion which i gave you was something like as shown in below screenshot:

find_real_file.png

 

Thanks,

Mohit Kaushik

Thanks,
Mohit Kaushik
ServiceNow MVP (2023-2025)

Thanks for the clarification.  I see what you mean by the Timeout now.  We certainly do not want to be trying contstantly for 2 days!

So, I did some additional testing, and I was able to confirm that if I just go straight to the next step (Task) in my ATF, it fails out, saying it cannot find the Task (because it has not been created yet).

I tested the "Server Side Script", and that makes the workflow wait that length of time, until it does the next step.  It then "works", because the Task is there then, but doesn't solve the question/issue (which is how to bypass that).

We are trying to build out our ATF testing for upgrades, and we have some workflows that might have timers that wait up to a full year.  So it isn't really feasible to put in Timers or Sleep statements and let the test run for a full year.

So it appears that maybe there is no solution to this.  Maybe I will open a HI ticket, and see if they have any recommendations.

Thanks for trying, though.