Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to write gatling tests in Java #2168

Closed
ptrthomas opened this issue Nov 2, 2022 · 23 comments
Closed

Add option to write gatling tests in Java #2168

ptrthomas opened this issue Nov 2, 2022 · 23 comments
Assignees
Milestone

Comments

@ptrthomas
Copy link
Member

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

avoiding the need for Scala would be a great improvement

@almohankumar
Copy link

@ptrthomas This would be an excellent improvement.

@dve
Copy link

dve commented Jun 6, 2023

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?

@f-delahaye
Copy link
Contributor

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)
);

@ptrthomas
Copy link
Member Author

@f-delahaye yes that sounds good, go ahead

@f-delahaye
Copy link
Contributor

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
Copy link
Contributor

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 added a commit to f-delahaye/karate that referenced this issue Nov 4, 2023
f-delahaye added a commit to f-delahaye/karate that referenced this issue Nov 4, 2023
f-delahaye added a commit to f-delahaye/karate that referenced this issue Nov 4, 2023
@ptrthomas ptrthomas added this to the 1.5.0 milestone Nov 4, 2023
@ptrthomas
Copy link
Member Author

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

@ptrthomas
Copy link
Member Author

@ptrthomas
Copy link
Member Author

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

@borzykin
Copy link

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
Copy link

borzykin commented Feb 23, 2024

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),

@ptrthomas
Copy link
Member Author

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

@f-delahaye
Copy link
Contributor

Hi, I will take a look asap.

@borzykin
Copy link

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 -_-

@f-delahaye
Copy link
Contributor

f-delahaye commented Feb 26, 2024

@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
Copy link
Contributor

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:
src/test/java/${gatlingConfig}

but then could you please give a bit more details?

Many thanks

@borzykin
Copy link

@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

@f-delahaye f-delahaye mentioned this issue Mar 6, 2024
5 tasks
@f-delahaye
Copy link
Contributor

@borzykin PR for new fluent api: #2525

@borzykin
Copy link

borzykin commented Mar 6, 2024

@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

@f-delahaye
Copy link
Contributor

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.

@fingerdancer
Copy link

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.

@ptrthomas
Copy link
Member Author

@ptrthomas
Copy link
Member Author

1.5.0 released

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants