0

我是 JUnit 的新手,正在尝试运行一些测试。我有以下代码。运行代码后,测试失败并显示 AssertionFailedError: null

  package com.seleniumsimplified.webdriver;
  import org.junit.Test;
  import org.openqa.selenium.WebDriver;
  import org.openqa.selenium.htmlunit.HtmlUnitDriver;
  import static junit.framework.Assert.assertTrue;

  public class FirstTest {

  @Test
  public void driverIsTheKing(){

      WebDriver driver = new HtmlUnitDriver(); 
      driver.get("http://www.compendiumdev.co.uk/selenium");

      assertTrue(driver.getTitle().startsWith("Selenium simplified") );
  }

}

这个测试总是在最后一行失败,堆栈跟踪如下

    junit.framework.AssertionFailedError: null
at junit.framework.Assert.fail(Assert.java:47)
at junit.framework.Assert.assertTrue(Assert.java:20)
at junit.framework.Assert.assertTrue(Assert.java:27)
at com.seleniumsimplified.webdriver.FirstTest.driverIsTheKing(FirstTest.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
4

1 回答 1

1
assertTrue(driver.getTitle().startsWith("Selenium simplified") );

似乎标题是别的东西。

尝试输出实际的标题,看看你得到了什么。

String title = driver.getTitle()
assertTrue(title, title.startsWith("Selenium simplified") );

(我希望有一个 assertStartWith 自动执行此操作,就像 assertEqual 一样)。

于 2013-07-18T11:02:43.563 回答