【发布时间】:2017-06-18 02:54:27
【问题描述】:
我的 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 毫无意义,因为除非标题是我们所断言的,否则代码不会得到这些,但是嘿,我只是在测试它。
谢谢。
【问题讨论】:
-
不是断言();方法设置为抛出异常而不是使用类似 system.out.print() 的操作直接登录到控制台?无论如何,如果断言在 assertEuals() 点上失败了,我相信你根本不应该看到“断言之后”的句子吗?无论如何,为什么不把你在 main() 中的东西放到 @Test-method 中呢? :)
-
啊,如果断言失败,它将输出到控制台(我将其更改为与“BBC - 天气 - 布里斯托尔”进行比较)并显示。
标签: java eclipse selenium-webdriver junit automated-tests