How can I run Selenium tests with Maven?

I am trying to figure out how to run Selenium WebDriver tests without using Eclipse or IntelliJ or any other development environment. I do my entire java development using a regular text editor and don't want to install (and learn) an IDE just to compile and run tests.

I tried running the Selenium documentation, but it doesn't stop, actually telling you how to run tests from the command line.

My short experience with maven comes down to the following:

$ mvn compile <snip> No sources to compile $ mvn test <snip> No tests to run $ mvn run <snip> Invalid task 'run' 

The only thing I know is mvn jetty:run , but that doesn't seem right, since I don't want to start a new web server.

I suspect that I just need to set the right goals, etc. in my pom.xml, but I donโ€™t know what they should be, and surprisingly canโ€™t find any online.

Can anyone help?

0
source share
2 answers

OK, I finally figured it out, realizing that this is actually a question specific to Maven, not Eclipse or Selenium.

Maven can be created to run the code that it compiles using the exec-maven plugin and adding the following to 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 assemble from a fragment, arguments can be passed by listing them in pom.xml. Also, be sure to use the correct package name in the mainClass element.

Then you can run mvn compile and then mvn test to compile and run your code.

The loan should go to http://www.vineetmanohar.com/2009/11/3-ways-to-run-java-main-from-maven/ to list several ways to do this.

0
source

In short:

mvn integration-test or mvn verify is what you are looking for.

Explanation

The goals you invoke are the maven lifecycle stages (see the Maven Lifecycle Reference ). mvn test designed for stand-alone unit tests, mvn integration-test works after compilation, testing and packaging. This will also be the phase in which you will refer to the Selenium tests. If you need to start and stop Jetty, Tomcat, JBoss, etc., you must bind start / stop to pre-integration-test and post-integration-test .

I usually run my integration tests using Failsafe and make calls there for Selenium and other integration tests.

+1
source

All Articles