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

[UI Script] Bulk delete test cases


#1

This extension is a small modification of my Bulk update test cases extension (go check it out, it uses REST API). It adds a “Delete selected cases” button beneath the table of cases and allows to delete all selected cases from current suite in one click (well, technically two, since confirmation alert).

name: Bulk delete cases
description: Adds possibility to delete multiple selected cases using TestRail REST API
author: Paul Danyliuk
version: 1.0
includes: ^suites/view
excludes:

js:
var deleteSelectedCases = function(onSuccess, onFailure) {
    var casesString = App.Tests.getSelected();
    if (casesString == "") {
        alert("Select one or more cases first");
        onFailure.apply();
        return;
    }

    var promises = [];

    casesString.split(',').forEach(function(caseID) {
        var deferred = $.Deferred();

        $.ajax("index.php?/api/v2/delete_case/" + caseID, {
            type        : "POST",
            dataType    : 'json',
            contentType : 'application/json'
        }).done(function() {
            deferred.resolve();
        }).error(function(data, textStatus) {
            deferred.resolve(textStatus);
        });

        promises.push(deferred.promise());
    });

    $.when.apply(null, promises)
        .done(function(promises) {
            var errors = [];
            for (var i = 0; i < arguments.length; i++) {
                if (typeof arguments[i] != 'undefined') {
                    errors.push(arguments[i]);
                }
            }
            if (errors.length > 0) {
                alert("There were errors during case delete:\n" + errors.join('\n'));
                onFailure.apply();
            } else {
                onSuccess.apply();
            }
        });
};

$(document).ready(function() {
    var $deleteButton = $('<a id="massDeleteCases"><img src="images/icons/cancel.png" class="negative" /> <span>Delete selected cases</span></a>');

    var onClick = function(button) {
        var $btn = $(button);
        if (!$btn.hasClass('disabled') && confirm("Are you sure?")) {
            $btn.addClass('disabled');
            var cachedSrc = $btn.find('img').attr('src');
            var cachedCaption = $btn.find('span').text();
            $btn.find('img').attr('src', 'images/animations/progressStandard.gif');
            $btn.find('span').text("Processing...");
            deleteSelectedCases(
                function() {
                    // Success
                    $btn.removeClass('disabled');
                    $btn.find('img').attr('src', cachedSrc);
                    $btn.find('span').text("Done. Reloading page...");
                    window.location.reload();
                },
                function() {
                    // Failure
                    $btn.removeClass('disabled');
                    $btn.find('img').attr('src', cachedSrc);
                    $btn.find('span').text(cachedCaption);
                }
            );
        }
    };

    $deleteButton.on('click', function() {
        onClick(this);
    });

    var $actions = $('<div class="actions" style="margin-top: 2em"></div>')
        .append($deleteButton);
    $("#groupContainer").after($actions);
});

With best regards,
Paul


#2

With TestRail 4.0 release this UI script is now deprecated :slight_smile: