使用selenium测试执行时断言问题

时间:2014-08-11 19:40:16

标签: java selenium junit webdriver

我有以下课程:

import static org.junit.Assert.*;

import org.openqa.selenium.WebElement;

 public class ClasseTeste extends Navegadores {

  public static void verificarTitulo() {
     abrirChrome();
     String titulo = driver.getTitle();
     assertTrue(titulo.contains("google"));
     fecharNavegador(); 
  }

}

当我执行主方法时

public static void main( String[] args )
{
     verificarTitulo();     
}

发生此异常:

Exception in thread "main" java.lang.NoClassDefFoundError: org/junit/Assert
at test.NovoProjeto.ClasseTeste.verificarTitulo(ClasseTeste.java:11)
at test.NovoProjeto.Main.main(Main.java:8)
Caused by: java.lang.ClassNotFoundException: org.junit.Assert
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 2 more

我正在尝试为selenium webdriver创建一些简单的测试。

2 个答案:

答案 0 :(得分:0)

此错误

Caused by: java.lang.ClassNotFoundException: org.junit.Assert

表示在运行测试时需要junit.jar在您的类路径中。

答案 1 :(得分:0)

当您使用maven作为构建管理器时,在POM.xml文件中添加以下依赖项:

             <dependency>
                 <groupId>org.seleniumhq.selenium</groupId>
                 <artifactId>selenium-java</artifactId>
                 <version>2.41.0</version>
             </dependency>

             <dependency>
                 <groupId>org.seleniumhq.selenium</groupId>
                 <artifactId>selenium-support</artifactId>
                 <version>2.41.0</version>
             </dependency>



             <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.10</version>
                <scope>test</scope>
            </dependency>

在代码中,您的导入将如下所示:

import junit.framework.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.PageFactory;

import org.openqa.selenium.WebElement;

希望这适合你。