Testing Strategies

Forsta HX Platform - API Scripting Guide

Testing Strategies

Test Data Generator:

/**
 * generateTestData - Creates random test data based on question type
 * @param {string} questionType - Type of question: 'single', 'multiple', 'text', 'number'
 * @returns {*} - Random value appropriate for the question type
 * @description Generates realistic test data for different question formats:
 *              - single: random number 1-5 (for radio buttons)
 *              - multiple: random array of 1-3 option codes (for checkboxes)
 *              - text: random alphanumeric string (for open-ends)
 *              - number: random integer 1-100 (for numeric inputs)
 */
function generateTestData(questionType) {
    switch(questionType) {
        case 'single':
            return Math.floor(Math.random() * 5) + 1;
        case 'multiple':
            var options = ['1', '2', '3', '4', '5'];
            var count = Math.floor(Math.random() * 3) + 1;
            return options.slice(0, count);
        case 'text':
            return 'Test response ' + Math.random().toString(36).substring(7);
        case 'number':
            return Math.floor(Math.random() * 100) + 1;
        default:
            return '';
    }
}

/**
 * autoFillSurvey - Automatically fills all survey questions with test data
 * @description Safety feature: only runs on localhost to prevent accidental
 *              use in production. Fills each question type with appropriate
 *              random data using generateTestData(). Logs completion to console.
 */
function autoFillSurvey() {
    // Safety check: only allow on localhost
    if(window.location.hostname !== 'localhost') {
        console.warn('Auto-fill only works on localhost');
        return;
    }
    
    f(AQ1).val(generateTestData('single'));
    f(AQ2).val(generateTestData('number'));
    f(BQ1).val(generateTestData('multiple'));
    f(CQ1).val(generateTestData('text'));
    
    console.log('Survey auto-filled with test data');
}

// Keyboard shortcut for testing (Ctrl+Shift+T)
$(document).on('keydown', function(e) {
    if(e.ctrlKey && e.shiftKey && e.key === 'T') {
        autoFillSurvey();
    }
});

Logic Testing Framework:

/**
 * testConditionalLogic - Runs automated tests on survey conditional logic
 * @description Defines an array of test cases, each with:
 *              - name: description of what's being tested
 *              - setup: function to set up the test conditions
 *              - assert: function returning true if test passes
 *              Runs each test and logs PASS/FAIL results to console.
 *              Use this to verify show/hide logic works correctly.
 */
function testConditionalLogic() {
    // Array of test case objects
    var tests = [
        {
            name: 'Show BQ1 when AQ1 is Yes',
            setup: function() { f(AQ1).val('1').trigger('change'); },
            assert: function() { return f(BQ1).is(':visible'); }
        },
        {
            name: 'Hide BQ1 when AQ1 is No',
            setup: function() { f(AQ1).val('0').trigger('change'); },
            assert: function() { return !f(BQ1).is(':visible'); }
        }
    ];
    
    tests.forEach(function(test) {
        test.setup();
        var passed = test.assert();
        console.log(test.name + ': ' + (passed ? 'PASS' : 'FAIL'));
    });
}

// Run tests in console
// testConditionalLogic();