【问题标题】:How to Write Assert method in Selenium once and use multiple times to verify text?如何在 Selenium 中编写一次 Assert 方法并多次使用来验证文本?
【发布时间】:2021-12-31 08:09:46
【问题描述】:

如何在下面编写 Assert 方法来验证文本/消息一次,以便我可以在 Selenium 的多个测试中使用它。

String expectedMessage = "Name: Z to A";
String actualMessage = getTextFromElement(By.xpath("//option[contains(text(),'Name: Z to A')]"));
Assert.assertEquals("Error, message not displayed", expectedMessage, actualMessage);

【问题讨论】:

    标签: selenium selenium-webdriver selenium-chromedriver assert


    【解决方案1】:

    你可以做一个这样的util方法

    public void verifyText(String actualMessage, String expectedMessage, String description) {
        Assert.assertEquals(actualMessage, expectedMessage, description);
    }
    

    并在您的项目中随时调用它。像这样

    verifyText(actualMessage, expectedMessage, "Error, message not displayed");
    

    此外,如果您不创建 util 类的对象,您可以将此方法设为静态。

    如果您正在创建一个对象,那么您可能可以使用对象引用来调用此方法。

    【讨论】:

      猜你喜欢
      • 2018-03-05
      • 2013-01-31
      • 1970-01-01
      • 1970-01-01
      • 2020-01-07
      • 2016-10-31
      • 1970-01-01
      • 1970-01-01
      • 2017-05-05
      相关资源
      最近更新 更多