Duplicate Annotations for cucumber in Java BDD framework - java

Below three #Then statement coming from different step definition how to resolve this in Java.
#Then("User selects {string} value")
#Then("User selects {string} and verify the value are Present in the dropdown")
#Then("User selects {string} value under placements")
public void user_selectsValue(String locatorString) throws Throwable {
locatorStr = POC_homePageMenuItems.mainButtonDropdownNewfrmData(locatorString);
elementclick(locatorStr);
}
How to get this issue resolved, in python duplicate #Then with different step definition is allowed. How to resolve this in Java

You could use a regex OR grouping
#Then("User selects {string} (value|value under placements|and verify the value are Present in the dropdown)")
public void user_selectsValue(String locatorString, String option) throws Throwable {
but probably simpler to refactor to a single test phrase if the new option is not used/needed.

I guess you're using an old version of Cucumber. Since Cucumber v5 the step definition annotations are repeatable

Related

Cucumber doesn't find Step Definition when passing a Java Enum type

I followed the example in this README: https://github.com/cucumber/cucumber-expressions#readme , but I must be doing something wrong. The scenario file doesn't find the method in the step definitions.
Line in scenario file:
And I disable identity provider with name "identity provider name"
Step definition and parametertype configuration in step definition file:
#ParameterType("enable|disable")
public State state(String name){
return State.valueOf(name);
}
#And("^I {state} identity provider with name \"([^\"]*)\"$")
public void I_change_enabled_status_of_identity_provider_with_name(State state, final String idpName) {
METHOD BODY
}
You cannot mix up Cucumber expressions ({state} is a syntax of Cucumber expressions) and regular expressions (which are detected by Cucumber by locating leading ^ and trailing $).
So you have to make your choice. Whether to use Cucumber or Regular expressions.
P.S. - With Cucumber expression the following would work for you:
#And("I {state} identity provider with name {string}")

Best way to Automate API (JSON) contract validation

I am trying to find the effective way to automate the API contract validation. Validations should cover
Field Mandatory or Optional
Field length
Field Type
Structure
Values not allowed
We are using Java with RESTAssured. For few APIs, in the past I have developed individual tests for each field & each validation (like below).
Ex: Test Employee Name is Mandatory (Submit an API request with empty Employee Name)
Test Employee Name is String (Submit an API request with number in Employee Name)
Test Employee name field length (Submit an API request with empty Employee Name more than allowed length)
This approach works but I guess there might be effective ways. Looking for suggestions.
I think using parameterized test will solve a part of your problem. The main idea is:
Provide a csv file (or any kinds), each line can contain data test and corresponding error message.
One line = one test.
Example:
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
public class DemoTest {
#ParameterizedTest
#CsvSource({
",name cannot be blank",
"lucas,name cannot less than 6 chars"
})
void test1(String name, String errorMsg){
System.out.printf("name=%s, errorMsg=%s", name, errorMsg);
}
}
The example goes with Junit5, you can do the same with TestNG #DataProvider
What you are referring to is JSON schema , you can create a schema for your api response mentioning which all fields are mandatory and then use it with postman or restassured .
You can generate schema using :
https://www.jsonschema.net/login

Java Cucumber storing value returned by step

i'm writing a cucumber test and i come up with some difficulty:
I have a step which creates dto and saves it using save client which returns dto back again i would need to use that returned dto for other step but don't know how to make it.
Here's how it looks in code :
commonExpenseCreationSteps.java
#Given("^new \"([^\"]*)\" expense with type \"([^"]*)\"$")
public ExpenseDTO newExpense(String description, String expenseType) throws Throwable {
ExpenseDTO expenseDTO = new ExpenseDTO();
expenseDTO.setDefaultPurpose(description);
expenseDTO.setExpenseType(expenseType);
return expenseSaveClient.save(expenseDTO);
}
expenseTransactionsSendSteps.java
#Given("^send expense for Approval$")
public void sendExpenseForApproval() throws InterruptedException {
expenseTransactionSendClient.sendToApproval(expenseDTO);
}
How it would be possible to store value returned by one Step and use it in other one in this case i return ExpenseDTO in newExpense method but i need to use it in sendExpenseForApproval but don't know how to do it !?
Create expenseDTO object outside of your glue code, probably in your stepdef class constructor.
ExpenseDTO expenseDTO = new ExpenseDTO();
The way to share state between steps in the same class is to use instance variables. Set the value in one step and use that value in a later step.
The way to share state between steps with two or more step classes is to use dependency injection.
I wrote a blog post that describes how it can be done using PicoContainer.

jBehave write common step

Can I have the following step method using jBehave;
#When("I select action <actionText> on <panelTitle>")
#Alias("I select action $actionText on $panelTitle")
public void myMethod(#Named("actionText") String actionText, #Named("panelTitle") String panelTitle) {
// My code
}
So as you can see, the step text is the same. Only thing is in one case, the value is through
parameter injection and in other it is through parameterised scenarios
I have seen similar implementation in link:
http://jbehave.org/reference/stable/parametrised-scenarios.html
So, you can use this method.

Write jBehave example step

I want to write a jBehave step as follows;
#When("I perform <someAction> on $panel")
So I can have steps like
i. When I perform Action1 on Panel1
ii. When I perform Action2 on Panel2
Now as you can see, this step has a mix of placeholder
1. someAction which actually comes via meta
2. $panel which is taken from the step text in the story
But this is not working from me and I get NullPointerException
It works if I write
#When("I perform <someAction> on Panel1")
i.e. I cannot use the 2 placeholders in the same step.
But since this is a generic step, I do not want to hard code any values.
Yes you can
#When("I perform <someAction> on *panel*")
public void perform(#Named("panel") String panelId){
}
and from now, I recommend to identify all elements by name, using jemmy you can use a new NameComponentChooser(panelId)
Please use $symbol before both your parameters. Then both the example parameter as well as the normal parameter will be handled.
#When("I perform $action on $panel")
public void performAction(String action, String panel){
}

Categories