0

I'm a starter in Cucumber Selenium with Java and have Google'd, but couldn't find if it was possible. This is the issue. I have a method that has a String parameter that I would like to match to a variable name. Then I would like to use that variable for another method.

So in short I would have a parameter String (f.e. "name") And would like that to be matched to the variable 'name' and get/use that value

Something like this.

My cucumber scenario would have a step like this:

Abstract Scenario: check errors in fields
If there is an error in "field"
Then foo

Examples:
|field  |
|name   |
|address|
|phone  |

My method in the step definition would be something like:

private final name = "field_ID_Name"
private final address = "field_ID_Address"
private final phone = "field_ID_Phone"

public void thereIsAnErrorIn(String field){
// Here the match from the argument "name" to use of the variable "name"
checkError(field)
}

private void checkError(String field){
// here I'd like to check based on the value, so in my case for variable
//'name' would get "field_ID_name" as passed parameter
seleniumdriver.pollVisible(field) //field = "field_ID_Name"
}

Now (obviously) it passes on "name" as the string, but I couldn't figure out a way to pass on "field_ID_Name" for this example. I could use a switch statement to check for various possible values of field, but I rather have it match based on the string passed. Another option to put the exact locator as an argument for the first method, but that isn't that easy to read for my customer who has to understand the feature-files (cucumber scenario's).

Any suggestions appreciated!

Blitzoff
  • 133
  • 4
  • 11

2 Answers2

1

You can create a map with variable names as keys and the desired argument to be passed to pollVisible function as the value. So your map would look something like :

myMap : name -> field_ID_Name

Now do:

private void checkError(String field){

seleniumdriver.pollVisible(myMap.get(field));

}

Learner
  • 79
  • 5
0

You can read object field values using Java Reflection API, the easiest way to do that - just use Apache BeanUtils library. Take a look at this similar answered question :

Retrieve field values using BeanUtils

Community
  • 1
  • 1