karate icon indicating copy to clipboard operation
karate copied to clipboard

Add option to write gatling tests in Java

Open ptrthomas opened this issue 2 years ago • 22 comments

refer: https://gatling.io/docs/gatling/reference/current/whats_new/3.7/

avoiding the need for Scala would be a great improvement

ptrthomas avatar Nov 02 '22 17:11 ptrthomas

@ptrthomas This would be an excellent improvement.

almohankumar avatar Nov 03 '22 11:11 almohankumar

I would be really happy to see the java/gatling integration in karate. My naive tries to create the needed "bridge" parts failed because of my (not existing) Scala knowledge. Is there someone with Scala knowledge how can show me the way?

dve avatar Jun 06 '23 10:06 dve

Hello,

I can take a look at this one, if it's ok...

Scala's concise syntax will not be easy to replace, but maybe we could try to define some kind of dsl instead?

For example:

  protocol = karateProtocol(
    "/cats/{id}" -> Nil,
    "/cats" -> pauseFor("get" -> 15, "post" -> 25)
  )

would be replaced by:

protocol = karateProtocol(patterns()
      .uri("/cats/{id}").nil()
      .uri("/cats").pauseFor("get", 15)
                     .pauseFor("post", 25)
);

f-delahaye avatar Oct 31 '23 13:10 f-delahaye

@f-delahaye yes that sounds good, go ahead

ptrthomas avatar Oct 31 '23 14:10 ptrthomas

I can't get karate-gatling to compile on develop (jdk17)...

[INFO] compiling 2 Scala sources and 1 Java source to D:\dev\karate\karate-gatling\target\test-classes ... [ERROR] D:\dev\karate\karate-gatling\src\test\scala\mock\MockUtils.java:3:24: error: package com.intuit.karate does not exist [ERROR] D:\dev\karate\karate-gatling\src\test\scala\mock\MockUtils.java:4:29: error: package com.intuit.karate.core does not exist [ERROR] D:\dev\karate\karate-gatling\src\test\scala\mock\MockUtils.java:24:69: error: cannot find symbol [ERROR] D:\dev\karate\karate-gatling\src\test\scala\mock\MockUtils.java:20:8: error: cannot find symbol [ERROR] D:\dev\karate\karate-gatling\src\test\scala\mock\MockUtils.java:20:28: error: cannot find symbol

Can't figure out what the problem is, karate-core compiles and is imported as a dependency...

Someone else have the same issue?

f-delahaye avatar Nov 03 '23 21:11 f-delahaye

Never mind. There was a 'é' in my local repository's path. I moved it to a different folder and it's compiling again.

f-delahaye avatar Nov 04 '23 07:11 f-delahaye

for those interested in trying this, 1.5.0.RC2 has been released

ptrthomas avatar Dec 01 '23 10:12 ptrthomas

posted on social media

LinkedIn: https://www.linkedin.com/feed/update/urn:li:activity:7137691423725780992 Twitter: https://twitter.com/getkarate/status/1731926887595913593

ptrthomas avatar Dec 05 '23 06:12 ptrthomas

1.5.0.RC3 is available, also the "karate-todo" example has been updated to use this approach: https://github.com/karatelabs/karate-todo/blob/d7d47ecd824904f51faaa826ef163ba92f32607a/src/test/java/app/perf/TodoSimulation.java

ptrthomas avatar Feb 01 '24 07:02 ptrthomas

hi @ptrthomas, I am trying to rewrite the scala simulation file into java one and it looks like it doesn't pick urls and pathes that I defined in karate-config.js. I am getting ReferenceError: "serverUrl" is not defined. Path to config is set via plugin configuration like: <configFolder>src/test/java/${gatlingConfig}</configFolder>

Would appreciate the help, thanks

borzykin avatar Feb 23 '24 14:02 borzykin

Also did I understand from the code correctly, that setting call names is not supported yet for Java DSL? * header karate-name = 'Initiate Login flow'

