首页 > 解决方案 > 在没有生成值的情况下取回存储的数据

问题描述

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

@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."



    

标签: javacucumberselenide

解决方案


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

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();
}

推荐阅读