Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Selenium WebDriver-第二部分-1)AssertEquals失败测试失败2)验证元素不存在_Java_Selenium_Selenium Webdriver_Junit_Webdriver - Fatal编程技术网

Java Selenium WebDriver-第二部分-1)AssertEquals失败测试失败2)验证元素不存在

Java Selenium WebDriver-第二部分-1)AssertEquals失败测试失败2)验证元素不存在,java,selenium,selenium-webdriver,junit,webdriver,Java,Selenium,Selenium Webdriver,Junit,Webdriver,我带着更愚蠢的问题回来了 1) 如果AssertEquals为假,如何使测试失败 我有这个密码- public boolean compareWidthPixels(By by, String expected) { System.out.println(driver.findElement(by).getCssValue("width")); try { assertEquals(expected, driver.findElement(by).getCssVa

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

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

我有这个密码-

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;

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

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

作为一个新手,我尝试了很多在Google和Stack Overflow中找到的东西- 等等,但没有一个奏效。我正在使用JUnit4,需要一个在元素不存在时应该传递的函数

谢谢

弥勒


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

答案1:要使用assert true或false,您应该使用if-else,然后通过assert调用函数,例如

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;
}
然后在测试中使用compareWidthPixels作为“
AssertTrue(compareWidthPixels(by,预期))

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

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

在断言中使用is元素。

回答1:要使用assert true或false,应该使用if-else,然后通过assert调用函数,例如

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;
}
然后在测试中使用compareWidthPixels作为“
AssertTrue(compareWidthPixels(by,预期))

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

private boolean isElementPresent(By by) {
    try {
        driver.findElement(by);
        return true;
    } catch (NoSuchElementException e) {
        return false;
    }
}
在断言中使用is元素。

添加
Assert.fail()在您的catch块中

例如:

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

} catch(Exception e) {
    Assert.fail();
}
希望这能解决您的问题

Add
Assert.fail()在您的catch块中

例如:

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

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

希望这能解决你的问题

看看下面的答案。至于为什么会发生这种情况,您正在捕获异常-因此测试运行者不会意识到有一个异常被提出,从而相信它通过了。有几种断言方法,包括assertTrue和assertFalse:请看下面的答案。至于为什么会发生这种情况,您正在捕获异常-因此测试运行者不会意识到有一个异常被提出,从而相信它通过了。有几种断言方法,包括assertTrue和assertFalse:检查元素是否显示不是更好吗???类似于-boolean visible=driver.findElement(By.xpath(locator)).isDisplayed()@弥勒佛,布尔可见=driver.findElement(By.xpath(locator)).isDisplayed();也是检查元素可用性的一种方法。@Prashant,在testcase中使用Assert语句是一种好方法吗?或者我们可以在函数中使用assert语句。你能告诉我哪一个是最佳实践吗?检查元素是否显示不是更好吗???类似于-boolean visible=driver.findElement(By.xpath(locator)).isDisplayed()@弥勒佛,布尔可见=driver.findElement(By.xpath(locator)).isDisplayed();也是检查元素可用性的一种方法。@Prashant,在testcase中使用Assert语句是一种好方法吗?或者我们可以在函数中使用assert语句。你能告诉我哪一个是最佳实践吗。