【问题标题】:Assert in Selenium在硒中断言
【发布时间】:2017-07-05 07:20:45
【问题描述】:

我正在尝试断言两个字符串。但即使字符串相同,测试用例也会失败。以下是我写的:

String actualmsg="My expected string";
String errormsg = driver.findelement(By.xpath("xpath of the element")).getText();
Assert.assertEquals(actualmsg, errormsg);

当我在控制台上打印 errormsg 时,它会给出与actualmsg 相同的字符串。请帮忙。

【问题讨论】:

  • 你确定完全一样吗?每个都没有尾随或前导空格? “我的预期字符串”和“我的预期字符串”不一样。如“我的预期字符串”和“我的预期字符串”
  • 我认为断言不是 Selenium 而是 JUnit 或 TestNG 或其他测试框架(您可能想指定您使用的是哪一个)。无论如何,如果您确定字符串相等,则可能会有一些您在控制台中看不到的前导或尾随空格。试试Assert.assertEquals(actualmsg.trim(), errormsg.trim());
  • 您能否分享日志/调试消息以确定两个字符串是否相同。还要分享“Assert”类所属框架的详细信息。

标签: java selenium-webdriver assert string-comparison


【解决方案1】:

使用 Hamcrest 断言比较你会得到异常不匹配的结果。

你使用:

assertThat(actualmsg, is(errormsg));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-01
    • 2016-01-11
    • 1970-01-01
    • 2021-12-22
    • 2018-12-23
    相关资源
    最近更新 更多