How do I run a selenium test using maven from the command line?

后端 未结 4 1784
再見小時候
再見小時候 2020-12-15 11:02

I currently use Eclipse as my Java IDE and I use Maven. I click the run button and it is able to run a Selenium Java test I wrote.

I then proceeded to install Maven

相关标签:
4条回答
  • 2020-12-15 11:17

    I didn't see this until after I had posted my own question...and subsequently found an answer to it! I've included my answer below:

    Maven can be made to run the code it compiles by using the exec-maven-plugin and adding the following to the pom.xml:

        <build>
         <plugins>
          <plugin>
           <groupId>org.codehaus.mojo</groupId>
           <artifactId>exec-maven-plugin</artifactId>
           <version>1.1.1</version>
           <executions>
            <execution>
             <phase>test</phase>
             <goals>
              <goal>java</goal>
             </goals>
             <configuration>
              <mainClass>Selenium2Example</mainClass>
              <arguments>
               <argument>arg0</argument>
               <argument>arg1</argument>
              </arguments>
             </configuration>
            </execution>
           </executions>
          </plugin>
         </plugins>
        </build>
    

    As you can probably gather from the snippet, arguments can be passed in by listing them in the pom.xml. Also, be sure to use the proper package name in the mainClass element.

    You can then run mvn compile followed by mvn test to compile and run your code.

    Credit has to go to http://www.vineetmanohar.com/2009/11/3-ways-to-run-java-main-from-maven/ for listing several ways to do this.

    0 讨论(0)
  • 2020-12-15 11:21

    This is most likely due to your directory structure. Maven uses an arbitrary directory structure.

    • src/main/java is your main java code
    • src/test/java is your tests. Maven will read THIS directory when executing mvn test by default.

    You have two options:

    1. Update your pom to something like:

      <build> <sourceDirectory>src/java</sourceDirectory> <testSourceDirectory>src/test</testSourceDirectory> ... </build>

    2. Abide by Maven convention and put your test sources under src/test/java

    0 讨论(0)
  • 2020-12-15 11:25

    This is a older thread, but still want to provide input for those who are stuck up at this. You have to make sure that the class file names you are creating are ending with "Test" string. e.g. AppTest, TempTest are all valid class file names, but AppCheck, TempTest1 are invalid name; maven will not detect these files for execution.

    0 讨论(0)
  • 2020-12-15 11:31

    I would recommend using Maven Failsafe (for integration tests), or Surefire (for unit tests).

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>2.12</version>
        <executions>
            <execution>
                <id>default</id>
                <goals>
                    <goal>integration-test</goal>
                    <goal>verify</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    
    0 讨论(0)
提交回复
热议问题