2017-02-01 12 views
1

Javaはかなり基本的なもので、私はSeleniumの新機能です。何らかの理由で私のコードで、私はコンソールで「アサート」の結果を見ることができますが、他の部分ではそうすることはできません。私はアサーションの前後でコンソールにメッセージを記録しましたが(どちらもどちらも表示されますが)、アサーションの結果は表示されません。Eclipse - コンソールでアサーションの結果を見ることができません

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

} 

} 

誰かが、アサーションが1つの場所に出力され、他の場所に出力されない理由を教えてください。 getTitleは意味がありません。なぜなら、タイトルが私たちが主張しているものでない限り、これらのコードを取得できないからです。ちょっと試してみました。

ありがとうございました。

+0

アサート<>()ではありません。メソッドは例外をスローするように設定され、system.out.print()のような操作を使用してコンソールに直接ログしませんか?とにかく、assertEuals()の時点でアサーションが失敗した場合は、私が信じるすべての "assert after"文を見てはいけませんか?とにかくmain()にあるものを@ Test-methodに入れてみましょう。 :) – vegaasen

+1

ああ、アサートが失敗した場合、コンソールに出力されます(私は「BBC - 天気 - Bristoll」と比較するように変更しました)。 – Rookie

答えて

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

かいつまん:期待通りに動作します。より重要なのは:パスが観察可能な出力を生成しないと主張することです!

関連する問題