首页 > 解决方案 > 从Robot Framework中的js文件调用函数

问题描述

有可能从文件中执行 javascript。

Execute JavaScript    ${CURDIR}/js_to_execute.js

但是我怎样才能从这个文件中按名称调用一个函数呢?

标签: javascriptseleniumautomated-testsrobotframework

解决方案


Execute Javascript无法使用开箱即用的方式调用函数。但是可以将函数名作为参数传递。

Execute Javascript    ${CURDIR}/js/utils.js    ARGUMENTS    clickElement    ${locator}

文件内容utils.js

var utils = utils || {};

(function (arguments) {
    utils.clickElement = function (locator) {
        // implementation of the function
    }

    // this piece of code does the trick
    // get function name from arguments and remove it from the list of arguments
    var functionName = [].shift.call(arguments);     
    // call the function and pass the arguments
    utils[functionName].apply(this, arguments);
})(arguments);

推荐阅读