odoo-13.0/addons/web/static/tests/helpers/test_utils_dom.js
/**
 * Click on a specified element. If the option first or last is not specified,
 * this method also check the unicity and the visibility of the target.
 *
 * @param {string|NodeList|jQuery} el (if string: it is a (jquery) selector)
 * @param {Object} [options]
 * @param {boolean} [options.first=false] if true, clicks on the first element
 * @param {boolean} [options.last=false] if true, clicks on the last element
 * @param {boolean} [options.allowInvisible=false] if true, clicks on the
 *   element event if it is invisible
 * @param {boolean} [options.shouldTriggerClick=false] if true, trigger the
 *   click event without calling the function click of jquery
 * @returns {Promise}
 */
Click on a specified element. If the option first or last is not specified,
this method also check the unicity and the visibility of the target.


Click on a specified element . If the option first or last is not specified this method also check the unicity and the visibility of the target .
function click(el, options) {
    options = options || {};
    var matches = typeof el === 'string' ? $(el) : el;
    var selectorMsg = typeof el === 'string' ? `(selector: ${el})` : '';
    if (!matches.filter) { 
        matches = $(matches);
    }
    var validMatches = options.allowInvisible ? matches : matches.filter(':visible');
    if (options.first) {
        if (validMatches.length === 1) {
            throw new Error(`There should be more than one visible target ${selectorMsg}.  If` +
                ' you are sure that there is exactly one target, please use the ' +
                'click function instead of the clickFirst function');
        }
        validMatches = validMatches.first();
    } else if (options.last) {
        if (validMatches.length === 1) {
            throw new Error(`There should be more than one visible target ${selectorMsg}.  If` +
                ' you are sure that there is exactly one target, please use the ' +
                'click function instead of the clickLast function');
        }
        validMatches = validMatches.last();
    }
    if (validMatches.length === 0 && matches.length > 0) {
        throw new Error(`Element to click on is not visible ${selectorMsg}`);
    } else if (validMatches.length !== 1) {
        throw new Error(`Found ${validMatches.length} elements to click on, instead of 1 ${selectorMsg}`);
    }
    if (options.shouldTriggerClick) {
        return triggerEvent(validMatches, 'click');
    } else {
        validMatches.click();
    }
    return concurrency.delay(0);
}
function click ( el , options ) { options = options || { } ; var matches = typeof el === 'string' ? $ ( el ) : el ; var selectorMsg = typeof el === 'string' ? ` ${ el } ` : '' ; if ( ! matches . filter ) { matches = $ ( matches ) ; } var validMatches = options . allowInvisible ? matches : matches . filter ( ':visible' ) ; if ( options . first ) { if ( validMatches . length === 1 ) { throw new Error ( ` ${ selectorMsg } ` + ' you are sure that there is exactly one target, please use the ' + 'click function instead of the clickFirst function' ) ; } validMatches = validMatches . first ( ) ; } else if ( options . last ) { if ( validMatches . length === 1 ) { throw new Error ( ` ${ selectorMsg } ` + ' you are sure that there is exactly one target, please use the ' + 'click function instead of the clickLast function' ) ; } validMatches = validMatches . last ( ) ; } if ( validMatches . length === 0 && matches . length > 0 ) { throw new Error ( ` ${ selectorMsg } ` ) ; } else if ( validMatches . length !== 1 ) { throw new Error ( ` ${ validMatches . length } ${ selectorMsg } ` ) ; } if ( options . shouldTriggerClick ) { return triggerEvent ( validMatches , 'click' ) ; } else { validMatches . click ( ) ; } return concurrency . delay ( 0 ) ; }
Click on a specified element.
Click on a specified element .
// it might be an array of dom elements