【问题标题】:Jasmine custom matcher with Protractor for checking browser title带有量角器的 Jasmine 自定义匹配器,用于检查浏览器标题
【发布时间】:2016-01-13 00:12:09
【问题描述】:

我正在编写一个 Jasmine 自定义匹配器以在量角器规范中使用,并且我想检查浏览器标题是否等于某个字符串。我无法让这段代码正常工作,在花了几个小时调试它之后,我只能假设 browser 对象没有像我预期的那样在匹配器函数中被访问。当我修改匹配器函数以接受 browse.getTitle() 作为实际参数时,它可以正常工作,这导致了我的假设。谁能在这里找到问题并向我解释一下?

beforeEach(function() {
    jasmine.addMatchers({
        toBeOnPage: function(util, customEqualityTesters) {
            return {
                compare: function(actual, expected) {
                    var result = {};
                    result.pass = actual.getTitle() === expected.title;
                    return result;
                }
            };
        }
    });
});

var homepage = { url: 'Homepage URL', title: 'Homepage Title' };

describe('regression:', function() {
    it('homepage loads successfully', function() {
        browser.get('http://localhost/#/home');
        expect(browser).toBeOnPage(homepage);
    });
});

【问题讨论】:

    标签: javascript testing jasmine protractor end-to-end


    【解决方案1】:

    问题在于getTitle() 返回了一个承诺。解决它:

    beforeEach(function() {
        jasmine.addMatchers({
            toBeOnPage: function(util, customEqualityTesters) {
                return {
                    compare: function(actual, expected) {
                        return {
                             pass: actual.getTitle().then(function (title) {
                                  return title === expected.title;
                             });
                        };
                    }
                };
            }
        });
    });
    

    【讨论】:

    • 谢谢。这是修复。
    猜你喜欢
    • 1970-01-01
    • 2015-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-15
    • 2022-08-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多