java Selenium WebDriver - 两部分 - 1) 对 AssertEquals 失败的失败测试 2) 验证元素不存在

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11612541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 05:46:51  来源:igfitidea点击:

Selenium WebDriver - two part - 1) Fail test on AssertEquals failure 2)Verify Element not present

javaseleniumselenium-webdriverjunitwebdriver

提问by some_other_guy

I am back with more silly questions

我带着更多愚蠢的问题回来了

1) How to fail a test if AssertEquals is false?

1) 如果 AssertEquals 为假,如何使测试失败?

I have this code -

我有这个代码 -

public boolean compareWidthPixels(By by, String expected) {
    System.out.println(driver.findElement(by).getCssValue("width"));
    try {
        assertEquals(expected, driver.findElement(by).getCssValue("width"));
        System.out.println("Width as expected");
        return true;

    } catch (Error e) {
        verificationErrors.append(e.toString());
        System.out.println("Width incorrect");
        return false;

    }

This code displays "Width incorrect" when the width does not match the expected value but the test case passes. I want the test to fail if the width are unequal.

当宽度与预期值不匹配但测试用例通过时,此代码显示“宽度不正确”。如果宽度不相等,我希望测试失败。

2) How to assert/verify an Element is NOT present?

2) 如何断言/验证元素不存在?

As a novice I tried many things I found in Google and here in Stack Overflow - Assert that a WebElement is not present using Selenium WebDriver with java, Selenium WebDriver - Test if element is present, etc. But none of them worked. I am working with JUnit4 and need a function that should pass if the element is not present.

作为新手,我尝试了很多我在 Google 和 Stack Overflow 中找到的东西 - 使用 Selenium WebDriver 和 java 断言 WebElement 不存在Selenium WebDriver - 测试元素是否存在等。但它们都不起作用。我正在使用 JUnit4 并且需要一个函数,如果元素不存在,该函数应该通过。

Thanks

谢谢

Maitreya

弥勒佛

P.S: Please feel free edit this question if it looks confusing or disoriented.

PS:如果这个问题看起来令人困惑或迷失方向,请随时编辑。

回答by Prashant Shukla

Answer 1: To use assert true or false you should use if else instead and then call the function by assert e.g.

答案 1:要使用 assert true 或 false 你应该使用 if else ,然后通过 assert eg 调用函数

public boolean compareWidthPixels(By by, String expected) {
    System.out.println(driver.findElement(by).getCssValue("width"));
    if (driver.findElement(by).getCssValue("width").equals(expected)){
        System.out.println("Width as expected");
        return true;
    } 
    System.out.println("Width incorrect");
    return false;
}

Then use compareWidthPixels as 'AssertTrue(compareWidthPixels(by, expected))' in your test

然后AssertTrue(compareWidthPixels(by, expected))在测试中使用 compareWidthPixels 作为 ' '

Similarly for 2nd question you can use following

同样对于第二个问题,您可以使用以下内容

private boolean isElementPresent(By by) {
    try {
        driver.findElement(by);
        return true;
    } catch (NoSuchElementException e) {
        return false;
    }
}

using is element in assertions.

在断言中使用是元素。

回答by user1846708

Add Assert.fail();in your catch block.

添加Assert.fail();您的 catch 块。

Example:

例子:

try {
    ----- your code -----

} catch(Exception e) {
    Assert.fail();
}

Hope this will solve your problem

希望这能解决你的问题