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

时间:2015-02-19 23:47:06

标签: angularjs firefox testing protractor end-to-end

关注Configuring multiple capabilities with promises主题。

使用案例:我有两个单独的测试,要求在 javascript禁用本地存储禁用的情况下触发Firefox。这意味着我需要两个具有javascript.enabled = falsedom.storage.enabled = false所需功能/首选项的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;
},

2 个答案:

答案 0 :(得分:3)

您可以使用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函数中生成函数promise,那取决于您。从本质上讲,我认为这里的关键是使用q.all,其余部分实际上取决于您的功能对象的复杂程度以及您希望如何构建代码

答案 1 :(得分:1)

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

    ...
};