Eclipse - 无法在控制台中看到断言结果

时间:2017-02-01 11:17:16

标签: java eclipse selenium-webdriver junit automated-tests

我的Java非常基础,我对Selenium很新,我正在测试它是如何工作的。出于某些原因,在我的代码中,我可以看到"断言"在控制台的结果,而在另一部分我不能。我在断言之前和之后将消息记录到控制台(两者都显示)但断言结果没有。

package automationFramework;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import static org.junit.Assert.*;

public class Weather {

public static void main(String[] args) throws InterruptedException {

    System.setProperty("webdriver.gecko.driver", "//home//aaronh//Documents//Drivers//geckodriver");
    // System.setProperty("webdriver.gecko.driver",
    // "//home//aaron//JARs//geckodriver-v0.14.0-linux64//geckodriver");
    WebDriver driver = new FirefoxDriver();

    // launch browser and go to the website
    String url = "https://www.bbc.com/weather";
    driver.get(url);
    driver.manage().window().maximize();

    // search weather information for Bristol
    WebElement location = driver.findElement(By.name("search"));
    location.clear();
    location.sendKeys("Bristol, Bristol");

    // click search button
    WebElement search = driver.findElement(By.name("submitBtn"));
    search.click();

    // this assertion fails because it checks the title before the search and IS LOGGED TO THE CONSOLE
    // for
    // Bristol weather has finished

    // String bristol = driver.getTitle();
    // assertEquals("BBC Weather - Bristol", bristol);
    System.out.println("before wait");
    WebDriverWait wait = new WebDriverWait(driver, 5);
    if (wait.until(ExpectedConditions.titleContains("BBC Weather - Bristol"))) {
        String bristol = driver.getTitle();
        System.out.println("before assert " + bristol);
// this does not log to the console
        assertEquals("BBC Weather - Bristol", bristol);
        System.out.println("after assert");
    }

    driver.close();
    System.out.println("Test script executed successfully.");
    System.exit(0);

}

}

有人可以告诉我为什么断言输出在一个地方而不是另一个地方?我很欣赏getTitle是没有意义的,因为代码不会得到这些,除非标题是我们所声称的,但是嘿,我只是测试它。

感谢。

1 个答案:

答案 0 :(得分:2)

您必须区分控制台 JUnit 视图本身。

示例:

@Test
public void test() {
    System.out.println("1");
    Assert.assertEquals("A", "A");
    System.out.println("2");
    Assert.assertEquals("A", "B");
    System.out.println("3");
}

结果:

A)控制台输出

1
2

B)JUnit view输出

org.junit.ComparisonFailure: expected:<[A]> but was:<[B]>
at org.junit.Assert.assertEquals(Assert.java:115)
...

长话短说:按预期工作;更重要的是:断言传递不会创建任何可观察的输出!