angularjs - 在 Protractor 中的 Angular $超时期间获取元素的文本

标签 angularjs protractor angularjs-e2e

注意:为应用程序使用 MEAN - Mocha、Express、Angular 和 Node

我正在学习如何进行 e2e 测试,并构建了一个小应用程序来进行测试。它所做的一件事是,每当您执行某项操作时,它都会显示一条消息 X 秒,然后消失。我正在使用 Angular $timeout。

$scope.displayMessage = function(messageIn, borderColor){

        var timeoutTime = 5000; //clear the message after x seconds
        $scope.borderType = "1px solid " + borderColor;

        $scope.messages = messageIn;

        $scope.visibility = true; //reveal the div
        $timeout.cancel($scope.timeout); //cancel any previous timers

        $scope.timeout = $timeout(function(){ //Set and start new timer.

            $scope.visibility = false;
            $scope.messages = "";
        }, timeoutTime);
    };

现在我正在尝试测试这一点。目前我有

 it("should display message", function(done){

        var button = element(by.id("getStudents"));

        console.log("clicking button");

        var messageElem = element(by.binding("messages"));

        button.click().then(function () {

            console.log("button clicked");

            messageElem.getText().then(function(text){

               console.log("The text should equal: " + text);
            });

            //expect(messageElem.getText()).toBe("Students retrieved");
            console.log("test executed");

        });

        done();
    });

但它似乎要等到超时后才执行 messageElem.getText().then() 此时 div 已被清除。在超时期间将打印“测试已执行”日志,但不会打印“getText()”函数。我猜这与 Angular 生命周期有关?

哦,这是 HTML(Jade 格式)

 div(ng-model="messages",ng-show = "visibility", style = "clear:both;padding-top:3em;border:{{borderType}};text-align:center;")

    h3
        {{messages}}

忽略我的风格,我懒得为这个测试应用程序制作 css 文件:D

最佳答案

尝试使用 $interval 而不是 $timeout。

Protractor attempts to wait until the page is completely loaded before performing any action (such as finding an element or sending a command to an element).

阅读here 。详情here .

关于angularjs - 在 Protractor 中的 Angular $超时期间获取元素的文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24041920/

相关文章:

angularjs - 用于检查元素列表的 Angular e2e 测试

javascript - 使用 Selenium/Protractor Javascript 切换到新窗口

javascript - 无法在 ng-click 上获取 href 内容

javascript - 使用 AngularJS 禁用/启用 html 元素

gulp - 无法使用 gulp 运行 Protractor (ECONNREFUSED 连接 ECONNREFUSED)

javascript - Protractor browser.getCurrentUrl() 不适用于路线更改

javascript - 如何在 AngularJS e2e 测试中测试拖放功能

javascript - 在 Angular 中重构 $rootScope.$apply

angularjs - 使用单个 Gulp 任务运行 Protractor

angularjs - Protractor 从 dom 中移除元素