我正在使用 Protractor 编写一些页面对象驱动的测试和 Astrolabe .
Jasmine 被用来实现 describe
/it
款式规范。
使用 this.addMatchers
添加自定义匹配器将不起作用(TypeError: Object #<Object> has no method 'toContainLowered'
),所以我用了this guide来实现它们。
它似乎有效,直到我仔细查看测试运行的输出:
$> 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
,这很令人困惑。
我什至尝试用 return false;
替换整个函数定义。这仍然没有做任何事情。最后,我尝试不向匹配器传递任何参数,这应该会引发无效参数错误或其他错误。
使用 Protractor/Astrolabe 测试时,如何在 Jasmine 中定义自己的匹配器?
最佳答案
我在匹配器方面也遇到过类似的问题,特别是在 .not 匹配器方面,这些问题似乎都不起作用。我假设 Protractor 正在扩展 Jasmine 匹配器来处理 promise ,并且该扩展尚未应用于 .not 或自定义匹配器。
就我而言,我想要一个 .not.toMatch,所以我只是编写了一个复杂的正则表达式,它给了我我想要的东西,但未嵌入正则表达式中。
我注意到您的匹配器称为“toContainLowered”,所以也许您正在寻找小写字母,因此您可以使用 .toMatch 通过正则表达式来执行此操作?
我在 Protractor github 上提出的问题在这里:https://github.com/angular/protractor/issues/266
我还看到,在这个代码文件中:https://github.com/angular/protractor/blob/master/jasminewd/spec/adapterSpec.js ,最后一次提交被标记为“修补的匹配器不应该理解”。这可能会为您修复自定义匹配器,或者提供修复该自定义匹配器需要执行的操作的指示。
编辑:现在进一步研究该问题线程,我发现您已经去过那里了。这使得我的回答有些多余。 :-)
关于jasmine - 自定义匹配器在 Astrolabe/Protractor + Jasmine 测试中未断言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19457121/