Reputation: 321
I need to automate some webservices, i create some methods for that and i want to use Cucumber for that but i can't figure how to use returned value in next step.
So, i have this Feature:
Feature: Create Client and place order
Scenario: Syntax
Given I create client type: "66"
And I create for client: "OUTPUTVALUEfromGiven" an account type "123"
And I create for client: "OUTPUTVALUEfromGiven" an account type "321"
And I want to place order for: "outputvalueFromAnd1"
and i have this Steps:
public class CreateClientSteps {
@Given("^I create client type: \"([^\"]*)\"$")
public static String iCreateClient(String clientType) {
String clientID = "";
System.out.println(clientType);
try {
clientID = util.createClient(clientType);
} catch (IOException e) {
e.printStackTrace();
}
return clientID;
}
@And("^I create for client: \"([^\"]*)\" an account type \"([^\"]*)\"$")
public static String createAccount(String clientID, String accountType) {
String orderID = "";
try {
orderID = util.createAccount(clientID,accountType);
} catch (IOException e) {
e.printStackTrace();
}
return orderID;
}
}
It's any way to use the returned values from step to step?
Thank you!
Upvotes: 4
Views: 17536
Reputation: 321
I solved in other way, i know the question it's from last year but maybe someone will find this usefull in the future.
So, i created a 'ClientsMap.java' where i store the outputs from last sentences. EX:
public class ClientsMap {
private static ArrayListMultimap<Integer, String> multimapCardNumber = ArrayListMultimap.create();
...
public static void addCardNumber(String cardNumberValue) {
multimapCardNumber.put(multimapCardNumber.size(), cardNumberValue);
}
public static String returnSpecificCardNumber(int specificCardNumberPosition) {
String returnedCardNumber = multimapCardNumber.get(specificCardNumberPosition - 1).get(0);
return returnedCardNumber;
}
}
Then i created some specific keywords to be used in sentences like:
And I want to make a card transaction with this parameters:
| XXX | 9999 |
| accountNumber | account1-client1 |
| cardNumber | cardNumber1 |
Then i have a method behind who check for keywords like 'cardNumber' and retrieve position of card like this:
if (paramsList[i].startsWith("cardNumber")) {
String cardNumberPosition = paramsList[i].replaceAll("[^0-9]", "");
paramsList[i] = returnSpecificCardNumber(Integer.valueOf(cardNumberPosition));
}
And after each Scenario don't forget to delete map.
Upvotes: 3
Reputation: 4323
Sharing state between steps, which is how I interpret your question, is not done by examining a returned value. It is done by setting the value in an instance variable and later read that instance variable in another step.
I would change your steps to this in order to achieve that:
public class CreateClientSteps {
private String clientID;
private String orderID;
@Given("^I create client type: \"([^\"]*)\"$")
public void iCreateClient(String clientType) {
System.out.println(clientType);
try {
clientID = util.createClient(clientType);
} catch (IOException e) {
e.printStackTrace();
}
}
@And("^I create for client: \"([^\"]*)\" an account type \"([^\"]*)\"$")
public void createAccount(String clientID, String accountType) {
try {
orderID = util.createAccount(clientID, accountType);
} catch (IOException e) {
e.printStackTrace();
}
}
}
The things I changed are
This is how you share state between steps in the same class. Sharing state between steps in different classes can also be done. It is a bit more complicated. Ask if you are interested.
Upvotes: 6