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

问题描述 投票:0回答:2

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

弹出屏幕

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

如何比较这些字符串?

javascript testing string-comparison
2个回答
4
投票

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

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

0
投票

规范行尾和修剪内容对我确实有用:

  const normalizeContent = (content: string) => content.replace(/\r\n/g, "\n").trim();

  expect(normalizeContent(fileContent)).toEqual(normalizeContent(expected));

一如既往,享受。

© www.soinside.com 2019 - 2024. All rights reserved.