1

I have Karate scenario defined as below

Feature: Random Users

  Background: 
    * url 'https://askuser.me'

  @get-user
  Scenario: Get Random User data
    Given path 'api'
    When method get
    Then status 200
    * string json = response
    * def Util = Java.type('com.example.mobiletest.utils.TestUtils')
    * def SaveResponse = Util.writeToJSONFile(json,'randomuser.json')

And is Corresponding Runner class defined as below:

public class RandomUserRunner {
    @Karate.Test
    public Karate testRandomUserRunner(){
        return Karate.run("RandomUser").relativeTo(getClass());
    }
}

I want to execute testRandomUSerRunner() programatically from other java function, how do I do that (reason this is, karate scenario fetches response and saves in json file, other method in java want to reuses these steps)

I tried to call as below but it didnt work:

RandomUserRunner runner = new RandomUserRunner();
runner.testRandomUserRunner();

Anyhelp or pointers would be really appreciated.

Sagar Varule
  • 145
  • 2
  • 9

1 Answers1

0

First - a disclaimer. Karate is not designed for this. It looks like you are already using some Java utils from Karate, so I personally think trying to call Karate from Java is wrong. The JUnit classes exist to take care of reporting, and here also - the parallel Runner is recommended: https://stackoverflow.com/a/65578167/143475

That said, see if the Runner.runFeature() API meets your use case. You will be able to access variables created by the Feature also.

Refer: https://github.com/karatelabs/karate#invoking-feature-files-using-the-java-api

Peter Thomas
  • 54,465
  • 21
  • 84
  • 248
  • 1
    Thanks your shared article helped. I have use-case where I have to : 1) Get data from GET request verify details 2) Use response data from step 1 as test data in UI form and validate the UI functionality. Hence I went into direction API testing with karate and UI test with cucumber+selenium. What are your thoughts on usecases like these as this. What should be the approach according to you. – Sagar Varule Nov 26 '22 at 11:35
  • 1
    @SagarVarule I'm biased ;) but you should consider Karate for UI testing, so you can do everything in one flow and one-syntax. there are many enterprises switching to karate from selenium and cypress, and since we support the webdriver W3C protocol, teams are able to use cloud grids such as lambdatest: https://github.com/karatelabs/karate/tree/master/karate-core – Peter Thomas Nov 26 '22 at 12:00