This repository has been archived by the owner on Sep 28, 2020. It is now read-only.
forked from mattermost/mattermost-webapp
-
Notifications
You must be signed in to change notification settings - Fork 9
/
index.js
82 lines (68 loc) · 3.13 KB
/
index.js
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
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
// ***********************************************************
// Read more at: https://on.cypress.io/configuration
// ***********************************************************
/* eslint-disable no-loop-func */
import './ui_commands';
import './api_commands';
import './task_commands';
import '@testing-library/cypress/add-commands';
import 'cypress-file-upload';
import addContext from 'mochawesome/addContext';
Cypress.on('test:after:run', (test, runnable) => {
// Only if the test is failed do we want to add
// the additional context of the screenshot.
if (test.state === 'failed') {
let filename = Cypress.spec.name + '/';
let parentNames = '';
// Define our starting parent
let parent = runnable.parent;
// If the test failed due to a hook, we have to handle
// getting our starting parent to form the correct filename.
if (test.failedFromHookId) {
// Failed from hook Id is always something like 'h2'
// We just need the trailing number to match with parent id
const hookId = test.failedFromHookId.split('')[1];
// If the current parentId does not match our hook id
// start digging upwards until we get the parent that
// has the same hook id, or until we get to a tile of ''
// (which means we are at the top level)
if (parent.id !== `r${hookId}`) {
while (parent.parent.id !== `r${hookId}`) {
if (parent.title === '') {
// If we have a title of '' we have reached the top parent
break;
} else {
parent = parent.parent;
}
}
}
}
// Now we can go from parent to parent to generate the screenshot filename
while (parent) {
// Only append parents that have actual content for their titles
if (parent.title !== '') {
parentNames = parent.title + ' -- ' + parentNames;
}
parent = parent.parent;
}
// Clean up strings of characters that Cypress strips out
const charactersToStrip = /[;:"<>/]/g;
parentNames = parentNames.replace(charactersToStrip, '');
const testTitle = test.title.replace(charactersToStrip, '');
// If the test has a hook name, that means it failed due to a hook
// and consequently Cypress appends some text to the file name
const hookName = test.hookName ? ' -- ' + test.hookName + ' hook' : '';
filename += parentNames + testTitle + hookName + ' (failed).png';
// Add context to the mochawesome report which includes the screenshot
addContext({test}, {
title: 'Failing Screenshot: >> screenshots/' + filename,
value: 'screenshots/' + filename,
});
}
});
// Add login cookies to whitelist to preserve it
beforeEach(() => {
Cypress.Cookies.preserveOnce('MMAUTHTOKEN', 'MMUSERID', 'MMCSRF');
});