量角器中的多个 Firefox 配置文件

Multiple firefox profiles in protractor

关注 Configuring multiple capabilities with promises 个主题。

用例: 我有两个单独的测试,需要在 javascript 禁用 和 [=33 的情况下启动 Firefox =]禁用本地存储。这意味着我需要两个带有 javascript.enabled = falsedom.storage.enabled = false 所需 capabilities/preferences 集的 firefox 配置文件。

我正在使用量角器 1.6 中引入的 getMultiCapabilities()。到目前为止,我只需要一个自定义的 firefox 配置文件就可以了,这里是配置:

getMultiCapabilities: function() {
    var deferred = q.defer();

    var multiCapabilities = [
        {
            browserName: "chrome",
            specs: [
                "*.spec.js"
            ],
            exclude: [
                "footer.disabledJavascript.spec.js"
            ]
        }
    ];

    // Wait for a server to be ready or get capabilities asynchronously.
    setTimeout(function() {
        var firefoxProfile = new FirefoxProfile();
        firefoxProfile.setPreference("javascript.enabled", false);
        firefoxProfile.encoded(function (encodedProfile) {
            var capabilities = {
                "browserName": "firefox",
                "firefox_profile": encodedProfile,
                "specs": [
                    "footer.disabledJavascript.spec.js"
                ]
            };
            multiCapabilities.push(capabilities);
            deferred.resolve(multiCapabilities);
        });
    }, 1000);

    return deferred.promise;
},

问题: 现在我需要第二个带有 dom.storage.enabled = false 的 firefox 配置文件实例,但我不知道该如何解决此中的 deferred案例,因为现在有两个 encoded() 调用和两个功能添加到 multiCapabilities。

问题:如何使用getMultiCapabilities配置多个 firefox 配置文件?


我让它工作的唯一方法是将一个配置文件嵌套到另一个配置文件中并在最深层次上调用 resolve()(如果有两个配置文件可能没问题 - 但这个解决方案并没有真正扩展):

var multiCapabilities = [
        {
            browserName: "chrome",
            specs: [
                "*.spec.js"
            ],
            exclude: [
                "footer.disabledJavascript.spec.js",
                "disabledLocalStorage.spec.js"
            ]
        }
    ];

    // Wait for a server to be ready or get capabilities asynchronously.
    setTimeout(function() {
        // profile with disabled javascript
        var firefoxProfile = new FirefoxProfile();
        firefoxProfile.setPreference("javascript.enabled", false);
        firefoxProfile.encoded(function (encodedProfile) {
            var capabilities = {
                browserName: "firefox",
                directConnect: true,
                firefox_profile: encodedProfile,
                specs: [
                    "footer.disabledJavascript.spec.js"
                ]
            };
            multiCapabilities.push(capabilities);

            // profile with disabled local storage
            var newFirefoxProfile = new FirefoxProfile();
            newFirefoxProfile.setPreference("dom.storage.enabled", false);
            newFirefoxProfile.encoded(function (newEncodedProfile) {
                var newCapabilities = {
                    browserName: "firefox",
                    directConnect: true,
                    firefox_profile: newEncodedProfile,
                    specs: [
                        "disabledLocalStorage.spec.js"
                    ]
                };
                multiCapabilities.push(newCapabilities);
                deferred.resolve(multiCapabilities);
            });
        });
    }, 1000);

    return deferred.promise;
},

您可以使用 q.all 来执行此操作。本质上你想做这样的事情:

return q.all([
    capabilityPromise1,
    capabilityPromise2,
    ...
]);

具体如何获得能力承诺取决于您。 这是一种通用方法:

var makeFirefoxProfile = function(preferenceMap, capabilityMap) {
    var deferred = q.defer();
    var firefoxProfile = new FirefoxProfile();
    // TODO: iterate over preferenceMap and set preference for each
    firefoxProfile.encoded(function (encodedProfile) {
        var capabilities = {
            "browserName": "firefox",
            "firefox_profile": encodedProfile,
        };
        // TODO: iterate over capabilityMap and set key/value for each
        deferred.resolve(capabilities);
    });
    return deferred.promise;
};

getMultiCapabilities: function() {
    return q.all([
        makeFirefoxProfile({javascript.enabled: false}, {specs: ['spec1.js']})
        makeFirefoxProfile({dom.storage.enabled: false}, {specs: ['spec2.js']})
    ]);
}

如果您不想创建辅助函数并希望在 1 个函数中生成功能承诺,那由您决定。本质上,我认为这里的关键是使用 q.all,其余的实际上取决于您的功能对象的复杂程度以及您希望如何构建代码

按照@hakduan 的建议使用 q.all() 并具有可重复使用的功能,这里是对我有用的配置(注意它有多干净):

var makeFirefoxProfile = function(preferenceMap, specs) {
    var deferred = q.defer();
    var firefoxProfile = new FirefoxProfile();

    for (var key in preferenceMap) {
        firefoxProfile.setPreference(key, preferenceMap[key]);
    };

    firefoxProfile.encoded(function (encodedProfile) {
        var capabilities = {
            browserName: "firefox",
            directConnect: true,
            firefox_profile: encodedProfile,
            specs: specs
        };

        deferred.resolve(capabilities);
    });
    return deferred.promise;
};

exports.config = {
    getMultiCapabilities: function() {
        return q.all([
            {
                browserName: "chrome",
                directConnect: true,
                specs: [
                    "*.spec.js"
                ],
                exclude: [
                    "footer.disabledJavascript.spec.js",
                    "disabledLocalStorage.spec.js"
                ]
            },
            makeFirefoxProfile({"javascript.enabled": false}, ["footer.disabledJavascript.spec.js"]),
            makeFirefoxProfile({"dom.storage.enabled": false}, ["disabledLocalStorage.spec.js"])
        ]);
    },

    ...
};