如何使用Protractor在弹出窗口中选择一个按钮

时间:2015-07-19 10:38:41

标签: javascript angularjs selenium selenium-webdriver protractor

当用户导航到该页面时,会出现一个小弹出窗口,询问用户“共享位置”,如下面的屏幕截图所示。 enter image description here

所以我想知道如何使用Protractor模拟用户点击“共享位置”按钮(显然,element(by.buttonText("Share Location")).click();不起作用)。

此屏幕截图中的浏览器也是Firefox,因为不同的浏览器可能有不同的消息/样式,我怎么能解决这个问题呢?

1 个答案:

答案 0 :(得分:0)

问题是这个按钮不在DOM中,你无法使用量角器/ selenium 来控制它。您需要首先避免它被打开,让浏览器知道如何自动响应。

this answer中应用解决方案(未经测试):

browser.executeScript("navigator.geolocation.getCurrentPosition = function(success) { success({coords: {latitude: 50.455755, longitude: 30.511565}}); }");

您也可以通过在FirefoxProfile preferences中将geo.prompt.testinggeo.prompt.testing.allow设置为true来解决此问题。带有makeFirefoxProfile()辅助函数的示例量角器配置:

var q = require("q");
var FirefoxProfile = require("firefox-profile");

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 = {
            firefox_profile: encodedProfile,
            specs: specs
        };

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

exports.config = {
    getMultiCapabilities: function() {
        return q.all([
            makeFirefoxProfile(
                {"geo.prompt.testing": true, "geo.prompt.testing.allow": true},
                ["specs/*.spec.js"]
            )
        ]);
    },

    // ...
}