【问题标题】:How to select a button on popup using Protractor如何使用量角器在弹出窗口上选择一个按钮
【发布时间】:2015-07-19 10:38:41
【问题描述】:

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

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

此外,此屏幕截图中的浏览器是 Firefox,由于不同的浏览器可能有不同的消息/样式,我该如何解决这个问题?

【问题讨论】:

    标签: javascript angularjs selenium selenium-webdriver protractor


    【解决方案1】:

    问题是这个按钮不在 DOM 中,你不能用 protractor/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"]
                )
            ]);
        },
    
        // ...
    }
    

    【讨论】:

    • 我没有收到require("q")。你用过requirejs什么的吗?与运行量角器配置时一样,它会抛出“错误:找不到模块'q'”
    • 我也试过“browser.executeScript”解决方案,但还是不行。
    猜你喜欢
    • 2017-09-28
    • 1970-01-01
    • 2016-07-19
    • 1970-01-01
    • 1970-01-01
    • 2016-07-15
    • 2022-05-23
    • 2012-02-17
    • 1970-01-01
    相关资源
    最近更新 更多