All files / wdio-cucumber-framework/src utils.js

100% Statements 60/60
100% Branches 62/62
100% Functions 17/17
100% Lines 58/58

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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      15x 8x     7x 5x   11x 22x 22x           2x 1x     1x                 12x               12x                   29x       69x 1x     68x 2x 2x   2x 1x 2x 1x     1x 2x 1x           2x     66x 66x 66x 66x               54x         54x 11x         11x 1x               54x 1x     54x 1x     54x 29x     54x       86x 5x 10x     81x       13x 13x 37x 12x   25x   13x   13x 12x 12x   12x 4x       9x    
import * as path from 'path'
 
export function createStepArgument ({ argument }) {
    if (!argument) {
        return undefined
    }
 
    if (argument.type === 'DataTable') {
        return [{
            rows: argument.rows.map(row => (
                {
                    cells: row.cells.map(cell => cell.value),
                    locations: row.cells.map(cell => cell.location)
                }
            ))
        }]
    }
 
    if (argument.type === 'DocString') {
        return argument.content
    }
 
    return undefined
}
 
/**
 * builds test parent string from feature and scneario names
 * @param {object} feature cucumber feature object
 * @param {object} scenario cucumber scenario object
 */
export function getTestParent(feature, scenario) {
    return `${feature.name || 'Undefined Feature'}: ${scenario.name || 'Undefined Scenario'}`
}
 
/**
 * builds test title from step keyword and text
 * @param {object} step cucumber step object
 */
export function getTestStepTitle(step) {
    return ((step.keyword || '') + (step.text || 'Undefined Step')).trim()
}
 
/**
 * builds test full title from test parent and title
 * NOTE: this function is exported for testing only
 * @param {string} parent parent suite/scenario
 * @param {string} stepTitle step/test title
 */
export function getTestFullTitle(parent, stepTitle) {
    return `${parent}: ${stepTitle}`
}
 
export function getUniqueIdentifier (target, sourceLocation) {
    if (target.type === 'Hook') {
        return `${path.basename(target.location.uri)}${target.location.line}`
    }
 
    if (target.type === 'ScenarioOutline') {
        let name = target.name || target.text
        const line = sourceLocation.line || ''
 
        if (Array.isArray(target.examples)) {
            target.examples[0].tableHeader.cells.forEach((header, idx) => {
                if (name.indexOf('<' + header.value + '>') === -1) {
                    return
                }
 
                target.examples[0].tableBody.forEach((tableEntry) => {
                    if (tableEntry.location.line === sourceLocation.line) {
                        name = name.replace('<' + header.value + '>', tableEntry.cells[idx].value)
                    }
                })
            })
        }
 
        return `${name}${line}`
    }
 
    const name = target.name || target.text
    const location = target.location || target.locations[0]
    const line = (location && location.line) || ''
    return `${name}${line}`
}
 
/**
 * format message
 * @param {object} message { type: string, payload: object }
 */
export function formatMessage ({ type, payload = {} }) {
    let message = {
        ...payload,
        type: type
    }
 
    if (payload.error) {
        message.error = payload.error
 
        /**
         * hook failures are emitted as "test:fail"
         */
        if (payload.title && payload.title.match(/^"(before|after)( all)*" hook/g)) {
            message.type = 'hook:end'
        }
    }
 
    /**
     * Add the current test title to the payload for cases where it helps to
     * identify the test, e.g. when running inside a beforeEach hook
     */
    if (payload.ctx && payload.ctx.currentTest) {
        message.currentTest = payload.ctx.currentTest.title
    }
 
    if (type.match(/Test/)) {
        message.passed = (payload.state === 'passed')
    }
 
    if (payload.title && payload.parent) {
        message.fullTitle = getTestFullTitle(payload.parent, payload.title)
    }
 
    return message
}
 
export function compareScenarioLineWithSourceLine (scenario, sourceLocation) {
    if (scenario.type.indexOf('ScenarioOutline') > -1) {
        return scenario.examples[0].tableBody
            .some((tableEntry) => tableEntry.location.line === sourceLocation.line)
    }
 
    return scenario.location.line === sourceLocation.line
}
 
export function getStepFromFeature (feature, pickle, stepIndex, sourceLocation) {
    let combinedSteps = []
    feature.children.forEach((child) => {
        if (child.type.indexOf('Scenario') > -1 && !compareScenarioLineWithSourceLine(child, sourceLocation)) {
            return
        }
        combinedSteps = combinedSteps.concat(child.steps)
    })
    const targetStep = combinedSteps[stepIndex]
 
    if (targetStep.type === 'Step') {
        const stepLine = targetStep.location.line
        const pickleStep = pickle.steps.find(s => s.locations.some(loc => loc.line === stepLine))
 
        if (pickleStep) {
            return { ...targetStep, text: pickleStep.text }
        }
    }
 
    return targetStep
}