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

如何优化排行榜测试中Show More按钮点击逻辑以提升效率

优化排行榜自动化测试:解决Show More重复点击与速度问题

看起来你遇到了两个核心痛点:一是show_more()方法只能触发一次数据加载,二是循环内频繁调用该方法导致测试耗时过长。下面我会给出针对性的优化方案,兼顾功能正确性和测试效率。

一、改进show_more()方法:自动点击所有Show More按钮

原来的方法仅单次检查按钮状态,自然只能触发一次加载。我们可以改成循环检查+显式等待的逻辑,让方法自动识别并点击所有出现的Show More按钮,直到没有更多数据可加载为止。

改进后的show_more()实现

import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

// 需要提前声明一个变量记录当前条目总数,初始化时设为playerRows.size()
private int previousRowCount;

public void show_more() throws Exception {
    WebDriverWait wait = new WebDriverWait(Drivers.getDriver(), 10); // 10秒超时可按需调整
    previousRowCount = playerRows.size();
    
    while (true) {
        try {
            // 显式等待按钮可点击,避免元素未加载完成就尝试操作
            wait.until(ExpectedConditions.elementToBeClickable(show_more_button));
            
            if (show_more_button.isDisplayed()) {
                show_more_button.click();
                System.out.println("Clicked Show More to load next batch of entries");
                
                // 等待新条目加载完成:校验条目数量是否增加
                wait.until(driver -> playerRows.size() > previousRowCount);
                previousRowCount = playerRows.size();
            } else {
                break; // 按钮不再显示,退出循环
            }
        } catch (Exception e) {
            System.out.println("No more Show More buttons available");
            break;
        }
    }
}

核心改进点:

  • 用WebDriverWait做显式等待,比隐式等待更精准,减少无意义的等待时间
  • 循环校验按钮状态,直到按钮消失或触发异常
  • 添加条目数量校验,确保新数据真正加载完成后再继续点击

二、优化测试速度:减少冗余DOM查询与重复操作

原来的test_player_leaderboard_entry()方法在循环内反复触发加载和DOM查询,是耗时的主要原因。我们可以从以下几点优化:

1. 先加载所有数据,再统一校验

把show_more()的调用移到循环外,一次性加载完所有排行榜数据,再进行遍历校验,避免循环内反复等待加载。

2. 缓存元素列表,减少重复DOM查询

不要每次循环都用findElement定位单个元素,提前获取所有条目元素的列表,后续直接从缓存中读取:

public void test_player_leaderboard_entry() throws Exception {
    // 第一步:先加载所有数据
    show_more();
    
    // 缓存所有条目元素,避免每次循环重新查询DOM
    List<WebElement> allPlayerRows = Drivers.getDriver().findElements(By.cssSelector("[data-qa-position-value]"));
    int size = allPlayerRows.size();
    
    Integer previous_total = 0;
    Integer previous_points = 0;
    String previous_user = null;

    for (int i = 0; i < size; i++) {
        WebElement currentRow = allPlayerRows.get(i);
        
        // 从缓存的行元素中获取子元素,比全局findElement效率高得多
        String position_first_player = currentRow.findElement(By.cssSelector("[data-qa-position-value='" + i + "']")).getText();
        String points_player = currentRow.findElement(By.cssSelector("[data-qa-points-value='" + i + "']")).getText();
        String username_player = currentRow.findElement(By.cssSelector("[data-qa-player-value='" + i + "']")).getText();
        
        Integer rowNumber = i + 1;
        Integer point_player = Integer.parseInt(points_player);
        Integer total_of_won_and_looking_good = 0;

        // 批量获取当前行的所有pick状态
        List<WebElement> pickElements = currentRow.findElements(By.xpath("./div[3]/div/div"));
        for (WebElement pick : pickElements) {
            String pick_status = pick.getAttribute("data-qa-pick-state");
            int status = Integer.parseInt(pick_status);
            if (status == 1 || status == 2) {
                total_of_won_and_looking_good++;
            }
        }

        // 校验逻辑保持不变
        if (previous_total.equals(total_of_won_and_looking_good) && (previous_points > point_player)) {
            System.out.println("Picks are the same, points are higher ");
        } else if (previous_total.equals(total_of_won_and_looking_good) && previous_points.equals(point_player)) {
            String prev_user = previous_user.toLowerCase();
            String use_player = username_player.toLowerCase();
            Assert.assertTrue(prev_user.compareTo(use_player) < 0);
            System.out.println("Picks are the same, points are the same, alpha order is correct ");
        }

        previous_total = total_of_won_and_looking_good;
        previous_points = point_player;
        previous_user = username_player;
        System.out.println("On row number " + rowNumber + " we find " + username_player + " in position " + position_first_player + " with " + total_of_won_and_looking_good + " correct picks and " + points_player + " points!");
    }
}

关键优化点:

  • 提前加载所有数据,避免循环内的等待开销
  • 缓存条目列表,从缓存的行元素中获取子元素,大幅减少DOM查询次数
  • 用findElements批量获取pick元素,替代循环内的多次xpath查询

三、额外的小优化

  • 禁用页面动画:通过ChromeOptions禁用页面动画,加快元素加载速度
    ChromeOptions options = new ChromeOptions();
    options.addArguments("--disable-animation");
    WebDriver driver = new ChromeDriver(options);
    
  • 优先使用CSS选择器:CSS选择器的查询效率通常比XPath更高,尽量替换不必要的XPath定位
  • 减少日志输出:频繁的System.out.println会增加耗时,可只保留关键日志,或用日志框架控制输出级别

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 06:41:29