【问题标题】:Matching a text having line breaks with another String将具有换行符的文本与另一个字符串匹配
【发布时间】:2017-02-28 07:31:10
【问题描述】:

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

弹出屏幕

测试代码

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");

电流输出(失败)

如何比较这些字符串?

【问题讨论】:

  • 你正在使用某种js框架,将其添加为相对标签
  • === 使用这个运算符

标签: javascript testing string-comparison


【解决方案1】:

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

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"
)

【讨论】:

  • 它说 .replace 不是函数
  • 我没有意识到getText 返回了一个 Promise。让我编辑我的答案,等一下……
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-17
  • 2013-05-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多