How can Use My own created Java jars in jmeter webdriver sampler? - java

I have some selenium code which repeats in every JMX I have, how can I create a jar of that and use it in JMeter web driver sampler? I am using Beanshell language in web driver sampler.
For example, if I need to use login and logout in every Jmx of web driver sampler, and now I am repeating it in every JMX. How do I keep those login and logout script somewhere and use that. Keeping as jars would be fine, but how can I do it in jMeter?

Given the .jar containing your functions to perform login/logout will be present in JMeter Classpath you should be able to normally using import statement or equivalent call your functions from the WebDriver Sampler code.
One point: don't use java language as it is not real Java, it is Beanshell interpreter which has limited support of Java features and not very good performance. Since JMeter 3.1 it is recommended to use groovy for any scripting purposes so consider migrating on next available opportunity. Most likely you won't have to change your code.
Also be aware that there is a built-in mechanism in JMeter which helps you to avoid code duplication: Module Controller so instead of having .jars you can have separate WebDriver Sampler instances which will be doing common tasks like login/logout and you will be able to call them via Module Controller where required.

Related

load test gui with jmeter and selenium webdriver

I created a java wrapper to feed jmeter. I have implemented java classes with selenium that are invoked by the wrapper and perform GUI tests.
I activated the headless option.
launching tests with a single user from jmeter all works correctly.
trying to launch two users tests fail.
can you help me to understand why?
Most probably you missed an important bit: each Selenium session need to have a separate URL and Selenium server needs to be running on a different port. So make sure to amend your "wrapper" to be aware of multiple WebDriver instances and to kick off a separate instance of Selenium server (or standalone client) for each JMeter thread (virtual user).
Unfortunately we cannot help further without seeing your code, just keep in mind that your wrapper needs to be thread-safe. Also pay attention to jmeter.log file - normally it should contain enough information to get to the bottom of your test failure.
P.S. Are you aware of WebDriver Sampler plugin? It's designed in line with JMeter threads model and you should be able to kick off as many browsers as your machine can handle. If you for some reason it doesn't fit your needs you can at least take a look into the source code to get an idea with regards to what you need to change in your "wrapper"

Execute java code with spring dependencies before JMeter test

I'm trying to do some stress tests on an API with JMeter. I have two environments (QA and production) and I want to set up QA's database before running JMeter tests.
I can't use JDBC or MongoDB configuration elements because it's a cloud database (DynamoDB - AmazonWS). I thought about using raw requests with API-token to AmazonWS's API but I'd prefer to use a Java class I already have (a class that create-delete queries to cloud DB) but it has Spring dependencies.
I know that with JMeter I can run some Java code but I don't know how to run classes with Spring dependencies like a BeanFactoryPostProcessor.
Any ideas?
I believe the best option would be to use
setUp Thread Group with 1 virtual user and 1 iteration
OS Process Sampler to execute your class (see How to execute a java .class from the command line for example)
Running class from JMeter using i.e. JSR223 Sampler is also possible but you will need to
have all the dependencies in JMeter's classpath
ensure that there are no conflicts between JMeter and your class dependency libraries

Can I open a REPL in the context of a Spring Web Application?

I'm working on a web application based on Spring MVC and Hibernate on Tomcat 8 (both for deployment and local development). The IDE is Spring Tool Suite (which is based on Eclipse).
I want to open a REPL (read-eval-print-loop, like Groovy's, Python's, Ruby's, etc) in the context of my application (while it's running on Tomcat locally), to speed up development by shortening the code -> test feedback loop.
That is, I want to be able to open a shell in the command line (or inside Eclipse) and do something like:
ClientDAO clientDAO = getAutowiredDAOFromSpringSomehow();
Client client = clientDAO.findByID(100);
client.setName("Another name");
clientDAO.save(client);
I can work around this a bit by setting a breakpoint somewhere in a controller and use Eclipse's debugger Display tab to execute arbitrary code, but this is a bit impractical and uncomfortable.
I'm open to using Groovy or Scala's shell if it's more convenient (I obviously still need access to my objects, though).
What are my options?
You may be able to do it using Spring Shell, JShell or BeanShell
Here's a project to embed a repl in an android app using BeanShell
I don't know if it's useful for your use-case but theoretically it should be possible to do this using CRaSH. It's a Shell like Bash on Linux but for your Java-Application and it's possible to create your own commands.

How to use existing Java API tests inside Jmeter

We have existing Java tests that singularly tests our back end. These tests are pretty elaborate, and also run as a single users. I would like to know if I can simply take these existing tests/classes/libraries/jars etc and just wrap JMeter around them to execute them as JMeter tests from the command line (i.e. Maven).
Maybe add in some listeners and other JMeter components, but the tests are perfect the way they are except that they are not multi-threaded and do not have the reporting functions that JMeter has.
Can this be done using JSR233?
What if my libraries are located elsewhere? How can I use them in the JMeter project?
You have at least 3 options:
Implement JavaSamplerClient by extending AbstractJavaSamplerClient, this class will call your class. Create a Jar from this and put it in jmeter/lib/ext + add dependencies to jmeter/lib folder and you can then use Java Request and select your class.
Use JSR223 Sampler + Groovy wrapper for your class
Use JUnit Sampler if you have some JUnit classes

Selenium Grid Execution using my own framework

I have developed a micro-framework in Java which does the following function:
All test cases list will be in a MS-Access database along with test data for the Application to be tested
I have created multiple classes and each having multiple methods with-in them. Each of these methods represent a test-case.
My framework will read the list of test cases marked for execution from Access and dynamically decide which class/method to execute based on reflection.
The framework has methods for sendkeys, click and all other generic methods. It takes care of reporting in Excel.
All this works fine without any issue.
Now I am looking to run the test cases across multiple machines using Grid. I read in many sites that we need a framework like TestNG to have this in Grid. But I hope that it could be possible to integrate Grid in my own framework. I have read many articles and e-books which does not explain the coding logic for this.
I will be using only windows 7 with IE. I don't need cross browser/os testing.
I can make any changes to the framework to accomplish this. So please feel free.
In the Access DB which I mentioned above, I will have details about test case and the machine in which the test case should run. Currently users can select the test cases they want to run locally in the Access DB and run it.
How will my methods(test scripts) know which machine its going to be executed? What kind of code changes I should do apart from using RemoteWebDriver and Capabilities?
Please let me know if you need any more information on my code or have any question. Aslo kindly correct me if any of my understanding on Grid is wrong.
How will my methods know which machine it is going to be executed? - You just need to know one machine with a grid setup - the ip of your hub machine. The hub machine will decide where to send the request to from the nodes that are registered with, depending upon the capabilities you specify while instantiating the driver. When you initialize the RemoteWebDriver instance, you need to specify the host (ip of your hub). I would suggest to keep the hub ip as a configurable property.
The real use of the grid is for parallel remote execution. So how do you make your tests run in parallel is a thing that you need to decide. You can use a framework like Testng which provides parallelism with simple settings. You might need to restructure your tests to accomodate testng. The other option would be to implement multithreading yourself to trigger your tests in parallel. I would recommend testng based on my experience since it provides many more capabilities apart from parallelism. You need to take care that each instance of driver is specific to your thread and not a global variable.
All tests can hit the hub and the hub can take care of the rest.
It is important to remember that Grid does not execute your tests in parallel for you. It is the job of your framework to divide tests across multiple threads and collate the results . It is also key to realise that when running on Grid, the test script still executes in the machine the test was started on. Grid provides a REST API to open and interact with browsers, so your test will be using this rather than opening a browser locally. Any other non-selenium code will be executed within the context of the original machine not machine where the browser has been opened (e.g. File System access is not where the browser has opened). Any use of static classes and globals in your framework may also cause issues as each test will acces these concurrently. Your code must be thread safe.
Hopefully this hasn't put you off using Grid. It is an awesome tool and really easy to use. It is the parallel execute which is hard and frameworks such as TestNG provide this out of the box.
Good luck with your framework.

Categories

Resources