Using custom java class in karate feature file [duplicate] - karate

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

Related

What is the gradle command to run scenarios with tags?

I am using Gradle 7.6, Karate 1.3.1, Java 17.0.5 and Junit 5.8.1.
I want to configure a Jenkin job for each feature to create a health check monitor. I need gradle commands to run feature files using tags #smoke, #regression, #featureName etc.,
I have tried with the following command, it worked earlier and stopped working recently.
./gradlew test -Dkarate.options="--tags #smoke" -Dtest.single=TestRunner#testTagsWithoutFeatureName
Where TestRunner is the following Java class
import com.intuit.karate.junit5.Karate;
public class TestRunner {
#Karate.Test
Karate testTagsWithoutFeatureName() {
return Karate.run().tags("#smoke").relativeTo(getClass());
}
}
My advice is use the Runner class, that is better designed for running tests in CI. The JUnit helpers are just for local-dev convenience: https://stackoverflow.com/a/65578167/143475
It should be possible to even pass a feature to karate.options as the last argument. Which might be more convenient than writing a Java class for every combinations. You should experiment.
Otherwise no suggestions, but if you feel there's a bug, follow this process: https://github.com/karatelabs/karate/wiki/How-to-Submit-an-Issue

How to make Karate Tests run as a service [duplicate]

I am using karate 0.9.2 with gradle. My project requires to have all karate tests inside src/main/java. So I configured the gradle dependency as ‘compile’ instead of ‘testCompile’ and also modified the sourceSets to point to main instead of test. When I ran my runner class with above configuration I got empty test suite message.
build.gradle snippet:
compile 'com.intuit.karate:karate-junit4:0.9.3'
compile 'com.intuit.karate:karate-apache:0.9.3'
sourceSets {
test {
resources {
srcDir file('src/main/java')
exclude '**/*.java'
}
}
}
Additionally, I have is to run the karate tests from the deployable project jar. Please point me the resources I can refer to achieve the same.
Not something we directly support but teams have done this in Spring Boot etc. It should be possible, see if this thread helps: https://github.com/intuit/karate/issues/520
Also you may not need JUnit also: https://github.com/intuit/karate/issues/427
And see the sample project in this ticket as an example: https://github.com/intuit/karate/issues/529
EDIT - in 1.0 onwards we hope that class-loading from spring-boot JAR files is more reliable: https://github.com/intuit/karate/issues/751

Runner.runFeature can't find the feature file path - karate 0.9.9.RC3-junit5 [duplicate]

I am using karate 0.9.2 with gradle. My project requires to have all karate tests inside src/main/java. So I configured the gradle dependency as ‘compile’ instead of ‘testCompile’ and also modified the sourceSets to point to main instead of test. When I ran my runner class with above configuration I got empty test suite message.
build.gradle snippet:
compile 'com.intuit.karate:karate-junit4:0.9.3'
compile 'com.intuit.karate:karate-apache:0.9.3'
sourceSets {
test {
resources {
srcDir file('src/main/java')
exclude '**/*.java'
}
}
}
Additionally, I have is to run the karate tests from the deployable project jar. Please point me the resources I can refer to achieve the same.
Not something we directly support but teams have done this in Spring Boot etc. It should be possible, see if this thread helps: https://github.com/intuit/karate/issues/520
Also you may not need JUnit also: https://github.com/intuit/karate/issues/427
And see the sample project in this ticket as an example: https://github.com/intuit/karate/issues/529
EDIT - in 1.0 onwards we hope that class-loading from spring-boot JAR files is more reliable: https://github.com/intuit/karate/issues/751

Selenium + sikuli Error- *No ClassDefFoundError: org/sikuli/script/Screen* in command prompt

When I'm running Selenium+ sikuli project in Command prompt getting error as
java.lang.NoClassDefFoundError: org/sikuli/script/Screen and java.lang.ClassNotFoundException: org.sikuli.script.Screen
Screen screen = new Screen();
Pattern fileUpload = new Pattern("fileUpload(1).PNG");
screen.click(fileUpload);
If any best tool than sikuli for GUI testing integrates with selenium, please Suggest.
If you run the above from command line, you probably did not define the classpath correctly and the classes you are referring to were not found. Make sure you include all related paths where your classes exist:
$ javac –classpath C:\dependency\framework.jar MyApp.Java
$ java –classpath C:\dependency\framework.jar MyApp

Intellij IDEA > Cucumber Test Results don't display Scenario name

Problem:
When running my Cucumber Acceptance tests from Intellij, the Test Results don't display the scenario name, just <no name> - making it difficult/impossible to determine which Scenario actually failed!
e.g.
I'm using JDK8 with Intellij IDEA 15 Community Edition (Build #IC-143.2287) with the following Intellij IDEA plugins:
Cucumber for Java (v 143.382) - github.com / plugins.jetbrains.com
Cucumber for Groovy (v 143.382) - github.com / plugins.jetbrains.com
The following Cucumber libraries (gradle):
testCompile "info.cukes:cucumber-junit:1.2.4"
testCompile "info.cukes:cucumber-java8:1.2.4"
testCompile "info.cukes:cucumber-spring:1.2.4"
This is my AcceptanceTests.groovy file:
import cucumber.api.CucumberOptions
import cucumber.api.junit.Cucumber
import org.junit.runner.RunWith
#RunWith(Cucumber.class)
#CucumberOptions(
strict = false,
plugin = ["pretty", "html:build/reports/cucumber"],
tags = ['~#ignore']
)
public class AcceptanceTests {}
Just wondering:
Is there anyway to display the scenario name?
Is it something I'm doing OR is it just not implemented yet? (OR does it exist in the Intellij IDEA Ultimate Edition?)
Who can I suggest this feature to, if it doesn't exist yet?
Update - 21/03/2016
I've forwarded my question to the author (Andrey Vokin) and raised an issue - https://youtrack.jetbrains.com/issue/IDEA-153338 .
I have same problem, and found where it's coming from in my case. For "scenario" at least, it seems to come from dots (".") : remove them from your scenario description and see what happens.
I was also facing the same problem, but I haven't used any dots (".") in scenario description. Though the full stop (dot) was present in the Feature file description and after removing that it worked for me.
Feature file description:
After result how it looks like: