Subscribe Join 180,000+ subscribers and receive articles from our blog about software quality, testing, QA and security. Subscribe

How to make a custom test case field required when other field is checked

Hi,

We have a requirement to make a custom integer type field ‘sanity_id’ required when another custom checkbox type field ‘sanity_test_case’ is checked during test case creation and update.

Can anyone please help us with the UI Script for the above that will trigger an error message for the user if ‘sanity_id’ field is left blank after ‘sanity_test_case’ field is checked?

we have multiple project and the ‘sanity_test_case’ is added for one project only, so will the script impact the other projects also?

can we do it for only a single suite?

Thanks in advance

can anyone please help me with this?

Hi abhishekgupta,
I can help. This script throws an alert when the checkbox is checked but the field is not filled out.
I could not think of another way because subscribing on the save button would save the test case and return to the last site and then show the message. Also saving with keyboard shortcut is possible, too. And I do not know how to solve that.
This script is currently NOT active in the test run view where tests can also be edited (additional changes are needed for that).

Enabling an UI script in only one project is possible.
To get the ID of a project hover or click on a project on the dashboard and it is in the URL. Change it to the project in which you want to enable the UI script.
Also you have to double check the IDs of the checkbox and the text field.

name: Test: Make a field mandatory when checkbox is checked
description: 
author: Jteve Sobs
version: 1.0
includes: ^cases/(edit|add)
excludes: 

js:
$(document).ready(
	function() {
		console.log('Project ID: ' + uiscripts.context.project.id);

		if (uiscripts.context.project.id == 27) {
			var textfield = document.getElementById('custom_sanity_id').value;

			var checkbox = document.getElementById('custom_sanity_test_case');			
			
			checkbox.addEventListener( 'change', function() {
			    if(this.checked) {
			        if (isEmpty(textfield)) {
					   alert('Field "Sanity id" has to be filled when "Sanity test case" is checked.');
				    }
			    }
			});
		}
	}
);

function isEmpty(str) {
	return !str.trim().length;
}

css:
div.some-class {
}

Best regards