简体   繁体   English

如何配置Spring STS运行Selenium测试类?

[英]How do I configure Spring STS to run Selenium test classes?

I'm using Spring STS (3.7) to develop an MVC application. 我正在使用Spring STS(3.7)开发MVC应用程序。 I'm attempting to run a simple Selenium test using an example I've read in a book. 我正在尝试使用我在书中阅读的示例来运行简单的Selenium测试。 I'm receiving a 'ClassNotFoundException' when I run the JUnit test. 运行JUnit测试时,我收到“ ClassNotFoundException”。 What's odd is that class that's not found is the test class itself: 'UIHomeTest'. 奇怪的是,找不到的类是测试类本身:'UIHomeTest'。 I've verified that I have JUnit, Hamcrest and Selenium on the classpath. 我已经验证我在类路径上有JUnit,Hamcrest和Selenium。 I've tried adding the JUnit library to the run configuration. 我尝试将JUnit库添加到运行配置中。 Here is the code for the test class: 这是测试类的代码:

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.openqa.selenium.firefox.FirefoxDriver;

import static org.junit.Assert.assertEquals;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.thoughtworks.selenium.SeleneseTestBase;

public class UIHomeTest { 公共类UIHomeTest {

private WebDriver browser;


private static final String HOME_URL = "http://localhost:8080/";

@Before
public void setUp() throws Exception {

    WebDriver driver = new FirefoxDriver();

}


@Test
public void testHomePage()
{

    browser.get(HOME_URL);

    assertEquals("Home", browser.findElement(By.id("title")).getAttribute("value"));
}

@After
public void tearDown() throws Exception
{
    browser.close();
}

}

Here is the error: 这是错误:

Class not found org.test.ui.UIHomeTest
java.lang.ClassNotFoundException: org.test.ui.UIHomeTest
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClass(RemoteTestRunner.java:685)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClasses(RemoteTestRunner.java:421)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:444)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

I've been able to run a non-Selenium JUnit test class inside of Spring STS before. 以前,我已经能够在Spring STS内运行非Selenium JUnit测试类。 Can anyone help me properly configure Spring STS to run Selenium test classes? 谁能帮助我正确配置Spring STS以运行Selenium测试类? Thanks. 谢谢。

On observing the imports and selenium code i found there are few not required imports used like import org.openqa.selenium.WebDriverBackedSelenium; 在观察导入和硒代码时,我发现很少使用不需要的导入,例如import org.openqa.selenium.WebDriverBackedSelenium; import com.thoughtworks.selenium.SeleneseTestBase; 导入com.thoughtworks.selenium.SeleneseTestBase; and also not used or initiated driver object. 也不使用或启动的驱动程序对象。 below is the complete code which works fine for me. 以下是适合我的完整代码。

com.tesngtraining; 培训 is my package. 是我的包裹。 If you are downloaded java specific selenium jars then please include all jars which are in different folders in build path or download standalone selenium jar file and give it in build path. 如果您下载了特定于Java的硒罐,请在构建路径中包含所有位于不同文件夹中的罐,或下载独立的硒罐文件并在构建路径中提供它。

 package com.tesngtraining;

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class TestingJunit {

private WebDriver browser;


private static final String HOME_URL = "http://localhost:8080/";

@Before
public void setUp() throws Exception {

    browser = new FirefoxDriver();

}


@Test
public void testHomePage()
{

    browser.get(HOME_URL);

    assertEquals("Home", browser.findElement(By.id("title")).getAttribute("value"));
}

@After
public void tearDown() throws Exception
{
    browser.close();
}

}

Thank you, Murali 谢谢Murali

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM