gpt4 book ai didi

java - Selenium 简单 Chrome WebDriver : java compilation and execution problems

转载 作者:行者123 更新时间:2023-12-01 11:26:00 24 4
gpt4 key购买 nike

我是Selenium新手,对java编译和运行过程知之甚少,因为我总是借助IDE来编译和执行。

我的问题

我正在尝试使用 selenium 运行一个简单的测试(出于学习目的)来请求“www.google.com”并断言标题是正确的。
我的目标是学习足够的知识,以便在工作中使用它,并减少在多个浏览器和操作系统上进行手动测试的时间。

我的问题是,当我想从命令行运行测试时,它失败并显示 java.lang.IllegalArgumentException: Could not find class

我在 SO 中看到了很多类似的问题,但没有一个对我有帮助......

我做了什么

我执行了以下步骤:

  1. 关注此https://code.google.com/p/selenium/wiki/Grid2并启动hub+node
  2. 下载 chromedriver 并编写这个简单的测试类

    import junit.framework.TestCase;
    import org.junit.*;
    import org.junit.runner.RunWith;
    import org.junit.runners.JUnit4;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriverService;
    import org.openqa.selenium.remote.DesiredCapabilities;
    import org.openqa.selenium.remote.RemoteWebDriver;

    import java.io.File;
    import java.io.IOException;

    @RunWith(JUnit4.class)
    public class ChromeTest extends TestCase {

    private static ChromeDriverService service;
    private WebDriver driver;

    @BeforeClass
    public static void createAndStartService() {
    service = new ChromeDriverService.Builder()
    .usingDriverExecutable(new File("tools/chromedriver"))
    .usingAnyFreePort()
    .build();
    try {
    service.start();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    @AfterClass
    public static void createAndStopService() {
    service.stop();
    }

    @Before
    public void createDriver() {
    driver = new RemoteWebDriver(service.getUrl(),
    DesiredCapabilities.chrome());
    }

    @After
    public void quitDriver() {
    driver.quit();
    }

    @Test
    public void testTitleDevel() {
    driver.get("http://www.google.com");
    TestCase.assertEquals("Google", driver.getTitle());
    }
    }
  3. 使用maven并创建pom.xml

        <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>Test</groupId>
    <artifactId>Test</artifactId>
    <version>1.0</version>
    <dependencies>
    <dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>2.46.0</version>
    </dependency>

    <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    </dependency>
    <dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest-core</artifactId>
    <version>1.3</version>
    <scope>compile</scope>
    </dependency>
    <dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>12.0</version>
    </dependency>
    </dependencies>

    <build>
    <sourceDirectory>/Users/user1/QAScripts/AmateurTest/src/</sourceDirectory>


    <plugins>
    <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.18.1</version>
    </plugin>
    </plugins>
    </build>
    </project>

我现在有以下目录结构:

测试
|--pom.xml
|--src
|------ChromeTest.java
|--工具
|------chromedriver
|------selenium-server-standalone-2.46.0.jar

  • 运行mvn dependency:build-classpath
  • 使用该类路径编译 ChromeTest.java
  • javac -cp "..HERE GOES CLASSPATH.." src/ChromeTest.java

    user1@tororrosso ~/T/src> ls -la
    total 16
    drwxrwxrwx 4 user1 staff 136 Jun 12 16:43 ./
    drwxrwxrwx 8 user1 staff 272 Jun 12 16:41 ../
    -rw-r--r-- 1 user1 staff 2316 Jun 12 16:44 ChromeTest.class
    -rwxrwxrwx 1 user1 staff 1372 Jun 12 16:43 ChromeTest.java*
  • 使用相同的类路径运行 ChromeTest.class
  • java -cp "..HERE GOES CLASSPATH.." org.junit.runner.JUnitCore src/ChromeTest

    但是失败了!!

    JUnit version 4.12
    .E
    Time: 0,002
    There was 1 failure:
    1) initializationError(org.junit.runner.JUnitCommandLineParseResult)
    java.lang.IllegalArgumentException: Could not find class [src/ChromeTest]
    at org.junit.runner.JUnitCommandLineParseResult.parseParameters(JUnitCommandLineParseResult.java:102)
    at org.junit.runner.JUnitCommandLineParseResult.parseArgs(JUnitCommandLineParseResult.java:50)
    at org.junit.runner.JUnitCommandLineParseResult.parse(JUnitCommandLineParseResult.java:44)
    at org.junit.runner.JUnitCore.runMain(JUnitCore.java:72)
    at org.junit.runner.JUnitCore.main(JUnitCore.java:36)
    Caused by: java.lang.ClassNotFoundException: src/ChromeTest
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:249)
    at org.junit.internal.Classes.getClass(Classes.java:16)
    at org.junit.runner.JUnitCommandLineParseResult.parseParameters(JUnitCommandLineParseResult.java:100)
    ... 4 more

    FAILURES!!!
    Tests run: 1, Failures: 1

    尝试过 java -cp "..HERE GOES CLASSPATH.." org.junit.runner.JUnitCore src/ChromeTest.class

    但也失败了

    JUnit version 4.12
    .E
    Time: 0,002
    There was 1 failure:
    1) initializationError(org.junit.runner.JUnitCommandLineParseResult)
    java.lang.IllegalArgumentException: Could not find class [src/ChromeTest.class]
    at org.junit.runner.JUnitCommandLineParseResult.parseParameters(JUnitCommandLineParseResult.java:102)
    at org.junit.runner.JUnitCommandLineParseResult.parseArgs(JUnitCommandLineParseResult.java:50)
    at org.junit.runner.JUnitCommandLineParseResult.parse(JUnitCommandLineParseResult.java:44)
    at org.junit.runner.JUnitCore.runMain(JUnitCore.java:72)
    at org.junit.runner.JUnitCore.main(JUnitCore.java:36)
    Caused by: java.lang.ClassNotFoundException: src/ChromeTest.class
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:249)
    at org.junit.internal.Classes.getClass(Classes.java:16)
    at org.junit.runner.JUnitCommandLineParseResult.parseParameters(JUnitCommandLineParseResult.java:100)
    ... 4 more

    FAILURES!!!
    Tests run: 1, Failures: 1

    编辑:

    也尝试过:

    user1@tororrosso ~/T/> cd src
    user1@tororrosso ~/T/src> javac -cp "....." ChromeTest.java
    user1@tororrosso ~/T/src> java -cp "....." "ChromeTest"
    JUnit version 4.12
    .E
    Time: 0,001
    There was 1 failure:
    1) initializationError(org.junit.runner.JUnitCommandLineParseResult)
    java.lang.IllegalArgumentException: Could not find class [ChromeTest]
    at org.junit.runner.JUnitCommandLineParseResult.parseParameters(JUnitCommandLineParseResult.java:102)
    at org.junit.runner.JUnitCommandLineParseResult.parseArgs(JUnitCommandLineParseResult.java:50)
    at org.junit.runner.JUnitCommandLineParseResult.parse(JUnitCommandLineParseResult.java:44)
    at org.junit.runner.JUnitCore.runMain(JUnitCore.java:72)
    at org.junit.runner.JUnitCore.main(JUnitCore.java:36)
    Caused by: java.lang.ClassNotFoundException: ChromeTest
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:249)
    at org.junit.internal.Classes.getClass(Classes.java:16)
    at org.junit.runner.JUnitCommandLineParseResult.parseParameters(JUnitCommandLineParseResult.java:100)
    ... 4 more

    FAILURES!!!
    Tests run: 1, Failures: 1

    user1@tororrosso ~/T/src> java -cp "....." 'ChromeTest'
    JUnit version 4.12
    .E
    Time: 0,001
    There was 1 failure:
    1) initializationError(org.junit.runner.JUnitCommandLineParseResult)
    java.lang.IllegalArgumentException: Could not find class [ChromeTest]
    at org.junit.runner.JUnitCommandLineParseResult.parseParameters(JUnitCommandLineParseResult.java:102)
    at org.junit.runner.JUnitCommandLineParseResult.parseArgs(JUnitCommandLineParseResult.java:50)
    at org.junit.runner.JUnitCommandLineParseResult.parse(JUnitCommandLineParseResult.java:44)
    at org.junit.runner.JUnitCore.runMain(JUnitCore.java:72)
    at org.junit.runner.JUnitCore.main(JUnitCore.java:36)
    Caused by: java.lang.ClassNotFoundException: ChromeTest
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:249)
    at org.junit.internal.Classes.getClass(Classes.java:16)
    at org.junit.runner.JUnitCommandLineParseResult.parseParameters(JUnitCommandLineParseResult.java:100)
    ... 4 more

    FAILURES!!!
    Tests run: 1, Failures: 1

    编辑2:我的类路径

    也许我的类路径有问题。但这是maven使用的......

    /Users/user1/.m2/repository/cglib/cglib-nodep/2.1_3/cglib-nodep-2.1_3.jar:/Users/user1/.m2/repository/com/google/code/findbugs/jsr305/1.3.9/jsr305-1.3.9.jar:/Users/user1/.m2/repository/com/google/code/gson/gson/2.3.1/gson-2.3.1.jar:/Users/user1/.m2/repository/com/google/guava/guava/12.0/guava-12.0.jar:/Users/user1/.m2/repository/commons-codec/commons-codec/1.10/commons-codec-1.10.jar:/Users/user1/.m2/repository/commons-collections/commons-collections/3.2.1/commons-collections-3.2.1.jar:/Users/user1/.m2/repository/commons-io/commons-io/2.4/commons-io-2.4.jar:/Users/user1/.m2/repository/commons-logging/commons-logging/1.2/commons-logging-1.2.jar:/Users/user1/.m2/repository/io/netty/netty/3.5.2.Final/netty-3.5.2.Final.jar:/Users/user1/.m2/repository/junit/junit/4.12/junit-4.12.jar:/Users/user1/.m2/repository/net/java/dev/jna/jna/4.1.0/jna-4.1.0.jar:/Users/user1/.m2/repository/net/java/dev/jna/jna-platform/4.1.0/jna-platform-4.1.0.jar:/Users/user1/.m2/repository/net/sourceforge/cssparser/cssparser/0.9.16/cssparser-0.9.16.jar:/Users/user1/.m2/repository/net/sourceforge/htmlunit/htmlunit/2.17/htmlunit-2.17.jar:/Users/user1/.m2/repository/net/sourceforge/htmlunit/htmlunit-core-js/2.17/htmlunit-core-js-2.17.jar:/Users/user1/.m2/repository/net/sourceforge/nekohtml/nekohtml/1.9.22/nekohtml-1.9.22.jar:/Users/user1/.m2/repository/org/apache/commons/commons-exec/1.3/commons-exec-1.3.jar:/Users/user1/.m2/repository/org/apache/commons/commons-lang3/3.4/commons-lang3-3.4.jar:/Users/user1/.m2/repository/org/apache/httpcomponents/httpclient/4.4.1/httpclient-4.4.1.jar:/Users/user1/.m2/repository/org/apache/httpcomponents/httpcore/4.4.1/httpcore-4.4.1.jar:/Users/user1/.m2/repository/org/apache/httpcomponents/httpmime/4.4.1/httpmime-4.4.1.jar:/Users/user1/.m2/repository/org/eclipse/jetty/jetty-io/9.2.11.v20150529/jetty-io-9.2.11.v20150529.jar:/Users/user1/.m2/repository/org/eclipse/jetty/jetty-util/9.2.11.v20150529/jetty-util-9.2.11.v20150529.jar:/Users/user1/.m2/repository/org/eclipse/jetty/websocket/websocket-api/9.2.11.v20150529/websocket-api-9.2.11.v20150529.jar:/Users/user1/.m2/repository/org/eclipse/jetty/websocket/websocket-client/9.2.11.v20150529/websocket-client-9.2.11.v20150529.jar:/Users/user1/.m2/repository/org/eclipse/jetty/websocket/websocket-common/9.2.11.v20150529/websocket-common-9.2.11.v20150529.jar:/Users/user1/.m2/repository/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar:/Users/user1/.m2/repository/org/seleniumhq/selenium/selenium-api/2.46.0/selenium-api-2.46.0.jar:/Users/user1/.m2/repository/org/seleniumhq/selenium/selenium-chrome-driver/2.46.0/selenium-chrome-driver-2.46.0.jar:/Users/user1/.m2/repository/org/seleniumhq/selenium/selenium-firefox-driver/2.46.0/selenium-firefox-driver-2.46.0.jar:/Users/user1/.m2/repository/org/seleniumhq/selenium/selenium-htmlunit-driver/2.46.0/selenium-htmlunit-driver-2.46.0.jar:/Users/user1/.m2/repository/org/seleniumhq/selenium/selenium-ie-driver/2.46.0/selenium-ie-driver-2.46.0.jar:/Users/user1/.m2/repository/org/seleniumhq/selenium/selenium-java/2.46.0/selenium-java-2.46.0.jar:/Users/user1/.m2/repository/org/seleniumhq/selenium/selenium-leg-rc/2.46.0/selenium-leg-rc-2.46.0.jar:/Users/user1/.m2/repository/org/seleniumhq/selenium/selenium-remote-driver/2.46.0/selenium-remote-driver-2.46.0.jar:/Users/user1/.m2/repository/org/seleniumhq/selenium/selenium-safari-driver/2.46.0/selenium-safari-driver-2.46.0.jar:/Users/user1/.m2/repository/org/seleniumhq/selenium/selenium-support/2.46.0/selenium-support-2.46.0.jar:/Users/user1/.m2/repository/org/w3c/css/sac/1.3/sac-1.3.jar:/Users/user1/.m2/repository/org/webbitserver/webbit/0.4.14/webbit-0.4.14.jar:/Users/user1/.m2/repository/xalan/serializer/2.7.2/serializer-2.7.2.jar:/Users/user1/.m2/repository/xalan/xalan/2.7.2/xalan-2.7.2.jar:/Users/user1/.m2/repository/xerces/xercesImpl/2.11.0/xercesImpl-2.11.0.jar:/Users/user1/.m2/repository/xml-apis/xml-apis/1.4.01/xml-apis-1.4.01.jar

    最佳答案

    您正在尝试运行类“src/ChromeTest”,但我怀疑您的类文件不存在。假设你的目标文件夹位于你的类路径中,你的 java 命令行应该是 'java -cp "blah;foo;..."ChromeTest' (因为你没有包)。

    关于java - Selenium 简单 Chrome WebDriver : java compilation and execution problems,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30805856/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com