Multiple firefox profiles in protractor

半世苍凉 提交于 2019-12-08 19:56:26

You can use q.all to do this. Essentially you want to do something like this:

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

Exactly how you get the capability promises is up to you. Here's one generic way:

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']})
    ]);
}

If you don't want to create a helper function and want to generate the capability promises in 1 function, that's up to you. Essentially, I think the key here is to use q.all, the rest really depends on how complex your capabilities objects are and how you want to structure your code

Following @hakduan's suggestion to use q.all() and have a reusable function, here is the configuration that worked for me (note how clean it is):

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"])
        ]);
    },

    ...
};
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!