Configure IntelliJ to run Groovy Cucumber Geb framework - intellij-idea

I'm trying to configure Run/Debug configurations in IntelliJ (v2017.2.5) to run cucumber tests written in Groovy (v2.4) using Geb (v1.1.1) in a Gradle project. I need to use InternetExplorerDriver (v3.6)
I tried:
- Installing Cucumber for Groovy plugin and did following in Edit Configurations for IntelliJ:
Main Class: cucumber.api.cli.Main
Glue: C:\automation\Project\src\cucumber\resources\steps C:\automation\Project\src\cucumber\resources\env
Feature or folder path: C:/automation/Project/src/cucumber/features
Program Arguments: --plugin org.jetbrains.plugins.cucumber.java.run.CucumberJvmSMFormatter --monochrome
I'm getting following error when I run/debug a feature file:
geb.driver.DriverCreationException: failed to create driver from callback 'script15077602688031015554790$_run_closure2#55d58825'
I have tried looking for solutions for similar problem on stackoverflow which suggested upgrading selenium/groovy versions (i'm using latest version
groovyVersion = '2.4'
gebVersion = '1.1.1'
seleniumVersion = '3.6.0'
I'm new to Groovy, Geb and Gradle (I know Java and Maven) hence I suspect it could be a configuration issue.
FYI, the tests run fine through commandline. I need to get debugging in intellij working.

Figured out the answer:
In my case the configuration in IntelliJ was wrong:
I used:
Main Class: cucumber.api.cli.Main
Glue: C:\automation\Project\src\cucumber\resources\steps
C:\automation\Project\src\cucumber\resources\env
Feature or folder path: C:/automation/Project/src/cucumber/features
Program Arguments: --plugin org.jetbrains.plugins.cucumber.java.run.CucumberJvmSMFormatter --monochrome
module: Project_cucumber
Before launch: Build, Gradle task, Activate tool window:
Run Gradle task 'Project: compileCucumberGroovy'
Activate tool window [checked]

Related

Using custom java class in karate feature file [duplicate]

Good morning. I am working on a project that uses Karate Standalone. I am completely new to Karate to excuse my lack of knowledge here.
The standalone karate jar is executed with the '-m' command line parameter to start a mock.feature. The mock.feature references a utils class that is built on 'org.springframework.amqp'.
The problem is that the karate.jar startup fails with a Command Line Execution Exception due to external library 'org/springframework/amqp/rabbit/connection/ConnectionFactory'
api1_mock_test.feature
Feature: API1 Mock Test
Background:
* def RabbitUtils = Java.type('utils.RabbitUtils')
.
.
Our RabbitUtils is just a java class that imports org.springframework.amqp external libraries to provide functions to interact with a Rabbit AMQP broker e.g. connect, receive, publish, purge etc. When built and run in IntelliJ all works ok. The POM reference in the project is:
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>2.1.5.RELEASE</version>
</dependency>
Does the Karate standalone jar have some way of referencing external libraries? The classpath parameter is set to reference our workspace '.\target\test-classes' and contains the RabbitUtils.class file.
The current execution from workspace root looks like this:
java -jar C:\intuit\karate-0.9.3.RC2.jar -cp .\target\test-classes -p 6868 -m .\src\test\java\mocks\api1_mock_test.feature
08:57:05.122 [main] INFO com.intuit.karate.Main - Karate version: 0.9.3.RC2
08:57:05.891 [main] ERROR com.intuit.karate - server-side background init failed - api1_mock_test.feature:4
Exception in thread "main" picocli.CommandLine$ExecutionException:
-unknown-:4 - org/springframework/amqp/rabbit/connection/ConnectionFactory
Thank you!
Thanks for asking this, and I think I've figured out a way to do this which opens up a lot of great possibilities. The solution is to use Java first-principles, and not use the -jar option. The Karate command-line-app (or CLI) class happens to be com.intuit.karate.Main. I'm going to provide a demo here of using SikuliX. First, the feature file test.feature:
Feature: sikuli test
Background:
* def Screen = Java.type('org.sikuli.script.Screen')
Scenario:
* def s = new Screen()
* def c = s.capture()
* c.getFile('.')
And with the karate.jar and sikulixapi.jar in the same folder on the command line, this works (for windows, use ; instead of : as the "path separator"):
java -cp karate.jar:sikulixapi.jar com.intuit.karate.Main test.feature
For those looking to customize the classpath for the Visual Studio Code "Karate Runner" extension, please refer this: https://github.com/intuit/karate/wiki/Karate-Robot-Windows-Install-Guide#change-command-line-settings
Also see: https://stackoverflow.com/a/58398958/143475
For those who really don't want to compile Java but need to use some JVM libraries, it is possible via pure JS, (but hard to troubleshoot and debug): https://stackoverflow.com/a/65035825/143475

alternate way to set Build and run using IntelliJ IDEA

I am working with Java source code with TestNG and frequently see errors like no test found to run OR Test event were not received whenever I try to run test cases in IntelliJ IDEA.
Which I can fix with changing Build and run using IntelliJ IDEA from Gradle.
I am looking for alternative way using which can add this somewhere as configuration instead of going and changing this manually.
You can use gradle-idea-ext-plugin to set build and run actions right in the Gradle build script:
import static org.jetbrains.gradle.ext.ActionDelegationConfig.TestRunner.CHOOSE_PER_TEST
plugins {
...
id "org.jetbrains.gradle.plugin.idea-ext" version "1.0"
...
}
idea.project.settings {
delegateActions {
delegateBuildRunToGradle = true // Delegate Run/Build to Gradle
testRunner = CHOOSE_PER_TEST // Test execution: PLATFORM, GRADLE or CHOOSE_PER_TEST
}
}
But actually, the fact that it works with IDE runner, but does not work with Gradle runner may indicate problems. I would first check if it works from the command line Gradle - make sure you run the same test with it as from the IDE. If it works in terminal but does not work in IDE, I would report a bug at YouTrack with reproducible sample.

Intelij 2019.1 update breaks JUnit tests

After 2019.1 update broke all tests with error:
no tests found for given includes xxxx.someThingTest
Intelij somehow changed setting with update.
Settings > Build,Execution,Deployment > Build Tools > Gradle > Runner > "Run tests using:"
Changed from "Gradle Test runner" to "Platform Test runner" and it worked.
I hope this is useful in some matter.
If you are using JUnit5 with Gradle, add below code to build.gradle file.
test {
useJUnitPlatform()
}
I've got a hint from https://www.baeldung.com/junit-5-gradle
Checkout this build.gradle file for using Junit5
https://github.com/junit-team/junit5-samples/blob/r5.4.0/junit5-jupiter-starter-gradle/build.gradle
I experienced the same problem in 2019.2 for a newly developped class that was not detected. I strangely solved it by manually running "Build->Rebuild Project"
The workaround with Runner by Mike was not working for me.
Switching to JDK 11.0.2 solved this for me.
Not sure if it is the real cause, though.
When using JUnit 5, make sure that you use the interfaces provided by org.junit.jupiter
So for instance, you should annotate your tests with org.junit.jupiter.api.Test instead of org.junit.Test
For JUnit 4, and prior, use the interfaces provided by org.junit

IntelliJ Error:java: java.lang.ExceptionInInitializerError

Every time I encounter this exception in IntelliJ, I fix it trivially and forget the fix easily.
Code:
package whatever;
import org.junit.Test;
public class TestClass
{
#Test
void test() {}
}
Scenario:
Add new TestClass.
Right-click TestClass.
Select "Run 'TestClass'" to run test cases.
The "Messages Build" pane shows:
Information:javac 9-ea was used to compile java sources
Information:Module "dummy" was fully rebuilt due to project configuration/dependencies changes
Information:8/16/17 11:35 PM - Compilation completed with 1 error and 0 warnings in 1s 663ms
Error:java: java.lang.ExceptionInInitializerError
What can possibly go wrong?
What are the likely issues in this simple scenario?
IntelliJ: COMMUNITY 2017.1 (idea-IC-171.4424.56)
To fix the issue, I do:
File -> Project Structure... -> Project Settings / Project -> Project SDK.
Change from "9-ea" to "1.8".
DETAILS
Apparently, the issue is discrepancies in selected JDK-s to build (java 9) and run (java 8).
I'm not sure how "9-ea" gets re-selected there for the same project - neither IntelliJ itself runs in "9-ea" JRE (according to Help -> About) nor JAVA_HOME env var is set to it nor other possible settings (like Maven -> Runner) suggest any "9-ea".
I also didn't manage to run the test under the same JDK (java 9) which it gets compiled under. However, it's unclear what JDK tests are run under because IntelliJ reports only about JDK for compilation.
If you use Lombok: For me it was a solution to set the newest version for my maven lombok dependency in the pom.xml.
*<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
<version>1.18.8</version>
</dependency>*
I was facing same error when i tried to run my application in IntelliJ-2019.2 version. Below are the steps i followed to resolve this issue.
Versions:
IntelliJ : IDEA-IntelliJ-2019.2
Java : jdk1.8_221
Go to below path in IntelliJ
File -> Project Structure -> Project -> Project SDK -> (select java version which you want to use )
(In my case under 'project SDK' java-11 was selected, I changed it to 'java8')
Click on 'Apply' and then 'OK'.
I feel I ran into this issue because IntelliJ was trying to compile my java classes using in-built java-11 whereas my java classes are built on java-8. So when i explicitly configured java-8 in IntelliJ, It worked!! Hope this helps.
I started seeing this exception once I installed Java 11 in my machine. JAVA_HOME was by default pointing to Java 11 and my project was still in Java 8. Changing JAVA_HOME to Java 8 jdk fixed the issue for me.
If you have multiple projects each running on a different JDK, use this command to temporarily change the Java version per command.
JAVA_HOME=/path/to/JVM/jdk/Home mvn clean install
If you have recently updated your IDE then you can try these steps.
Delete .idea directory for the idea project/workspace
Then go to File -> Invalidate Caches / Restart...
Once Idea is restarted re-add/import your module(s)
I faced a similar issue with JARs and Jena (while run from IntelliJ it works).
I was using Apache Jena v4.0.0 in my project and have built a JAR (with a main class for the JAR to act as a console app).
The JAR builts successfully with IntelliJ but when run throws java.lang.ExceptionInInitializerError ... Caused by: java.lang.NullPointerException. NPE suggests that something was not initialized properly.
The jar built with previous version Jena 3.17.0 works perfectly.
What I did to fix it
I've opened both the JARs, compared their META-INF folders and encountered the difference in
my.jar\META-INF\services\org.apache.jena.sys.JenaSubsystemLifecycle
The new version (jena v4.0.0) contains only one line:
org.apache.jena.tdb.sys.InitTDB
The old version (jena v3.17.0) contains two different lines:
org.apache.jena.riot.system.InitRIOT
org.apache.jena.sparql.system.InitARQ
I've added the old two lines to the file and repacked new JAR with it:
org.apache.jena.tdb.sys.InitTDB
org.apache.jena.riot.system.InitRIOT
org.apache.jena.sparql.system.InitARQ
It resolved my issue.
Update: recent Jena v4.4.0 builts with the same "bug".
I'm not an expert and there is probably a better way than patching a JAR by hand.
But I still hope that this solution will help someone like me.

Cucumber in Intellij failing to use certificates

When running a Cucumber test, using cucumber.api.cli.Main, the test is failing to recognise the certificates that are installed in the JDK.
I am running using the following:
IntelliJ 2016.1.2 (Build #IU-145.972)
JRE 1.8.0_74-b02
Plugin Cucumber for Java version: 999.999
It works, when run from the following gradle task, from the command line:
task functional(dependsOn: ['testClasses']) << {
javaexec {
main = "cucumber.api.cli.Main"
classpath = sourceSets.test.runtimeClasspath
args = ['--tags', '~#wip',
'--plugin', 'junit:build/junit-test-report.xml',
'--plugin', 'pretty',
'--plugin', 'html:build/cucumber-html-report',
'--plugin', 'json:build/cucumber-json-report.json',
'--glue', 'step_definitions',
'--strict',
'src/test/resources'
]
if(project.hasProperty("coverage")) {
jvmArgs = ["-javaagent:${configurations.codeCoverage.asPath}=destfile=${buildDir}/jacoco/jacoco.exec,sessionid=HSServ,append=false"]
}
}
}
But when I create a Run Configuration for Cucumber, using:
Main Class: cucumber.api.cli.Main
Glue: step_definitions
Feature or Folder path: /Users/xetius/application/src/test/resources/system_tasks/request/validation_handling/request/validate_hardware_fulfilment_request.feature
Program Arguments: --plugin org.jetbrains.plugins.cucumber.java.run.CucumberJvmSMFormatter --monochrome --name "^Pending details$"
Working Directory: /Users/xetius/application
It fails to recognise the certificates for connecting to the remote services.
I have configured IntelliJ to boot using the external JDK, into which I have installed the certificates using keytool, and I can see that it is using this JDK when running the configuration. I have also tried configuring Server Certificates, and importing the same certificate.
However, within IntelliJ, it throws an exception when connecting to remote services. It is also throwing the exception when gradle is run from within the IntelliJ terminal.