Hot questions for Using Cucumber in web services

Top Java Programmings / Cucumber / web services

Question:

As I'm trying to automate the API testing process, have to pass the XML file to Read method for example,

Given request read ( varXmlFile )

FYI: XML file is present in the same folder where the feature file exists.

Doing this, its throwing an exception like this

com.intuit.karate.exception.KarateException: called: D:\workspace\APIAutomationDemo\target\test-classes\com\org\features\rci_api_testing.feature, scenario: Get Membership Details, line: 15
javascript evaluation failed: read (varXmlFile )

So Karate doesn't allow this way or can we have any other alternative ?

Suggestion please.

Thanks


Answer:

Please ensure the variable is set:

* def varXmlFile = 'some-xml-file.xml'
Given request read(varXmlFile)

Or just use normally:

Given request read('some-xml-file.xml')

Question:

I have read some articles regarding how we can implement BDD with cuuumber but i am not able to understand fully.

Lets say i have a service

/v1/picture?viewer=1&viewed=2&flag=1

For these 3 input i am looking for output as "1.jpg"

I am looking for

  1. How can we implement above ? -- i.e output based on input
  2. How can we build data ? -- mysql data basically stubs

Please help with examples and good links. Thanks


Answer:

It's hard to answer your question as there's not a lot of detail about what you're trying to achieve, but I'll give it a go.

You've got 3 different inputs there: - The viewer - Whether it's been viewed or not - A flag.

So those inputs provide different contexts (Givens) for the scenarios.

  • Given a .jpg viewer
  • Given I have already viewed the image twice
  • Given the image has been flagged

How you set up the data for these givens is entirely up to you. You can hard-code it, use mysql data or input real data using an admin console, etc.. It shouldn't matter. The behaviour you're interested in is what happens when you apply the event (When):

  • When I retrieve the v1 picture

Presumably, the input provided in your contexts is relevant, and will provide different outcomes (Thens) depending on that input.

  • Then I should see a .jpg file
  • Then I should see the "Image flagged" banner
  • Then I should see the message "Viewed 2 times"

So I might have scenarios like:

Given a .jpg viewer  
And an image that's been viewed 3 times  
When I retrieve the v1 picture  
Then the service should give me 1.jpg  
And I should see the message "Viewed 3 times"  

Given a .png viewer  
When I retrieve the v1 picture  
Then the service should give me 1.png

etc.

I don't know what your real service is doing, but hopefully this helps you see the pattern. Try to keep different aspects of the behaviour separated in the scenarios (more like the 2nd than the 1st) unless there are combinations you need to exemplify.

Question:

If i have Cucumber scenario like this :

Scenario: Correct response should be returned.
    When User sent "xml example" request
    Then Response should have some data "

And for this first step in ma TestStep class :

@When("^User sent \"(.*)\" request")
    public void sendRequest(String requestName) throws Exception
    {
       //code for SOAP request 
    }

If service is not available, test will run for hours. I would like to stop it after some period of time. Any suggestions?


Answer:

If your lib is not terminating request after provided timeout, you can use alternative approach with ExecutorService. The main idea is submit task with request to SOAP service to ExecutorService and then call get(time, TimeUnit.SECONDS) with timeout from Future.

Example:

Future<String> future = executor.submit(new SoapRequeset());
future.get(5L, TimeUnit.SECOND); //will throw exception if result not available after timeout