【问题标题】:Get back stored data without generated values在没有生成值的情况下取回存储的数据
【发布时间】:2021-08-06 19:31:49
【问题描述】:

我有一个测试用例,它想用一些特定的和生成的数据创建一个用户,并且它想存储生成的数据以再次重复此方法。但是,当它运行第二次创建时,我得到了存储的数据 + 新生成的数据。我想取回没有生成值的存储数据。

@When("I create a new user with id: {smartString}, " +
      "name: {smartString} and birth: {smartString}")
public void CreateNewUnit(String id, String name, String date) {

    String testName = name + "-" + RandomUtils.getRandomNumeric(6);
    szepScenarioContext.storeVariable(testName, "testName");

    $(getBy("Create button")).click();
    $(getBy("Id field")).sendKeys(id);
    $(getBy("Name field")).sendKeys(testName);
    $(getBy("Birth field")).sendKeys(date);
    $(getBy("Submit button")).click();
}

Scenario: Create a user
    Given I navigate to the XY page
    When I create a new user with id: "123", name: "Test" and birth: "1999.12.12."
    And I create a new user with id: "123", name: "<$testName>" and birth: "1999.12.12."



    

【问题讨论】:

标签: java cucumber selenide


【解决方案1】:

每次调用同一个步骤时,您都使用通过步骤参数获得的name 参数,添加一个随机数并将该值存储在一个名为testName 的变量中。所以每次使用 step 时,都会覆盖这个变量的值。

如果您想在第二次调用同一步骤时重用该值,您需要添加一个逻辑来检查您是否已经有一个存储的值并使用它,或者如果szepScenarioContext 有一个新的值,则生成一个新的没有存储变量。 像这样的:

public void CreateNewUnit(String id, String name, String date) {
   
   Optional<String> testName = szepScenarioContext.getStoredVariable("testName");
   if (testName.isEmpty())
   {
      String newTestName = name + "-" + RandomUtils.getRandomNumeric(6);
      szepScenarioContext.storeVariable(testName, "testName");
   } 

   $(getBy("Create button")).click();
   $(getBy("Id field")).sendKeys(id);
   $(getBy("Name field")).sendKeys(testName);
   $(getBy("Birth field")).sendKeys(date);
   $(getBy("Submit button")).click();
}

【讨论】:

  • 感谢您的快速回复!不幸的是,我无法运行 if 语句,代码无法识别空字符串,所以我得到了这个:'Optional[]'
  • 我添加的代码假定实现了来自 szepScenarioContext 的新方法“getStoredVariable”以返回一个 Optional 对象。你是这样实现的吗?您需要添加以下导入:import java.util.Optional;。除了使用 Optional 之外还有其他选择,但主要思想与我所说的相同:您需要检查是否已经生成了一个值并检索它,或者您需要生成它并存储它以供后续调用。跨度>
猜你喜欢
  • 2011-08-31
  • 1970-01-01
  • 2021-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多