【问题标题】:Selenium WebDriver - how to use AssertionsSelenium WebDriver - 如何使用断言
【发布时间】:2014-12-08 19:57:43
【问题描述】:

这是我在这里的第一个问题,我想学习 Selenuim。我正在尝试在我的脚本中添加断言以验证页面上的文本是否可用。该代码仅适用于第一个断言,而第二个则失败。这是用户断言的正确方法吗?谢谢!

public void verifyCampaignStatusDropdownMenu() throws InterruptedException {

    driver.findElement(By.linkText("TextOne"));
    String wipText = "TextOne";
    Assert.assertTrue("TextOne".equals(wipText), "TextOne text is available");

    driver.findElement(By.linkText("TextTwo"));
    String tempText = "TextTwo";
    Assert.assertTrue("Template".equals(tempText), "TextTwo text is available" );
}

【问题讨论】:

  • 提供你得到的异常
  • 这个问题与 Selenium 无关。您应该阅读一些 JUnit 文档。
  • @SiKing。我想这个问题与硒有关。我相信OP的要求是验证webelements的文字
  • @Saifur OP 显然需要先了解 JUnit,然后才能开始将 Selenium 混入混乱中。

标签: java junit


【解决方案1】:

使用多个断言不是最佳实践。最佳实践是每个测试用例有一个断言。如果由于任何原因第一个断言失败,则没有第二个断言的价值。见this。您应该将它们分解为两种不同的方法并执行测试。

而且,您的断言对我来说没有任何意义,因为您将相同变量的内容与父级而不是 webelement 进行比较 你的方法应该如下所示

public void verifyCampaignStatusDropdownMenuOne() throws InterruptedException {

    String wipText = driver.findElement(By.linkText("TextOne")).getText() ;
    Assert.assertTrue("TextOne text is available", "TextOne".equals(wipText));
}

public void verifyCampaignStatusDropdownMenuTwo() throws InterruptedException {

    String tempText = driver.findElement(By.linkText("TextTwo")).getText() ;
    Assert.assertTrue("TextTwo text is available", "Template".equals(tempText));
}

使用 Java

【讨论】:

    猜你喜欢
    • 2016-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-21
    • 2016-07-16
    • 2011-03-18
    • 2014-06-14
    • 2017-05-26
    相关资源
    最近更新 更多