You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何编写自动化脚本验证批量用户拥有相同角色?

批量验证多用户角色的解决方案

针对你需要批量验证A-tester、B-tester等用户均拥有tester角色的需求,提供以下几种可行方案:


方案1:循环调用现有单验证方法

直接复用已有的verifyValueAtRowAndColumnEqualToExpectationInDatasetsList方法,在测试用例中循环遍历每个用户进行验证。

修改测试代码中的最后一行:

// 替换原有的单个验证逻辑
for (String dataset : datasets) {
    user.dataSetManagementPage()
        .verifyValueAtRowAndColumnEqualToExpectationInDatasetsList("Roles", dataset, role);
}

优缺点:

  • 优点:无需修改原有验证方法,代码改动量极小,快速实现需求
  • 缺点:每个用户都要重复执行元素定位逻辑,执行效率略低,适合小规模用户列表场景

方案2:扩展原方法为批量验证版本

新增一个批量验证方法,仅执行一次列索引定位和行映射构建,再批量校验所有目标用户的角色,提升执行效率并统一收集错误信息。

新增方法代码:

@Step
protected void verifyAllRowsInColumnMatchExpectation(String columnName, List<String> rowNames, String expectation) {
    // 仅定位一次目标列索引
    List<WebElement> tableColumns = waitUntilElementsAreVisible(By.cssSelector(columnHeaderXpath));
    int columnIndex = 0;
    for (int i = 0; i < tableColumns.size(); i++) {
        if (tableColumns.get(i).getText().equals(columnName)) {
            columnIndex = i + 1;
            break;
        }
    }
    Assert.assertNotEquals(columnIndex, 0, String.format("未找到名为'%s'的列\n", columnName));
    if (optional != 0) {
        columnIndex += optional;
    }
    log.info("找到列'" + columnName + "',索引为" + columnIndex);

    // 构建行名到行索引的映射,避免重复遍历
    List<WebElement> tableRows = waitUntilElementsAreVisible(By.cssSelector(allFirstCells));
    Map<String, Integer> rowNameToIndexMap = new HashMap<>();
    for (int i = 0; i < tableRows.size(); i++) {
        String rowText = tableRows.get(i).getText();
        rowNameToIndexMap.put(rowText, this.rowIndex + i);
    }

    // 批量校验所有目标行
    List<String> notFoundRows = new ArrayList<>();
    List<String> mismatchRows = new ArrayList<>();
    for (String rowName : rowNames) {
        Integer rowIndex = rowNameToIndexMap.get(rowName);
        if (rowIndex == null) {
            notFoundRows.add(rowName);
            continue;
        }
        String cellToVerify = String.format(verifyXpath, rowIndex, columnIndex);
        String actualValue = driver.findElement(By.cssSelector(cellToVerify)).getText();
        if (!actualValue.equals(expectation)) {
            mismatchRows.add(String.format("行'%s'实际角色为'%s',预期为'%s'", rowName, actualValue, expectation));
        }
    }

    // 统一抛出断言错误,一次性展示所有问题
    Assert.assertTrue("未找到以下行: " + notFoundRows, notFoundRows.isEmpty());
    Assert.assertTrue("以下行角色不匹配: " + mismatchRows, mismatchRows.isEmpty());
}

测试用例中调用新方法:

.verifyAllRowsInColumnMatchExpectation("Roles", datasets, role);

优缺点:

  • 优点:执行效率更高,一次性收集所有错误信息,便于问题排查
  • 缺点:需要新增方法,有一定代码改动量

方案3:直接遍历数据行批量验证

如果页面表格结构稳定,可以直接遍历所有数据行,构建用户与角色的映射关系后批量校验,逻辑更直观。

新增方法代码:

@Step
protected void verifyAllRolesForDatasets(List<String> datasets, String expectedRole) {
    // 定位Roles列索引
    List<WebElement> headers = waitUntilElementsAreVisible(By.cssSelector(columnHeaderXpath));
    int roleColumnIndex = -1;
    for (int i = 0; i < headers.size(); i++) {
        if (headers.get(i).getText().equals("Roles")) {
            roleColumnIndex = i + 1;
            break;
        }
    }
    Assert.assertNotEquals(roleColumnIndex, -1, "未找到Roles列");

    // 遍历所有数据行,构建用户-角色映射
    List<WebElement> dataRows = waitUntilElementsAreVisible(By.cssSelector("table tbody tr"));
    Map<String, String> datasetToRoleMap = new HashMap<>();
    for (WebElement row : dataRows) {
        String datasetName = row.findElement(By.cssSelector("td:first-child")).getText();
        String role = row.findElement(By.cssSelector(String.format("td:nth-child(%d)", roleColumnIndex))).getText();
        datasetToRoleMap.put(datasetName, role);
    }

    // 批量校验目标用户角色
    List<String> errors = new ArrayList<>();
    for (String dataset : datasets) {
        String actualRole = datasetToRoleMap.get(dataset);
        if (actualRole == null) {
            errors.add(String.format("未找到数据集: %s", dataset));
        } else if (!actualRole.equals(expectedRole)) {
            errors.add(String.format("数据集%s的角色为%s,预期为%s", dataset, actualRole, expectedRole));
        }
    }

    Assert.assertTrue(String.join("\n", errors), errors.isEmpty());
}

测试用例中调用:

.verifyAllRolesForDatasets(datasets, role);

优缺点:

  • 优点:逻辑清晰,避免多次重复定位元素,执行效率高
  • 缺点:依赖页面表格结构(如tbody、td的选择器),若页面结构变更需同步调整选择器

内容的提问来源于stack exchange,提问作者Olgaa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 22:02:28