代码之家  ›  专栏  ›  技术社区  ›  Nital

如何使用HtmlUnit驱动程序和Spring Boot运行Selenium自动化测试?

  •  0
  • Nital  · 技术社区  · 6 年前

    无法配置和运行 Selenium 配置后的测试 HtmlUnit Spring Boot

    执行测试时出错。

    public abstract class BaseSeleniumTests {
    
        protected static String BASE_URL = "";
        private static final String URL_PROPERTIES = "url.properties";
        protected WebDriver driver;
    
        @Before
        public void setUp() {
            //load test properties
            Properties p = loadAppTestProperties();
            BASE_URL = p.getProperty("base.url");
            System.out.println("BASE_URL = " + BASE_URL);
    
            //load driver
            this.driver = new HtmlUnitDriver();
        }
    
        private Properties loadAppTestProperties() {
            ClassLoader classLoader = getClass().getClassLoader();
            String filePath = classLoader.getResource(URL_PROPERTIES).getFile();
            File f = new File(filePath);
            try {
                Properties p = new Properties();
                p.load(new FileReader(f));
                return p;
            } catch (IOException e) {
                e.printStackTrace();
                return null;
            }
        }
    
        @After
        public void tearDown() {
            if (driver != null) {
                driver.close();
                driver.quit();
            }
        }
    
    }
    

    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class HomePageTest extends BaseSeleniumTests {
    
        @Test
        public void testHomePage() {
            this.driver.get(BASE_URL);
            String htmlSource = this.driver.getPageSource();
            assertTrue(htmlSource.contains("US Router"));
            assertTrue(htmlSource.contains("Welcome to JPMorgan Chase"));
        }
    
    }
    

    pom.xml文件

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-htmlunit-driver</artifactId>
            <version>2.52.0</version>
            <scope>test</scope>
        </dependency>
    

    错误:

    java.lang.NoClassDefFoundError: org/openqa/selenium/remote/SessionNotFoundException
    
        at com.jpmchase.gct.ft.BaseSeleniumTests.loadHtmlUnitDriver(BaseSeleniumTests.java:71)
        at com.jpmchase.gct.ft.BaseSeleniumTests.setUp(BaseSeleniumTests.java:33)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
        at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
        at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
        at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
        at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
        at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
        at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:251)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
        at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
        at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
        at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
        at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
        at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
        at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
        at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
    Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.remote.SessionNotFoundException
        at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 31 more
    
    0 回复  |  直到 6 年前