I am working quite some time with jMeter now but this is the first time I want to start several jMeter Tests from my own code.
Basically, I copied from here: 5 ways to launch jmeter test without using jmeter gui
The JMX-File has been created with the GUI containing two variables.
The HTTP Sampler contains in the "Server name or IP" field:
${__P(server)}
The Path field contains:
/${__P(target)}
The respective entries in the jmeter.properties file are:
server=127.0.0.1
target=README
When running in jMeter, both values get replaced and the correct URL is passed.
When accessing the both properties during Runtime in my java code it yields the correct results.
When jMeter tries to replace my values, I get this message:
"Not running version, return raw function string"
I tried to trace down the problem in the jmeter sources but did not get to a result.
The Java code I use is:
public static void main(String[] args) throws IOException {
StandardJMeterEngine jmeter = new StandardJMeterEngine();
JMeterUtils.loadJMeterProperties("C:\\data\\apache-jmeter-2.13\\bin\\jmeter.properties");
JMeterUtils.setJMeterHome("C:\\data\\apache-jmeter-2.13");
JMeterUtils.initLocale();
System.out.println("Property: " + JMeterUtils.getProperty("server"));
SaveService.loadProperties();
FileInputStream in = new FileInputStream("c:\\data\\test.jmx");
HashTree testPlanTree = SaveService.loadTree(in);
in.close();
jmeter.configure(testPlanTree);
jmeter.run();
}
Any help is highly appreciated.
Best regards, Jan
I ran into the exact same issue. There is a separate jar artifact which contains a couple of Functions such as org.apache.jmeter.functions.Property2. You need to make sure that those get registered properly.
Using maven I added this to my pom:
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_functions</artifactId>
<version>${jmeter.version}</version>
</dependency>
Now, tell JMeter to search for additional components within it
JMeterUtils.setProperty("search_paths", "ApacheJMeter_functions-2.13.jar");
The string you add to the search path needs to match the artifact as it is found on the classpath. So, when launched from eclipse that is the path to the artifact within your maven repository.
Related
I have a test suite (jUnit, Selenium, Cucumber) Maven project.
I need to be able to run the tests from the command line, passing in different properties files as arguments to diversify the test cases. How can I do this?
I currently have a properties reader that has a path to a shared properties folder concatenated with a variable that holds the name of a given properties file. I'm wondering if that can be parameterized for use with a Maven command in the CLI?
I've been researching this for awhile and have found many questions that sound similar to what I'm trying to achieve, but none of the answers have been applicable to my situation/what I'm trying to do. Any advice, ideas, or resources given will be greatly appreciated.
You can simply pass java properties to Maven:
$ mvn clean test -Dmyproperty=some-property-file.properties
Then you can access the property in your test:
#Test
public void test() {
String propertyFile = System.getProperty("myproperty");
assertEquals("some-property-file.properties", propertyFile);
}
I have created a Java project with 1 class containing 2 simple tests, exported as jar and added the jar in my JMeter JUnit folder, now after restarting the JMeter i don't see my class in JMeter even after selecting Annotation 4 option.
This is my class:
package print;
import org.junit.Test;
public class PrintClass {
#Test public void test() {
System.out.println("Hello World..!");
}
}
Consider the following checklist:
Your JUnit test class should have either empty constructor or constructor taking a single string as an argument. Other options are not supported
You should place your .jar file(s) under "lib/junit" folder of your JMeter installation
If there are any dependencies you need to put them somewhere in JMeter classpath as well
JMeter restart will be required to pick the .jars up
In case of any problems first of all check jmeter.log file (normally it lives under "bin" folder of your JMeter installation and contains enough troubleshooting information)
See How to Use JUnit With JMeter article for more details.
Even I faced same kind of issue then I added the dependency jars to the lib file.
Make sure you copy these files at this location -> apache-jmeter-5.1.1\lib
1.Selenium webdriver jar file (selenium-server-standalone)
2. Junit4 Jar file (junit4)
Make sure you add Jar file from eclipse or any IDE to this location -> apache-jmeter-5.1.1\lib\junit
1. Jar file which contains the automation selenium code (Using Junit)
Restart Jmeter and continue which the normal process of adding thread group and adding Junit sampler etc...
This resolved my issue and I was able to run my scripts on Jmeter.
In eclipse make sure that you create a JUnit class, not just the class and add junit annotations to this, even i was facing the same issue, it got resolved when i was created a JUnit class and then uploaded my project in JMeter
Did you put the jar in :
/lib/junit
Check you jar by running below command to see if it is ok:
jar -tvf <your jar>
And its dependencies as described in:
JUnit test classes not showing up in JMeter
See this for more details:
http://jmeter.apache.org/usermanual/junitsampler_tutorial.html
I am very new to JMeter
I am trying to use Junit Request sampler in JMeter.In my project we have a class called PayloadProcessorTest.java. from these class methods i am calling some other class methods.It has lot of dependencies
How can i create jar file for PayloadProcessorTest.java with dependencies
I saw many examples for JMeter with Junit Request sampler. But, those all examples are independent classes
Can any one please help me
There are several ways of creating a .jar file:
Using Maven
Using Ant
Using Eclipse
.jar files are basically ZIP archives so you can just compile your PayloadProcessorTest.java and put resulting PayloadProcessorTest.class into /lib/junit/test.jar file keeping package structure. After restart JMeter will pick up the class. Don't forget to add any 3rd-party jars used in PayloadProcessorTest (if any) to JMeter classpath. For more information check out How to Use JUnit With JMeter guide.
If you want to get the dependencies with Maven you can run
mvn install dependency:copy-dependencies, which will create a folder inside your target folder called 'dependency' filled with the dependencies. To speed this up you can add the command as External Tool in Eclipse using Run > External Tools > External Tools Configurations.
Or if you want to use Eclipse you can choose File > Export > Java > Runnable JAR file and select the option 'Copy required libraries into a sub folder next to the generated JAR'. However to do this you will need to add a main class, and run it once as a Java Application before trying to export. The Main class can be empty or not.
package test;
public class Main {
public static void main(String[] args) {
}
}
Really, it depends what packaging capabilities you have.
You need to compile your classes in one or more jars, and then put them in the %JMETER_HOME%/lib/ext folder.
Or use maven to do it all for you.
I don't know how to call a Job defined in Spring Batch using CommandLineJobRunner, documentation details are not enough for me.
I've followed the Spring Batch official guide to write Jobs in Spring Batch using Java annotations e.g. #EnableBatchProcessing because I wanted to avoid XML configuration files for the description of the job, the steps, etc.
So far I have:
a configuration class (com.package.bla.bla.ClassContainingTheBatchConfiguration see below) where I've put all the stuff defining ItemReader, ItemProcessor, ItemWriter, Job, and Step (with return jobs.get("nameOfTheJob") see below) using a #Bean annotaion.
a class with a main method with SpringApplication.run(...) and and annotation with #ImportResource("classpath:META-INF/spring/applicationContext.xml") to import some beans I need when processing the data in the Job.
On the Maven side I am currently using some plugins:
maven-jar-plugin specifying <addClasspath>true</addClasspath> and the class containing the main method in the tag <mainClass>
maven-assembly-plugin because I would like a unique executable jar containing all the stuff in the dependencies, I am using <phase>package</package> to be able to build the jar in the package phase, I am also using <goal>single</goal> to be able to properly build the jar using the assembly
maven-compiler-plugin specifying I am using Java 1.7
I think I've configured all the things I need to configure, however after having a Maven BUILD SUCCESS I am not able to run the job from the command line:
java -cp ./target/JAR_FILE_NAME.jar org.springframework.batch.core.launch.support.CommandLineJobRunner com.package.bla.bla.ClassContainingTheBatchConfiguration nameOfTheJob
Is throwing IOException due to the java.io.FileNotFoundException regarding com.package.bla.bla.ClassContainingTheBatchConfiguration. How should I specify the parameters in the command line in order to get the Job executed?
If you are already using SpringApplication from Spring Boot, why not finish the job and use #EnableAutoConfiguration as well, and also the Maven plugin (see for example this guide)? That way you will get something working pretty quickly and you can always add your own features later.
If the first argument to the CommandLineJobRunner is your #Configuration FQCN instead of a resource path, the ClassPathXmlApplicationContext constructor that's called from the CommandLineJobRunner's start() method will break.
int start(String jobPath, String jobIdentifier, String[] parameters, Set<String> opts) {
ConfigurableApplicationContext context = null;
try {
context = new ClassPathXmlApplicationContext(jobPath);
If you've already written a class with a main(), that replaces the CLJR, you shouldn't be passing CLJR as the class name in the command line. Pass that instead.
dont use spring.batch.job.enabled=false
then run using java -jar [jar-files] --spring.batch.job.names=[job-name]
I'm using JUnit and Selenium2 to test my application. I execute the tests using Maven with the surefire plugin running in Jenkins. The test where fine some weeks and I added more and more tests and now the tests fails with this message:
Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.11:test (default-test) on project guitest: Failure or timeout
...
Process leaked file descriptors
I first thought I'm just leave files open in my code, for example when taking screenshots using the WebDriver (and apache commons IO to copy them):
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
File destFile = new File(...);
FileUtils.copyFile(scrFile, destFile);
But the problem persists even after removing the screenshot recording.
Do you guys have any pointers what's wrong here and/or how to tackle this problem?
EDIT:
I've implemented a WebdriverPool, to reuse WebDrivers / browser instances. I use a shutdown hook to close all open instances when the test has finished:
Runtime.getRuntime().addShutdownHook(new Thread(){
#Override
public void run(){
for (WebDriver driver : drivers.values())
driver.close();
if (!driversInUse.isEmpty())
throw new IllegalStateException("There are still drivers in use (" + driversInUse.size() + ")");
}
});
Might this be the problem?