匹配具有换行符的文本与另一个String

时间:2017-02-28 07:31:11

标签: javascript testing string-comparison

我试图从弹出消息中获取文本,然后将其与另一个字符串进行比较,以检查两者是否相同。

弹出屏幕

enter image description here

测试代码

var text = element(by.css('.modal.fade.AppLockPopup.in'));
//expect(text.getText()).toEqual("Warning" + "\n" + " You haven't saved your changes.Are "+ "\n" +" you sure you want to discard your changes? "+ "\n" +" Yes No");
expect(text.getText()).toEqual("Warning You haven't saved your changes.Are you sure you want to discard your changes? Yes No");

电流输出(失败)

enter image description here

如何比较这些字符串?

1 个答案:

答案 0 :(得分:2)

由于您遇到空格和换行问题,我建议使用正则表达式将所有空白序列规范化为单个常规空间,然后再使用您的测试框架比较这两个值:

var text = element(by.css('.modal.fade.AppLockPopup.in')).getText().then(function (e) {
  return e.replace(/\s+/g, ' ')
})

expect(text).toEqual(
  "Warning You haven't saved your changes. Are you sure you want to discard your changes? Yes No"
)