在 Protractor 预期条件下使用 'or'

Using 'or' in Protractor expected conditions

我正在尝试在使用 or expected condition 的 Protractor 中编写 expects 语句。但是,文档没有任何示例说明如何将 'or' 或 'and' 与这些块一起使用。

我试过

expect(myString).or.toEqual('a string').toEqual('another string')

expect(myString).toEqual('a string').or.toEqual('another string')

只是为了好玩

expect(myString).toEqual('a string', 'another string')

有人问过这个问题previously,但只给出了解决方法。

我想实际使用 Protractor 中内置的 or 函数,因为它应该允许消息读取类似

的内容
Expected 'a totally different string' to be 'a string' or 'another string'

您始终可以编写自定义匹配器。在这种情况下,无需编写一个, 中提供的那个将适合您的用例。对于茉莉花 1.x:

this.addMatchers({
    toBeIn: function(expected) {
        var posibilities = Array.isArray(expected) ? expected : [expected];
        return posibilities.indexOf(this.actual) > -1;
    }
});

用法:

expect(myString).toBeIn(["a string", "another string"]);

仅供参考,还有一个有用的自定义 jasmine 匹配器库:jasmine-matchers

此处提供示例:http://angular.github.io/protractor/#/api?view=ExpectedConditions。但是,您混淆了预期条件的使用,它用于 browser.wait 而不是 expects.

这有点坑爹,但对我很有帮助。我发布它是为了防止它对某人有用,而不是因为我认为它是最优雅的方法(它也可能是 "workaround")。如果您的字符串真的不同,它会起作用,并且它适用于字符串,而不是更普遍。对于我的情况,我必须先解决承诺。

myString.getText().then(function(text){
                  expect('a stringanother string'.search(text)).toBeGreaterThan(-1);
                });

当然上面会很乐意通过像'n'或'another'这样的部分匹配或者像'ngano'这样的废话。 None 我的情况会发生。