Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Write ATF test for Script Include

escanor
Giga Contributor

Hi,

I'm trying to write a run server script test for my script include below

var EmailValid = Class.create();
EmailValid.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
	isEmailValid: function(email){
			var getEmail = this.getParameter('sysparm_id') || email;	
			var regex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]{8,}/; 
			return regex.test(getEmail);
	},
    type: 'EmailValid'
});

Pls help me !

1 ACCEPTED SOLUTION

Ankush Agrawal
ServiceNow Employee
ServiceNow Employee

Hi

Please follow the below steps:

  1. Go to Automated Test --> Test
  2. Create a Test 
  3. Add a step to 'Run Server Side Script'
  4. Write the following lines
var emailToTest = 'test@amIValid.com';
var emailValid = new EmailValid();
return emailValid.isEmailValid(emailToTest);

You can use any one of the strategies as per your preference:

  1. Create an array having different emails. Set a boolean flag 'result' to true. Iterate over each element and set the boolean to false if any of the verification fails. Return this boolean flag outside the iteration. 
    1. Pros: Clean code
    2. Cons: Test result count will show only 1 test is executed. Not a clear report.
  2. Create multiple tests to assert different email. The report will clearly say which tests passed and which failed.
    1. Pros: Clean report
    2. Cons: Duplication of code
  3. Use Jasmine to validate multiple emails.
    1. Pros: Clean report as test count will cover all the test emails. No code redundancy.
    2. Cons: Need knowledge of Jamsine.

Please let me know if you need any further information. 

 

--
Best regards

Ankush

P.S.: Please mark helpful/correct as appropriate to help others in identifying correct solution.

View solution in original post

11 REPLIES 11

Ankush Agrawal
ServiceNow Employee
ServiceNow Employee

Hi

Please follow the below steps:

  1. Go to Automated Test --> Test
  2. Create a Test 
  3. Add a step to 'Run Server Side Script'
  4. Write the following lines
var emailToTest = 'test@amIValid.com';
var emailValid = new EmailValid();
return emailValid.isEmailValid(emailToTest);

You can use any one of the strategies as per your preference:

  1. Create an array having different emails. Set a boolean flag 'result' to true. Iterate over each element and set the boolean to false if any of the verification fails. Return this boolean flag outside the iteration. 
    1. Pros: Clean code
    2. Cons: Test result count will show only 1 test is executed. Not a clear report.
  2. Create multiple tests to assert different email. The report will clearly say which tests passed and which failed.
    1. Pros: Clean report
    2. Cons: Duplication of code
  3. Use Jasmine to validate multiple emails.
    1. Pros: Clean report as test count will cover all the test emails. No code redundancy.
    2. Cons: Need knowledge of Jamsine.

Please let me know if you need any further information. 

 

--
Best regards

Ankush

P.S.: Please mark helpful/correct as appropriate to help others in identifying correct solution.

i add this describe function like example above of jasmine

(function(outputs, steps, stepResult, assertEqual) {
    // add test script here
	var email = 'test@email.com';
	var emailValid = new EmailValid();
	return emailValid().isEmailValid(email);
	
	describe('my suite of script tests', function() {
            it('should meet expectations', function() {
                 expect(true).not.toBe(false);
           });
      });
})(outputs, steps, stepResult, assertEqual);
// uncomment the next line to execute this script as a jasmine test
jasmine.getEnv().execute();

escanor
Giga Contributor

can you show me more detail if i choose jasmine way

Here you go

(function(outputs, steps, stepResult, assertEqual) {
    // add test data in the array testEmails
    var testEmails = ['test@email.com', 'test2#email.co'];

    var emailValid = new EmailValid();

    describe('my suite of script tests', function() {
        //Iterate over each data and verify
    	testEmails.forEach(function(email) { 
    		it('should meet expectations', function() {
                        //optional line to print the test data
                        gs.info(email);
    			expect(emailValid.isEmailValid(email)).toBe(true);
    		});
    	}, this);
    });
})(outputs, steps, stepResult, assertEqual);
// uncomment the next line to execute this script as a jasmine test
jasmine.getEnv().execute();