How can I run my Selenium tests with Maven? [duplicate] - java

This question already has answers here:
How do I run a selenium test using maven from the command line?
(4 answers)
Closed 7 years ago.
I'm trying to figure out how to run Selenium WebDriver tests without having to use Eclipse or IntelliJ or any other IDE. I do all my java development using a plain text editor, and don't want to have to install (and learn) an IDE just for the sake of compiling and running tests.
I've tried following the Selenium documentation, but it stops short of actually telling you how to run the tests from the command line.
My brief experience with maven amounts 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 other one I know is mvn jetty:run but that doesn't seem right as I'm not wanting to run a new web server.
I suspect I just need to set the correct targets etc in my pom.xml, but I don't know what they should be, and surprisingly can't find any online.
Can anyone help please?

In short:
mvn integration-test or mvn verify is the thing you're looking for.
Explanation
The goals, you're invoking, are lifecycle phases of maven (see Maven Lifecycle Reference). mvn test is intended for standalone unit tests, mvn integration-test runs after compiling, testing and packaging. That would be also the phase, where you invoke Selenium tests. If you need to start and stop Jetty, Tomcat, JBoss, etc., you would bind start/stop of these to pre-integration-test and post-integration-test.
I usually run my integration-tests using Failsafe and perform there invocations to Selenium and other integrative tests.

OK, I finally figured it out after realising that this is actually a Maven-specific question rather than Eclipse or Selenium.
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.

Related

How to debug a Maven application in Eclipse

