java - 在没有生成值的情况下取回存储的数据
问题描述
我有一个测试用例,它想要创建一个具有一些特定和生成数据的用户,并且它想要存储生成的数据以再次重复此方法。但是,当它运行第二次创建时,我得到了存储的数据 + 新生成的数据。我想取回没有生成值的存储数据。
@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."
解决方案
每次调用同一个步骤时,您都在使用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();
}
推荐阅读
- java - 从 Spring Boot 1 升级到 Spring Boot 2 后,对每个 HTTP 请求进行不必要的日志记录
- php - PHP 7.2.23 MongoDB\Driver\Manager 数组集群返回空
- sql - 替换oracle数据库值范围内的字符
- c++ - 是否可以限制 C++ 17 并行“for_each”的线程数?
- python - H2O 以不同的方式解析文件类型
- c - 将字符串数组传递给在所有级别下施加 const-ness 的函数的最佳方法
- python - 如何针对烧瓶中的 jsonschema 验证 json?
- c# - 使用 Jodit WYSIWYG 和 .NET 上传图像
- javascript - 使用 Radio 值设置类背景颜色?
- android - 怎么可能收到收取 0 美元的订单?谷歌开发者控制台