2

我正在使用ProtractorAstrolabe编写一些页面对象驱动的测试。

Jasmine 被用于实现describe/it样式规范。

添加自定义匹配器无法使用this.addMatchers( TypeError: Object #<Object> has no method 'toContainLowered'),因此我使用本指南来实现它们。

它似乎正在工作,直到我仔细查看测试运行的输出:

$> grunt test:func
Running "test:func" (test) task

Running "shell:protractor" (shell) task
Using the selenium server at http://localhost:4444/wd/hub
..

Finished in 6.727 seconds
2 tests, 1 assertion, 0 failures

这是我的代码:

var loginPage = require('./../pages/loginPage');

describe('Login page', function () {

    var ptor = loginPage.driver;

    beforeEach(function () {
        jasmine.Matchers.prototype.toContainLowered = function (expected) {
            return this.actual.toLowerCase().indexOf(expected) > -1;
        };
        loginPage.go();
        ptor.waitForAngular();
    });

    it('should display login page', function () {
        expect(loginPage.currentUrl).toEqual(ptor.baseUrl);
    });

    it('should display an error when the username or password is incorrect', function() {
        loginPage.login('bad', 'credentials');
        ptor.waitForAngular();
        expect(loginPage.lblError.getText()).toContainLowered('invalid username and/or password');
        // expect(loginPage.lblError.getText()).toContain('Invalid Username and/or Password');
    });

});

如果我取消注释最后一行并删除toContainLowered匹配器,我会得到正确的输出:

2 tests, 2 assertions, 0 failures

我在调试这个基于 Promise 的代码时遇到了非常困难的时间,并且任何努力都console.log(this.actual.toLowerCase().indexOf(expected) > -1);将打印出来false,这令人困惑。

我什至尝试用 just 替换整个函数定义return false;。仍然没有做任何事情。最后,我尝试不向匹配器传递任何参数,它应该抛出一个无效参数错误或其他东西。

使用 Protractor/Astrolabe 测试时,如何在 Jasmine 中定义自己的匹配器?

4

1 回答 1

2

我在匹配器上遇到过类似的问题,尤其是 .not 匹配器,它们似乎都不起作用。我假设量角器正在扩展 Jasmine 匹配器以处理承诺,并且该扩展尚未应用于 .not 或自定义匹配器。

就我而言,我想要一个 .not.toMatch,所以我只写了一个复杂的正则表达式,它给了我想要的东西,而不是嵌入在正则表达式中。

我注意到您的匹配器被称为“toContainLowered”,所以也许您正在寻找小写字母,因此您可以使用 .toMatch 使用正则表达式来执行此操作?

我在量角器 github 上提出的问题在这里:https ://github.com/angular/protractor/issues/266

我还看到,在这个代码文件中:https://github.com/angular/protractor/blob/master/jasminewd/spec/adapterSpec.js,最后一次提交被标记为“补丁匹配器不应该理解”。这可能会为您修复自定义匹配器,或者指示需要做什么来修复该自定义匹配器。

编辑:现在进一步研究该问题线程,我看到您已经去过那里。这让我的回答有些多余。:-)

于 2013-11-18T00:23:37.777 回答