Let me start by clarifying I have check every possible resource, tutorial, video, other stackoverflow questions that I could get my hands on that are related in order to try and find an answer to this. I'm using java 8 and Eclipse Luna Service Release 2 (4.4.2) with m2e plugin, It's quite hard to believe but it seems there isn't a single example that clearly explains how you actually debug a Maven Java application USING ECLIPSE. There are no less than 40 different sources here are some
Debugging with exec-maven-plugin
https://blog.jooq.org/how-to-debug-your-maven-build-with-eclipse/
Eclipse not stopping at java breakpoints during remote debug
https://github.com/howlger/Eclipse-IDE-improvements-videos/tree/2022-03/sample_code
IntelliJ IDEA Maven project StackOverflowError when running debug configuration
Debugging in Maven?
What I'm trying to do is click the debug button in eclipse and run a debug configuration that allows me to hit my breakpoints.
I have the following configuration in Eclipse
this runs the exec:exec goal from my pom.xml which looks like this
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>java</executable>
<arguments>
<argument>-classpath</argument>
<classpath />
<argument>core.app.server</argument>
<argument>-agentlib:jdwp=transport=dt_socket,suspend=y,server=y,address=*:49875 </argument>
</arguments>
<workingDirectory>${project.build.outputDirectory}</workingDirectory>
</configuration>
</plugin>
Ok, so far so good. At this point if I run the debug configuration the app launches and then hangs which I'm assuming it is waiting for the debugger to remotely connect based on the arguments in my pom.xml. So the app starts hangs and in Eclipse at this point I'm looking at this
At this point I've tried everything I can imagine. I notice the exec plugin launcher starts on a random port which in the picture is 51661, when in my exec arguments in the pom.xml I have it set to 49875 so this seems off. Another thing I noticed if I removed the <argument>-agentlib:jdwp=transport=dt_socket,suspend=y,server=y,address=*:49875 </argument> line from my pom.xml the app launches completely , runs fine but this gets me no where. I have tried to connect using a "Remote Java Application" configuration after the app launches this also does not work. I normally use IntelliJ which makes it an absolute breeze since everything is handled OOTB, unfortunately I have to get this working in Eclipse as well in a similar fashion.
How do I configure the Debug Configuration in a way that will allow me to launch the app and hit my breakpoints?
EDIT 1
When setting suspend=n the app still hangs when launching with the argument agentlib:jdwp
EDIT 2
In an effort to get this figured out without wasting another day on something that should be effortless, I tested running a clean install from and debug configuration and then tested using the run as commands provided by m2e shown below and the m2e commands work perfectly where as the same exact commands ran with a debug configuration fail for missing references.
EDIT 3
Reading the documentation on the exec maven plugin here https://www.mojohaus.org/exec-maven-plugin/index.html it says the difference between exec:exec and exec:java is that the ladder execute programs and Java programs in a separate process and the former execute Java programs in the same VM. I think this might be somewhat related to the issue I'm having. This should be really easy to test for someone familiar with MAven/Eclipse I would think, is anyone able to create a super basic hello world app maven project and see if they can set and hit a break point in the main method, should take but 5-10 min?
I got it working like so:
Download, extract (latest!?) Luna SR2 JavaEE Edition
set (correct) JAVA_HOME/vm in eclipse.ini (oracle-8 latest)
Welcome Screen, create new Maven project, simple, skip achetype...
(So with fresh defaults, simple project):
With the following pom section :
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<executable>C:\Program Files\Java\jdk1.8.0_333\bin\java.exe</executable>
<arguments>
<argument>-agentlib:jdwp=transport=dt_socket,suspend=y,server=y,address=49875</argument>
<argument>-classpath</argument>
<classpath />
<argument>com.example.Demo</argument>
</arguments>
<workingDirectory>${project.build.outputDirectory</workingDirectory>
</configuration>
</plugin>
</plugins>
</build>
(very similar to yours, only significant difference: address, server is crucial in this approach, suspend either)
With the same (Maven) Run configuration, as you show:
We now additionally need:
"Debug Configurations..."
Remote Java Application(! ..because we chose "server"..)
we select the (sources) project as "Project"
Connection Type: attach
Host: localhost
Port: the one configured in jdwp
Looks like:
Set "my" breakpoint in:
package com.example;
public class Demo {
public static void main(String[] args) {
int x = 0;
x++; // just to set breakpoint somewhere
System.out.println(x);
}
}
"Run" "Exec_Java" (suspend, and server flags (and all) have effect!)
Console(Exec_Java) should report:
...
[INFO] --- exec-maven-plugin:3.0.0:exec (default-cli) # luna-res ---
Listening for transport dt_socket at address: 49875 //!!
"Debug" "New_Configuration".
..and the breakpoint halts(, and we get the "switch perspective dialog";)

How to Skip All Integration Tests (-DskipITs) and One Unit Test mvn Command Line

I am relatively new to Maven. I have done a lot of research and digging on this topic, but I can't seem to find an answer, so I thought I would ask here.
Goal: I would like to run mvn clean install test while skipping integration tests, as well as one particular unit test class.
I have tried the following:
mvn clean install -DskipITs -Dtest=!MyTestClass test
mvn clean install -DskipITs&&test=!MyTestClass test
mvn clean install -DskipITs&test=!MyTestClass test
However, none of the above commands seem to work. The first command of the three above made the most sense to me, but it seems as though the integration tests are being run when using that command. This is where my knowledge and understanding of Maven has a gap; I'm not sure if that's the expected behavior, or if that is the appropriate way to pass multiple properties on the command line?
When I run this command: mvn clean install -DskipITs test, the integration tests are successfully skipped.
I am familiar with the Maven build life-cycle, but it is possible that I am misunderstanding something or missing a detail.
Integration tests with maven are normally run with maven-failsafe-plugin
To tell this plugin to skip integration tests (make sure your integration test class names follow the convention *IT.java, otherwise you need to include them with <inclusions>), you can do that in the plugin's configuration, or from the command line (official doc):
mvn test -DskipITs
Single tests can be skipped with:
mvn test -Dtest=!MyTestClass
So this should work:
mvn clean install -DskipITs -Dtest=!MyTestClass
What worked for me was the following command:
mvn clean install -DskipITs "-Dtest=!MyTestClass, !**/*IT.java" test
I am still learning Java, but here is what I think happened in my case.
There are two plugins pertaining to testing in Java (there are probably many more, but these two were relevant to my issue): one is called "maven-failsafe-plugin", while the other is "maven-surefire-plugin". As #hovanessyan and others have pointed out, maven-failsafe-plugin typically runs integration tests, while maven-surefire-plugin typically runs unit tests (Maven docs reference).
In my case, when I would run the command mvn clean install -DskipITs -Dtest=!MyTestClass test, upon further digging in the logs, the integration tests would fail and I would receive the following additional buried error message:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.21.0:test (default-test)
The integration tests for the project are found in a directory called integrationtests, and are named according to the convention "MyIntegrationTestIT.java". What eventually led me down the right track was this: Surefire docs. These docs describe the "test" argument that you can pass with a Maven command.
It seems to me that when I passed the argument -Dtest=!MyTestClass, it's as if that instructed the Surefire plugin to "don't run MyTestClass, but do run every other test file." Meanwhile, the -DskipITs argument instructed the Failsafe plugin to skip integration tests (which it had been doing all along). When I explicitly called out the test files that I didn't want to run, in the form "-Dtest=!MyTestClass, !**/*IT.java", Surefire understood exactly what I wanted to do. The Surefire plugin ran every test with the exception of MyTestClass and the integration tests, and the Failsafe plugin skipped the integration tests.
I don't fully understand why, in my case, the Surefire plugin was running the integration tests in the first place. Maybe it has to do with some config setting in the codebase I'm working with, or the naming convention of the integration test files, or some annotation (I'm still learning a lot about these things). I am sure that this answer could be edited to include even more helpful information or context that I don't yet have. In any case, hopefully these learnings are helpful for some other folks experiencing this issue.
Finally, it helped a lot when debugging to run the command mvn help:effective-pom and pass -X along with my mvn clean install test command.
This configuration works for me:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<excludes>
<exclude>%regex[.*TestIT.*.class]</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<includes>
<include>%regex[.*TestIT.*.class]</include>
</includes>
</configuration>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>

Spring boot doesn't run unit tests

How can I run unit tests for spring boot application while building and deploying using spring boot:run command.
My expectation is to have all my unit tests executed before running application, but I dont want to make another maven command like mvn test before.
My problem:
I made a simple spring boot application and I could'd find a way to run unit tests while running application from intellij or from command line. Firstly I thought that maybe I have wrong configuration or wrong names of test classess or maybe wrong project structure. So I created spring boot application from intellij template. To my happiness it had already default test written so I simply run application. Unfortunatelly test was not executed.
This is a screenshot of project structure, pom.xml, main class and unit test created by intellij.Project created by intetelij
I changed the test runner and test to fail and tried again. Same result.
unit test changed to fail
I googled what is hidden underneath spring boot:run command here
http://docs.spring.io/spring-boot/docs/current/maven-plugin/run-mojo.html
I found something interesting at the top of manual: "Invokes the execution of the lifecycle phase test-compile prior to executing itself." So my understanding is that this command only compiles tests but not run them? If So, the question is - Is it possible to add "test" phase by adding some flag to the command?
Your problem here is to do with the maven lifecycle. According to the docs for the spring-boot:run, it binds to the lifecyle phase validate by default, and invokes the phase test-compile before executing.
What you're asking for is to execute the tests before running the application. You could do this with a custom maven profile in your POM - something like the following.
<project>
<profiles>
<profile>
<id>test-then-run</id>
<build>
<defaultGoal>verify</defaultGoal>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>spring-boot-run</id>
<phase>verify</phase>
<goals>
<goal>run</goal>
</goals>
<inherited>false</inherited>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
...
</profiles>
...
</project>
With this in your POM, you could then run the tests and start the app with:
mvn -P test-then-run
This binds the run goal to the verify phase instead of the validate phase, which means that the tests will be run first. You can see which order the phases are run here: https://maven.apache.org/ref/3.3.9/maven-core/lifecycles.html
I knew that I could use mvn test before running application. In fact it can be achieved by many ways in intellij. For me, I just add mvn goal test before launching application:
spring boot configuration in intellij.
But of course it may be done differently.
The thing is, I was just very curious whether I could somehow manipulate spring boot:run command by using some flag or sth else to get same results but its impossible.
olambert, thank You for Your answer too, it works very well.

How can I prevent tests from running during deploy?

currently I have surefire configured though Spring Boot's BOM.
I have tests running during deploy though there seems to be some issue with one of them... that said this same test passes just fine in the previous part of the pipeline. I don't actually need these tests to run twice.
I would like to do this as part of my parent BOM (has springs BOM as its parent)
How can I configure tests to not be run during the deploy phase? mvn deploy, running mvn test and mvn verify must continue to work as normal.
You can achieve this while building(install) the archive itself.
We can skip tests while building by two ways:
Using CLI:
mvn install -Dmaven.test.skip=true
Make the same change in pom.xml as:
<configuration>
<skipTests>true</skipTests>
</configuration>
Then give:
mvn install
Both will build the archive without running the testcases.
And mvn deploy, running mvn test and mvn verify will continue to work as normal. Since this command is independent of these three commands.
You can achieve it by using commands while deploying or in pom.xml
To skip the entire unit test, uses argument -Dmaven.test.skip=true
mvn install -Dmaven.test.skip=true
OR in pom.xml
<configuration>
<skipTests>true</skipTests>
</configuration>
Need to configure in skipTests in surfire plugin
<configuration>
<skipTests>true</skipTests>
</configuration>
For more details please follow
http://maven.apache.org/surefire/maven-surefire-plugin/examples/skipping-test.html

How to execute code after eclipse:eclipse in maven?

I want to run some code after the eclipse goal of the eclipse plugin (eclipse:eclipse) runs.
The documentation of the eclipse plugin says that the generate-resources phase is execute prior to the eclipse goal, but it doesn't mention any phase that is executed after.
I wanted to include the code with the gmaven-plugin but I can't find the right configuration:
<execution>
<id>delete_generated_sources_from_cp</id>
<phase>generate-resources</phase>
<goals>
<goal>execute</goal>
</goals>
<configuration>
<providerSelection>1.7</providerSelection>
<source>
modifyClasspath.groovy
</source>
</configuration>
</execution>
Simply add the plugin you want to execute to your project. If you add an execution of the plugin to the phase process-resourcesit will execute after the eclipse plugin if you run mvn process-resources (or any phase after process-resources)
You could also invoke them directly: mvn eclipse:eclipse otherplugin:goal
eclipse:eclipse is not good way for executing the codes. I mean if you want to import project to eclipse, you should use 'eclipse:eclipse' otherwise you shouldn't.
Executing the code is depend on you codes; if this is web based, you should deploy it in your application server. If it has executable main class, you should execute it with 'java' command.
I wanted to include the code with the gmaven-plugin but I can't find the right configuration
I could not understand what you mean. could you explain more ..

Categories