index.js 6.88 KB
Newer Older
cxk committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
(function () {
    var settings = allure.getPluginSettings('screen-diff', { diffType: 'diff' });

    function renderImage(src) {
        return (
            '<div class="screen-diff__container">' +
            '<img class="screen-diff__image" src="' +
            src +
            '">' +
            '</div>'
        );
    }

    function findImage(data, name) {
        if (data.testStage && data.testStage.attachments) {
            var matchedImage = data.testStage.attachments.filter(function (attachment) {
                return attachment.name === name;
            })[0];
            if (matchedImage) {
                return 'data/attachments/' + matchedImage.source;
            }
        }
        return null;
    }

    function renderDiffContent(type, diffImage, actualImage, expectedImage) {
        if (type === 'diff') {
            if (diffImage) {
                return renderImage(diffImage);
            }
        }
        if (type === 'overlay' && expectedImage) {
            return (
                '<div class="screen-diff__overlay screen-diff__container">' +
                '<img class="screen-diff__image" src="' +
                expectedImage +
                '">' +
                '<div class="screen-diff__image-over">' +
                '<img class="screen-diff__image" src="' +
                actualImage +
                '">' +
                '</div>' +
                '</div>'
            );
        }
        if (actualImage) {
            return renderImage(actualImage);
        }
        return 'No diff data provided';
    }

    var TestResultView = Backbone.Marionette.View.extend({
        regions: {
            subView: '.screen-diff-view',
        },
        template: function () {
            return '<div class="screen-diff-view"></div>';
        },
        onRender: function () {
            var data = this.model.toJSON();
            var testType = data.labels.filter(function (label) {
                return label.name === 'testType';
            })[0];
            var diffImage = findImage(data, 'diff');
            var actualImage = findImage(data, 'actual');
            var expectedImage = findImage(data, 'expected');
            if (!testType || testType.value !== 'screenshotDiff') {
                return;
            }
            this.showChildView(
                'subView',
                new ScreenDiffView({
                    diffImage: diffImage,
                    actualImage: actualImage,
                    expectedImage: expectedImage,
                }),
            );
        },
    });
    var ErrorView = Backbone.Marionette.View.extend({
        templateContext: function () {
            return this.options;
        },
        template: function (data) {
            return '<pre class="screen-diff-error">' + data.error + '</pre>';
        },
    });
    var AttachmentView = Backbone.Marionette.View.extend({
        regions: {
            subView: '.screen-diff-view',
        },
        template: function () {
            return '<div class="screen-diff-view"></div>';
        },
        onRender: function () {
            jQuery
                .getJSON(this.options.sourceUrl)
                .then(this.renderScreenDiffView.bind(this), this.renderErrorView.bind(this));
        },
        renderErrorView: function (error) {
            console.log(error);
            this.showChildView(
                'subView',
                new ErrorView({
                    error: error.statusText,
                }),
            );
        },
        renderScreenDiffView: function (data) {
            this.showChildView(
                'subView',
                new ScreenDiffView({
                    diffImage: data.diff,
                    actualImage: data.actual,
                    expectedImage: data.expected,
                }),
            );
        },
    });

    var ScreenDiffView = Backbone.Marionette.View.extend({
        className: 'pane__section',
        events: function () {
            return {
                ['click [name="screen-diff-type-' + this.cid + '"]']: 'onDiffTypeChange',
                'mousemove .screen-diff__overlay': 'onOverlayMove',
            };
        },
        initialize: function (options) {
            this.diffImage = options.diffImage;
            this.actualImage = options.actualImage;
            this.expectedImage = options.expectedImage;
            this.radioName = 'screen-diff-type-' + this.cid;
        },
        templateContext: function () {
            return {
                diffType: settings.get('diffType'),
                diffImage: this.diffImage,
                actualImage: this.actualImage,
                expectedImage: this.expectedImage,
                radioName: this.radioName,
            };
        },
        template: function (data) {
            if (!data.diffImage && !data.actualImage && !data.expectedImage) {
                return '';
            }

            return (
                '<h3 class="pane__section-title">Screen Diff</h3>' +
                '<div class="screen-diff__content">' +
                '<div class="screen-diff__switchers">' +
                '<label><input type="radio" name="' +
                data.radioName +
                '" value="diff"> Show diff</label>' +
                '<label><input type="radio" name="' +
                data.radioName +
                '" value="overlay"> Show overlay</label>' +
                '</div>' +
                renderDiffContent(
                    data.diffType,
                    data.diffImage,
                    data.actualImage,
                    data.expectedImage,
                ) +
                '</div>'
            );
        },
        adjustImageSize: function (event) {
            var overImage = this.$(event.target);
            overImage.width(overImage.width());
        },
        onRender: function () {
            const diffType = settings.get('diffType');
            this.$('[name="' + this.radioName + '"][value="' + diffType + '"]').prop(
                'checked',
                true,
            );
            if (diffType === 'overlay') {
                this.$('.screen-diff__image-over img').on('load', this.adjustImageSize.bind(this));
            }
        },
        onOverlayMove: function (event) {
            var pageX = event.pageX;
            var containerScroll = this.$('.screen-diff__container').scrollLeft();
            var elementX = event.currentTarget.getBoundingClientRect().left;
            var delta = pageX - elementX + containerScroll;
            this.$('.screen-diff__image-over').width(delta);
        },
        onDiffTypeChange: function (event) {
            settings.save('diffType', event.target.value);
            this.render();
        },
    });
    allure.api.addTestResultBlock(TestResultView, { position: 'before' });
    allure.api.addAttachmentViewer('application/vnd.allure.image.diff', {
        View: AttachmentView,
        icon: 'fa fa-exchange',
    });
})();