Skip to content

Commit

Permalink
introducing the table keyword that converts a cucumber data-table int…
Browse files Browse the repository at this point in the history
…o json, which makes the call feature in loop based data-driven testing even more elegant - which fixes OrkoHunter#11
  • Loading branch information
ptrthomas committed Mar 10, 2017
1 parent a5c5940 commit d71e2de
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 2 deletions.
4 changes: 2 additions & 2 deletions karate-core/src/main/java/com/intuit/karate/Script.java
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ public static void evalXmlEmbeddedExpressions(Node node, ScriptContext context)
evalXmlEmbeddedExpressions(child, context);
}
}
}
}

public static void assign(String name, String exp, ScriptContext context) {
name = StringUtils.trim(name);
Expand All @@ -401,7 +401,7 @@ public static void assign(String name, String exp, ScriptContext context) {
logger.trace("assigning {} = {} evaluated to {}", name, exp, sv);
context.vars.put(name, sv);
}

public static boolean isQuoted(String exp) {
return exp.startsWith("'") || exp.startsWith("\"");
}
Expand Down
13 changes: 13 additions & 0 deletions karate-core/src/main/java/com/intuit/karate/StepDefs.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.intuit.karate.ScriptValue.Type;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import cucumber.api.DataTable;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
Expand All @@ -41,6 +42,7 @@
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.NewCookie;
import javax.ws.rs.core.Response;
import org.apache.commons.lang3.StringUtils;
import org.glassfish.jersey.media.multipart.BodyPart;
import org.glassfish.jersey.media.multipart.FormDataBodyPart;
import org.glassfish.jersey.media.multipart.MultiPart;
Expand Down Expand Up @@ -177,6 +179,17 @@ public void defDocString(String name, String expression) {
public void def(String name, String expression) {
Script.assign(name, expression, context);
}

private static DocumentContext toJson(DataTable table) {
return JsonPath.parse(table.asMaps(String.class, Object.class));
}

@When("^table (.+) =$")
public void table(String name, DataTable table) {
DocumentContext doc = toJson(table);
name = StringUtils.trim(name);
context.vars.put(name, doc);
}

@When("^assert (.+)")
public void asssertBoolean(String expression) {
Expand Down
1 change: 1 addition & 0 deletions karate-demo/src/test/java/demo/callarray/main.feature
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Feature: calling another feature file in a loop

Background:
* url demoBaseUrl

* def creator = read('kitten-create.feature')
* def kittens = read('kittens.json')
* def result = call creator kittens
Expand Down
13 changes: 13 additions & 0 deletions karate-demo/src/test/java/demo/calltable/CallTableRunner.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package demo.calltable;

import cucumber.api.CucumberOptions;
import demo.TestBase;

/**
*
* @author pthomas3
*/
@CucumberOptions(features = "classpath:demo/calltable/main.feature")
public class CallTableRunner extends TestBase {

}
16 changes: 16 additions & 0 deletions karate-demo/src/test/java/demo/calltable/kitten-create.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@ignore
Feature: re-usable feature to create a single cat

Scenario:

Given url demoBaseUrl
And path 'cats'
And request { name: '#(name)' }
When method post
Then status 200






39 changes: 39 additions & 0 deletions karate-demo/src/test/java/demo/calltable/main.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
Feature: calling another feature file in a loop

Background:
* url demoBaseUrl

* def creator = read('kitten-create.feature')

* table kittens =
| name | age |
| Bob | 2 |
| Wild | 1 |
| Nyan | 3 |
| Keyboard | 5 |
| LOL | 3 |
| Ceiling | 2 |

* def result = call creator kittens

* def created = get result[*].response

Scenario: create parent cat using kittens

# create mom cat
Given path 'cats'
And request { name: 'Billie', kittens: '#(created)' }
When method post
Then status 200
And def billie = response

# get kittens for billie
Given path 'cats', billie.id, 'kittens'
When method get
Then status 200
And match each response == { id: '#number', name: '#string' }
And match response[*].name contains ['LOL', 'Nyan']
And assert response.length == 6



Original file line number Diff line number Diff line change
Expand Up @@ -344,3 +344,11 @@ Then match pdf == read('test.pdf')
* def currentBalance = agencyAccount.liabilityAccount.currentBalance
* print 'current balance: ' + currentBalance

# data tables to json
* table cats =
| name | age |
| Bob | 2 |
| Wild | 4 |
| Nyan | 3 |

* match cats == [{name: 'Bob', age: 2}, {name: 'Wild', age: 4}, {name: 'Nyan', age: 3}]

0 comments on commit d71e2de

Please sign in to comment.