karateProtocol(
                uri("Initiate Login flow").pauseFor("get", TIMEOUT_1000),

borzykin avatar Feb 23 '24 15:02 borzykin

will let @f-delahaye answer. I was able to get the karate-todo example to work as I commented above

ptrthomas avatar Feb 23 '24 16:02 ptrthomas

Hi, I will take a look asap.

f-delahaye avatar Feb 24 '24 11:02 f-delahaye

As a workaround for "serverUrl" is not defined I am reading karate-config.js into variable "*def config = ..." instide .feature file and then referencing it, so not a huge deal, but being able to remap call names from "POST /bla/bla" into meaningfull names with header karate-name very desirable feature to have it back -_-

borzykin avatar Feb 25 '24 21:02 borzykin

@borzykin Maybe what you're looking for is nameResolver ?

It is still available but you may need to change your code to:

        KarateProtocolBuilder protocolBuilder = karateProtocol(uri("/bla/bla").pauseFor("get", 1000));
          protocolBuilder.nameResolver = (req, ctx) -> req.getHeader("karate-name");
          KarateProtocol protocol = protocolBuilder.protocol();

If you're using it in a different way, could you please share an example?

Anyway, I will raise a PR for a more fluent api, something like:

        KarateProtocol protocol = 
             karateProtocol(uri("/bla/bla")
                                     .pauseFor("get", 1000)
                                     .nameResolver = (req, ctx) -> req.getHeader("karate-name"))
            protocol();

f-delahaye avatar Feb 26 '24 10:02 f-delahaye

As a workaround for "serverUrl" is not defined I am reading karate-config.js into variable "*def config = ..." instide .feature file and then referencing it, so not a huge deal, but being able to remap call names from "POST /bla/bla" into meaningfull names with header karate-name very desirable feature to have it back -_-

Good to hear you found a workaround!

However, do you have any reason to believe this "serverUrl" is not defined is caused by the gatling java dsl? As far as I know, karate-config.js is loaded by the Karate core engine.

Unless it's something to do with what you said in your initial post

Path to config is set via plugin configuration like: <configFolder>src/test/java/${gatlingConfig}</configFolder>

but then could you please give a bit more details?

Many thanks

f-delahaye avatar Feb 26 '24 10:02 f-delahaye

@f-delahaye changing the code to your example fixed the issues with the name resolver, thanks a lot as for URL issue I will play with it a bit more to find more details

borzykin avatar Feb 26 '24 12:02 borzykin

@borzykin PR for new fluent api: https://github.com/karatelabs/karate/pull/2525

f-delahaye avatar Mar 06 '24 20:03 f-delahaye

@f-delahaye @ptrthomas I am not sure that problem is due to gatliung DSL via Java, just trying to point out problem for people who seems to care enough to try to understand :) As fo URL problem, I cloned example project, and in the simple.feature file when I comment out this line:

* def urlBase = karate.properties['url.base'] || karate.get('urlBase', 'http://localhost:8080')

and just hardcode let urlBase = "http://localhost:8080"; in karate-config.perf.js = I am start getting same error that URL is not defined, even though it is difened in config.

If I add this line to the simple.feature:

* call read("classpath:../karate-config-perf.js")

It start to work fine again. I am not competent enough to say it is correct behaviour or not, just pointing out that used to work on 1.4.1 version without this hacks

borzykin avatar Mar 06 '24 20:03 borzykin

I am start getting same error that URL is not defined, even though it is difened in config.

That makes sense. As far as I know, karate-config-perf.js is not read by default unless the vm arg -Dkarate.env=perf is specified.

Anyway, back to your initial problem, which probably has nothing to do with the karate-todo example or karate-config-perf.js ;) Please create another issue with a reproduceable example as explained here and someone will take a look. And please don't forget the last step (provide the exact command to run and reproduce the problem locally) which might be helpful in your case.

f-delahaye avatar Mar 07 '24 18:03 f-delahaye

Hello @ptrthomas , is there an java example for this feature ? I tried many ways but still not work. I want to use java to write gatling test.

fingerdancer avatar Mar 13 '24 08:03 fingerdancer

@fingerdancer https://github.com/karatelabs/karate-todo/blob/d7d47ecd824904f51faaa826ef163ba92f32607a/src/test/java/app/perf/TodoSimulation.java

ptrthomas avatar Mar 13 '24 12:03 ptrthomas

1.5.0 released

ptrthomas avatar Aug 05 '24 15:08 ptrthomas