【问题标题】:How to exit a testcase in protractor if element contains value如果元素包含值,如何在量角器中退出测试用例
【发布时间】:2015-02-16 15:46:48
【问题描述】:

我一直在用 Protractor 编写一套测试,并且快完成了。我很难弄清楚如何做一些相当普遍的事情。我希望能够检查文本框是否包含值,如果是,则以失败退出测试用例。 (如果文本框包含一个值,我知道测试用例无论如何都不可能通过)

我目前正在尝试这样的事情:

    tasksPage.generalInformationDateOfBirthTextbox.getAttribute('value').then(function(attr){
        //attr contains the correct value in the textbox here but how do I return it to parent scope so I can exit the test case?
        console.log("attr length is " + attr.length);
        expect(attr.length).toBe(0);
    },function(err){
        console.log("An error was caught while evaluating Date of Birth text value: " + err);
    });

expect 语句失败,正如我所期望的那样,但测试用例继续运行,这似乎是 expect 的预期行为。所以我尝试从“then”块中返回一个真/假,但我似乎无法弄清楚如何将该值返回到父范围以做出决定。换句话说,如果我将以上内容更改为:

    var trueFalse = tasksPage.generalInformationDateOfBirthTextbox.getAttribute('value').then(function(attr){
        if(attr === ""){
            return true;
        }else{
            return false;
        }
    },function(err){
        console.log("An error was caught while evaluating Date of Birth text value: " + err);
    });

    //This is the 'it' block's scope
    if(trueFalse == true){
        return;
    }

我知道我对承诺的缺乏经验可能是造成这个麻烦的罪魁祸首。基本上,我只想说,'如果某某文本框包含文本,则停止测试用例失败'。

谢谢,

【问题讨论】:

    标签: angularjs node.js testing protractor angular-promise


    【解决方案1】:

    这不是protractor 问题,而是测试框架问题(jasminemocha 等)。

    不过,protractor 的问题跟踪器存在问题:

    jasmine 问题跟踪器中引用了一个打开的问题

    虽然这没有实现,但有一个变通方法(最初提到here):

    使用npm安装模块后,当你希望你的测试失败并退出时,添加:

    jasmine.getEnv().bailFast();
    

    (别忘了加载依赖require('jasmine-bail-fast');)


    另见:

    【讨论】:

      【解决方案2】:

      上面代码的问题是你试图比较一个“承诺”和一个“价值”。

      trueFalse.then(函数(值) { 如果(值) …… });

      或通过期望语句进行比较。像expect(trueFalse).toBe(false);

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-07-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-12-28
        • 1970-01-01
        相关资源
        最近更新 更多