Can you please help me understand the script in ATF

Community Alums
Not applicable

Hi,

Can you please help me understand the script. It is a server side script of ATF. Have two input and output variables.

 

Description Generation:

 

function generateDescription() {
    var td = GlideTableDescriptor.get(step.inputs.u_table);
	if (!td) {
		gs.log("Invalid table name in test step: " + step.inputs.u_table);
		return gs.getMessage("Set field values");
	}
    var descriptionGenerator = new ATFStepDescriptionGenerator();
	var description;
	description = gs.getMessage("Validate there is at least one record in '{0}' matching query:\n{1}",
		[step.inputs.u_table.getDisplayValue(),
		descriptionGenerator.getConditionDescription(step.inputs.u_table, step.inputs.u_field_values)]);
	return description;
}
 generateDescription();

 

 

Script:

 

 

(function executeStep(inputs, outputs, stepResult, timeout) {

  // Find how many rows are in the table for this query
  var ga = new GlideAggregate(inputs.u_table);
  ga.addEncodedQuery(inputs.u_field_values);
  ga.addAggregate('COUNT');
  ga.query();

  if (ga.next()) {
    var totalRows = ga.getAggregate('COUNT');

    // Get a random row based on the number of rows in a table
    var randomRow = Math.floor(Math.random() * totalRows);

    var gr = new GlideRecord(inputs.u_table);
    gr.addEncodedQuery(inputs.u_field_values);
    gr.chooseWindow(randomRow, randomRow + 1);
    gr.query();

    if (gr.next()) {
      outputs.u_table = inputs.u_table;
      outputs.u_record = gr.getValue('sys_id');

      stepResult.setOutputMessage('Record "' + outputs.u_record + '" found.');
      stepResult.setSuccess();
      return;
    }
  }

  stepResult.setOutputMessage('No records found for table "' + inputs.u_table + '" and query "' + inputs.u_field_values + '"');
  stepResult.setFailed();

}(inputs, outputs, stepResult, timeout));

 

 

Regards

Suman P.

2 ACCEPTED SOLUTIONS

M Ismail
Tera Guru

Hi @Community Alums,

This script  aims to generate a description of what the test step is doing and then executes the test step itself.

Let's break down each part of the script:

1. Description Generation (function `generateDescription`):
- This function is responsible for generating a human-readable description of the test step.
- It first retrieves the `GlideTableDescriptor` for the table specified in the input (`u_table`). If the table descriptor is not found (which may happen if the table name is invalid), it logs an error and returns a default message indicating that field values need to be set.
- Then it creates an `ATFStepDescriptionGenerator` object and uses it to generate a description of what the test step is supposed to do, which involves validating the existence of at least one record in the specified table that matches the provided query (`u_field_values`).

2. Test Step Execution (function `executeStep`):
- This function executes the test step.
- It begins by counting the total number of records in the specified table that match the provided query (`u_field_values`).
- If there are records found, it randomly selects one of those records.
- It sets the output variables (`u_table` and `u_record`) to the table name and the sys_id of the randomly selected record, respectively.
- If a record is found, it sets the test step result to success; otherwise, it marks the test step as failed.

Overall, this script aims to dynamically generate a description of what the test step is supposed to do based on the input parameters and then executes the test step, verifying the existence of at least one record in the specified table that matches the given query. If such a record is found, it marks the test step as successful; otherwise, it marks it as failed.
Please hit helpful and accept this as a solution if it solved your problem.
Thank you!

View solution in original post

Hi @Community Alums,

Yes, you are correct. The "Description Generation" part of the script is not essential for the functionality of the ATF Test Step itself. It is mainly used for providing a human-readable description of the test step, which can be helpful for documentation purposes.

Since you are working on your ATF Test Step within an application scope and considering that "GlideTableDescriptor" doesn't work in other scopes, you can focus solely on the script provided for the test step. This script is responsible for executing the test logic, such as counting records in the specified table, selecting a random record, and handling the test result.

 

View solution in original post

4 REPLIES 4

M Ismail
Tera Guru

Hi @Community Alums,

This script  aims to generate a description of what the test step is doing and then executes the test step itself.

Let's break down each part of the script:

1. Description Generation (function `generateDescription`):
- This function is responsible for generating a human-readable description of the test step.
- It first retrieves the `GlideTableDescriptor` for the table specified in the input (`u_table`). If the table descriptor is not found (which may happen if the table name is invalid), it logs an error and returns a default message indicating that field values need to be set.
- Then it creates an `ATFStepDescriptionGenerator` object and uses it to generate a description of what the test step is supposed to do, which involves validating the existence of at least one record in the specified table that matches the provided query (`u_field_values`).

2. Test Step Execution (function `executeStep`):
- This function executes the test step.
- It begins by counting the total number of records in the specified table that match the provided query (`u_field_values`).
- If there are records found, it randomly selects one of those records.
- It sets the output variables (`u_table` and `u_record`) to the table name and the sys_id of the randomly selected record, respectively.
- If a record is found, it sets the test step result to success; otherwise, it marks the test step as failed.

Overall, this script aims to dynamically generate a description of what the test step is supposed to do based on the input parameters and then executes the test step, verifying the existence of at least one record in the specified table that matches the given query. If such a record is found, it marks the test step as successful; otherwise, it marks it as failed.
Please hit helpful and accept this as a solution if it solved your problem.
Thank you!

Community Alums
Not applicable

Hi @M Ismail ,

 

I am using this information to work on my ATF Test Step. Thank you so much for the help. I am using an application, but not in Global Scope. I read "GlideTableDescriptor" doesn't work in other scopes.

 

https://www.servicenow.com/community/developer-articles/atf-selecting-a-random-record/ta-p/2300035

 

Here, "Description Generation" is not really needed right? I can just use the script for the test step.

 

Regards

Suman P.

Hi @Community Alums,

Yes, you are correct. The "Description Generation" part of the script is not essential for the functionality of the ATF Test Step itself. It is mainly used for providing a human-readable description of the test step, which can be helpful for documentation purposes.

Since you are working on your ATF Test Step within an application scope and considering that "GlideTableDescriptor" doesn't work in other scopes, you can focus solely on the script provided for the test step. This script is responsible for executing the test logic, such as counting records in the specified table, selecting a random record, and handling the test result.

 

Community Alums
Not applicable

Thank you very much @M Ismail 

Regards

Suman P.