我的 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 毫无意义,因为除非标题是我们所断言的,否则代码不会获取这些内容,但是嘿,我只是在测试它。
谢谢。
最佳答案
您必须区分控制台和JUnit View 本身。
示例:
@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)
...
长话短说:按预期工作;更重要的是:通过的断言不会创建任何可观察的输出!
关于java - Eclipse - 在控制台中看不到断言结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41978443/