我在 Cucumber 中有以下场景:
Scenario: As sysadmin I delete an user used for testing
Given I in selfservice at the page Manage user
When I search for an existing user
| users | ORGADMINUSER_LASTNAME |
| users | ORGADMINUSER_LASTNAME |
字符串 ORGADMINUSER_LASTNAME 是我的 Java 类中的一个变量,它在执行场景时包含一个值。如何将此字符串转换为具有该名称的变量的值?
这是 Java 中的步骤定义:
@When("^I search for an existing user and delete it$")
public void i_search_for_an_existing_user_and_delete_it(DataTable table) throws Throwable {
// I get the list of users from the Data table and then pass them through the web elements in manage users
Thread.sleep(5000);
List<List<String>> user =table.raw();
System.out.println("----------------------------------------------");
System.out.println("Number of users in Feature file: " + user.size());
try{
for (int i = 0; i < user.size(); i++) {
List<String> org= user.get(i);
String Userstring=user.get(i).get(1).toString();
System.out.println("----------------------------------------------");
System.out.println(Userstring);
最佳答案
最简单的方法是简单地检查用户值是否为 ORGADMINUSER_LASTNAME
,然后简单地对该变量进行 get 调用,并将 Userstring
替换为该变量。
否则,如果您想要更通用的东西,您可能需要研究一下 Reflection API。就像是:
ClassOfTheField.getField(Userstring).get(null)
首先使用 getFields()
确保 Userstring 字段存在于 ClassOfTheField
中,否则您将收到 NoSuchFieldException
并且该字段是静态的(或者如果不在 get 调用中,则传入该类的实例)。
关于java - 如何在 Java 的步骤定义中使用 Cucumber 数据表中的变量名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31307526/