javascript - 将具有换行符的文本与另一个字符串匹配

标签 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

如何比较这些字符串?

最佳答案

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

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

关于javascript - 将具有换行符的文本与另一个字符串匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42502695/

相关文章:

javascript - 如何在 Javascript 中每隔几秒调用一个函数?

javascript - React Material onClick Collapse [答案已更新]

ios - 无法将新版本添加到 testflight

c# - 为什么我不能通过 Resharper Test Runner 单元测试从单元测试中删除 %Program Files% 中的文件?

javascript - 从一个数组中删除代表另一个数组的重复数据

javascript - Node.js:process.argv 与 process.ARGV

xcode - 使用 Xcode 在 iPod 上测试应用程序?

c++ - 如何比较两个不同大小的字符串

C++ 比较使用运算符的字符串

mysql 函数,去除非字母数字字符和前导和/或尾随 "the"