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

what's ur result bro ? mine is false

Yes, it will be false as I purposefully added a negative scenario: test2#email.co

If you use the following line as the test data, the test will pass:

var testEmails = ['test@email.com', 'fn.ln@test2.com'];

 

Also, you can add another set to test for the negative scenario i.e. where the test returns false

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

    var emailValid = new EmailValid();

    describe('my suite of script tests', function() {
        //Iterate over each data and verify
    	testEmailsToBeTrue.forEach(function(email) { 
    		it('should be valid emails', function() {
                        //optional line to print the test data
                        gs.info(email);
    			expect(emailValid.isEmailValid(email)).toBe(true);
    		});
    	}, this);

    	testEmailsToBeFalse.forEach(function(email) { 
    		it('should be invalid emails', function() {
                        //optional line to print the test data
                        gs.info(email);
    			expect(emailValid.isEmailValid(email)).toBe(false);
    		});
    	}, this);


    });
})(outputs, steps, stepResult, assertEqual);
// uncomment the next line to execute this script as a jasmine test
jasmine.getEnv().execute();

i did like us said but i realized that there is error that in Global application there is no EmailValid function of mine. But describe function just support Global app. What should i do now bro ?

I understand your Script Include is in some scope (say x_snc_email) and the test you are creating is in global scope. You should be able to fix it in either of 2 ways:

  1. Create test cases in the same scope (e.g: x_snc_email). Then the above script will work.
  2. If cross scope access is enabled and you want to create the test in global scope, please access the script include with the scope prefix, e.g: 

instead of 

var emailValid = new EmailValid();

please use 

var emailValid = new x_snc_email.EmailValid();

 

Please replace x_snc_email with the appropriate scope prefix. 

hi bro,

i did it and it's worked. but it throw a result like this below

find_real_file.png

Why 2 valid email still be failed ? "Expected false to be true" 

(function(outputs, steps, stepResult, assertEqual) {
    // add test script here
	var testEmailTrue = ['test1@email.com', 'test2@gmail.com'];
	var testEmailFalse = ['test#email.com', 'fn.ln@blah'];
	var emailValid = new x_230904_example1.EmailValid();
	
	describe('my suit test for my script include', function(){
		testEmailTrue.forEach(function(email){
			it('should be an valid email', function(){
				gs.info(email);
				expect(emailValid.isEmailValid(email)).toBe(true);
			});
		}, this);
		
		testEmailFalse.forEach(function(email){
			it('should be an invalid email', function(){
				gs.info(email);
				expect(emailValid.isEmailValid(email)).not.toBe(true);
			});
		}, this);
	});
})(outputs, steps, stepResult, assertEqual);
// uncomment the next line to execute this script as a jasmine test
jasmine.getEnv().execute();