【问题标题】:Selenium Assert Equals to Value1 or Value2Selenium 断言等于 Value1 或 Value2
【发布时间】:2019-01-29 15:27:38
【问题描述】:

我想在 Selenium 中进行测试(Java、Testng) 如果我的实际值等于两个值之一,因为我的值可以是 Value1 或 Value2 并且两个值都是正确的。

如果我只想断言一个相等,我会使用这种构造:

String expectedPopUpV1 = "Value1";
String expectedPopUpV2 = "Value2";
String actualPopUp = driver.findElement(By.cssSelector(Value)).getText();
Assert.assertEquals(actualPopUp,expectedPopUp);

但是如果我想做类似的东西我应该怎么做

Assert.assertEquals(actualPopUp,expectedPopUp1||expectedPopUp1);

【问题讨论】:

    标签: java selenium selenium-webdriver intellij-idea testng


    【解决方案1】:

    您可以使用assertTrue(boolean condition, String message) 并在消息中提供详细信息

    boolean isEqual = actualPopUp.equals(expectedPopUpV1) || actualPopUp.equals(expectedPopUpV2);
    Assert.assertTrue(isEqual, "The text " + actualPopUp + " is not " + expectedPopUpV1 + " or " + expectedPopUpV2);
    

    【讨论】:

      【解决方案2】:

      下面的选项也应该使用三元运算符:

      Assert.assertEquals(expectedPopUpV1.equalsIgnoreCase(actualPopUp )?expectedPopUpV1:expectedPopUpV2 , actualPopUp );
      

      【讨论】:

        【解决方案3】:

        没有任何这样的方法可用。我能想到的两个最接近的选择:

        if(!actualPopUp.equals(expectedPopUp1) || !actualPopUp.equals(expectedPopUp2) {
            Assert.fail("insert failure messsage here"); 
        }
        

        if(actualPopUp.equals(expectedPopUp1) || actualPopUp.equals(expectedPopUp2) {
            Assert.assertTrue("insert message");
        }
        

        另一种选择是通过构建您自己的 Builder 模式来真正扩展 Assert 功能,以适应:

        AssertBuilder.with(actualPopUp).equals(expectedPopUp1).or(expectedPopUp2);
        

        但这对于简单的用例来说可能太复杂了。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-03-07
          • 2020-01-17
          • 2011-02-24
          • 1970-01-01
          • 2023-04-05
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多