简体   繁体   中英

Eclipse - Can't see assert outcome in console

My Java is quite basic and I am very new to Selenium and I'm testing to see how it works. For some reason in my code, I can see the "assert" outcome in the console, whilst in another part I cannot. I've logged messages to the console before and after the assertation (both of which show) but the assertation result does not.

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);

}

}

Can someone please tell me why the assertation outputs in one place and not the other? I appreciate the getTitle is pointless because the code won't get these unless the title is what we're asserting but hey, I'm just testing it out.

Thanks.

You have to distinguish between the Console and the JUnit view itself.

Example:

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

results in:

A) console output

1
2

B) JUnit view output

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

Long story short: works as expected; and more importantly: asserts that pass do not create any observable output!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM