How to run a java file inside of Gatling script? - java

I am using Gatling for the first time. I have functional tests that are written in java/cucumber. I want to run these functional tests from a Gatling-scala script to do the performance testing of my application. Is there any way to do so?
The idea is to use the existing functional tests and wrap them around gatling scripts so that they could be executed concurrently for multiple users.

What you want to do is to call a Java method from Scala.
Make sure that the method you want to call is available on the class path Scala sees. Then refer to the method you want to call.
This blog post may help you.

If you are using the Gatling for the first time, have you been considering usage of some other performance tools which can provide you such options? As an analog to Gatling for your case (if you want to create functional tests on Java) and run them later using loading tools I would recommend you to check the Locust.
Using Locust you can write the tests using Java or even Kotlin. You can find the handy tutorial by this link:
https://www.blazemeter.com/blog/locust-performance-testing-using-java-and-kotlin
Another preferable option might be to use Taurus framework which allows you to run JUnit/TestNG tests right away:
https://gettaurus.org/docs/JUnit/
https://www.blazemeter.com/blog/navigating-your-first-steps-using-taurus

Gatling is primarily for http testing. what I would do is to call java code from within a gatling test that will return me a value that I check for ex: I return a boolean from a java code below for doing performance test(same also for functional test which needs extending GatlingHttpFunSpec instead of Simulation class). Also will need to use a dummy endpoint (like a health check url which will always return 200).
val myJavaTest: MyJavaTest = new MyJavaTest()
val baseURL="http://localhost:8080"
val endpoint_headers=Map("header1" -> "val1")
val endPoint="/myurl/healthcheck"
setUp(scenario("Scenario ")
.exec(
http("run first test")
.get(endpoint)
.headers(endpoint_headers)
.check(bodyString.transform(str => {
myJavaTest.runTest1()//should return boolean
}).is(true)))
.inject(atOnceUsers(1))).protocols(http
.baseURL(baseURL))

Related

How to collect a coroutine flow in java?

I'm developing an Android library,
When the user receives a push notification it may contain deep links, that I need to return to the app.
I did in kotlin with no problem.
This is the function that is called when it needs to send the deep links
fun getDeepLinkFlow(): Flow<HashMap<String, String>?> = flow {
emit(deepLinks)
}
And in my kotlin test app I managed to use with no problems as well, using like this.
GlobalScope.launch(coroutineContext) {
SDK.getDeepLinkFlow().collect { deepLinks ->
println(deepLinks)
}
}
But now, there's a RN project that whant to use the lib, in order to do that we are doing a RN module that joins the iOS code and the Android code. But it uses java.
So, how can I use the collect from Coroutines on a Java code? or what could I do differently?
Coroutines/Flow is inherently awkward to use from Java since it relies on transformed suspend code to work.
One possible solution is to expose an alternative way of consuming the Flow to your java code. Using the RxJava integration library you can expose a compatible Flowable that the java side can consume.
I've decided to change the way deep links were used and start using the ViewModel with a liveData instead.

Cascading method calls in FitNess?

I'm new to FIT and FitNess and I'm wondering if is possible to cascade method calls without defining special fixtures.
Background: we are testing our web based GUI with Selenium WebDriver. I have created a framework based on the PageObject pattern to decouple the HTML from the page logic. This framework is used in our JUnit tests. The framework is implemented in a Fluent API style with grammar.
Something like this:
boolean connectionTest =
connectionPage
.databaseHost( "localhost" )
.databaseName( "SOME-NAME" )
.instanceNameConnection()
.instanceName("SOME-INSTANCE-NAME")
.windowsAuthentication()
.apply()
.testConnection();
Some testers want to create acceptance tests but aren't developers. So had a look to FIT. Would it be possible to use my framework with FIT as is without developing special fixtures?
I don't believe you can use the existing code with 'plain-vanilla' Fit, it would at least require a special fixture class to be defined. Maybe 'SystemUnderTest' could help?
Otherwise Slim's version might be something to get it to work for you.
As a side note: I've put a FitNesse baseline installation including features to do website testing with (almost) no Java code on GitHub. In my experience it's BrowserTest will allow non-developers to create/modify/maintain tests easily, and integrate those tests with you continuous integration process (if you have one). I would suggest you (or your testers) also have a look at that.
I know you asked about Java but in case any .NET developers see this, it's possible with the .NET implementation, fitSharp:
|with|new|connection page|
|with|database host|localhost|
|with|database name|some-name|
etc.
See http://fitsharp.github.io/Fit/WithKeyword.html
I have solved my problem by writing a generic fixture which receives the target methods and their arguments from the fitness table and uses Java reflection to invoke the appropriate framework methods.
So I have one fixture of all different page objects that are returned from the framework.

Changing the behaviour of a Java app dynamically using Groovy

I am investigating methods of dynamically modifying the behaviour of a Java application (specifically, I'm trying to make a Minecraft mod that allows users to modify the behaviour of the objects they find by writing code without the need to restart the game) and I stumbled upon Groovy. My question is: is it possible to integrate Java and Groovy in such way they "share" objects? (I'm thinking about having a specific set of classes that are actually Groovy code so you can change the code during runtime, similarly to what you can do in any Smalltalk implementation)
Take a look at Integrating Groovy in a Java Application. It shows examples of how you can run a Groovy script from inside a Java application and share data between them using groovy.lang.Binding.
What a cool idea!
1. Groovy: Java and Groovy can share objects and call back and forth. Groovy classes that implement Java interfaces are easily called from Java. (There are other ways, like calling groovyObject.invokeMethod("methodName", args) from Java.) Of JVM languages, Groovy has the tightest integration with Java. It's also easy for Java programmers to learn since it shares so much with Java.
The book Groovy in Action has a chapter on "Integrating Groovy" that explains and compares the approaches (in more detail than the reference docs do): GroovyShell, GroovyScriptEngine, GroovyClassLoader, Spring integration, and JSR-223 ScriptEngineManager. GroovyClassLoader is the most capable choice.
However, while it's easy to compile and load Groovy code at runtime, I'm puzzled about how to change behavior of existing object instances (short of the notes below on hot swapping). (It might depend on whether the class overrides a Java interface or subclasses a Java class.) Consider:
class G implements Runnable {
void run() { println 'Groovy' }
}
g = new G()
g.run()
This prints Groovy. Now redefine the class:
class G implements Runnable {
void run() { println 'Groovy!' }
}
g1 = new G()
g.run()
g1.run()
This prints
Groovy
Groovy!
Now use the meta-class to change methods at runtime:
G.metaClass.run = { println 'Groovy!!!' }
g2 = new G()
g.run()
g1.run()
g2.run()
This prints
Groovy
Groovy!
Groovy!
If we omitted implements Runnable from those class definitions, then the last step would instead print
Groovy
Groovy!
Groovy!!!
But with our class that does implement Runnable, now do:
G.metaClass.run = { println 'Very Groovy!!!' }
g3 = new G()
g.run()
g1.run()
g2.run()
g3.run()
this prints:
Groovy
Groovy!
Very Groovy!!!
Very Groovy!!!
A workaround would implement the methods in closures held in class variables.
2. Hot Swapping: If the main point is to redefine method bodies at run time for classes with existing instances, then you can simply run within an IDE's debugger and use hot swapping.
E.g. for IntelliJ, here are the instructions to configure hot swapping of Java and Groovy code.
3. Expanded Hot Swapping: If you also want to be able to add/remove methods and instance variables at run time, then see this JetBrains article on extended hot swapping via DCEVM (Dynamic Code Evolution VM).
See Hot Swap code code at https://github.com/HotswapProjects
Also see this SO Q&A on hot swapping techniques.
I'm not sure that's something you can accomplish with Groovy without compiling it. You could do it, but the "scripting" aspect of Groovy won't help you. I'd look into having the player write javascript and using Java's ScriptEngine. See here: http://docs.oracle.com/javase/7/docs/technotes/guides/scripting/programmer_guide/
Yes, You can achieve that. For example, You have something written in java that uses some objects from let's say spring context. So now what u can do is :
execute groovy script before that java code is executed,
use delegate design pattern to wrap it, overwrite some methods
finaly put it back into context.
So basicly in moment where Your java code is executed, he'll get a wrapped object with some changes made in runtime.
If that's what are You trying to do, let me know i could write You some example code.

Jmeter Multiple SampleResult for AbstractJavaSamplerClient

I wrote a custom Java Request which extends the AbstractJavaSamplerClient to measure the performance of a JAVA API invocation. However, now i need to measure the performance for a multiple API which is part of the same use case.
i.e.
Server severInst = new Server();
severInst.api1();
severInst.api2();
severInst.api3();
Need to get the metrics in Jmeter for each API invocation (api1, api2, api3). However, I cannot split those API calls since the api2 call is dependent on api1. (same for api3 which depends on api2). If i could split then I can write a different "Java Sampler Client" for each API. Since all these apis are inter-dependent i have to invoke all of them at once.
The method runTest returns only one SampleResult. However, I am in need of a situation where I need to return the multiple SampleResult. I tried the SampleResult.setParent() and SampleResult.storeSubResult() but no luck.
Any pointer on this will be helpful?
Thanks
How about creating three different tests. Each one collects the time for the required apis. So, in test 1 you'd have:
startTiming();
api1();
api2();
api3();
completeSample();
Then in the second test:
api1();
startTiming();
api2();
api3();
completeSample();
and so on.

Call javascript function from Java (Groovy) class

I have a javascript function (very big one!) that I need its functionality in a Java (Groovy) class. It is a simple calendar converter. I can rewrite it in groovy but just want to know if it is possible to call javascript function from a java (groovy) method? I guess functional testing libraries like selenium and Canoo should have something like this, am I right?
PS: I don't want to wake up a real-world browser in order to use its JS runtime env.
Thanks,
As mentioned in the other answers, it is possible to use the Scripting API provided as part of the javax.script package, available from Java 6.
The following is a Groovy example which executes a little bit of Javascript:
import javax.script.*
manager = new ScriptEngineManager()
engine = manager.getEngineByName("JavaScript")
javascriptString = """
obj = {"value" : 42}
print(obj["value"])
"""
engine.eval(javascriptString) // prints 42
It is not necessary to call a browser to execute Javascript when using the Scripting API, but one should keep in mind that browser-specific features (probably the DOM-related functionalities) will not be available.
You can use Rhino, an implementation of JavaScript language in Java. Here is example of calling JavaScript function from java, but you can do it from groovy also.

Categories

Resources