首页 > 解决方案 > 使用 Selenium 以非整数偏移量移动滑块手柄

问题描述

我正在尝试使用 Selenium测试移动滑块(使用 rc-slider: http ://react-component.github.io/slider/)。问题是基于滑块的宽度和值的范围,有时目标偏移量是非整数量。如果我将目标偏移量转换为 int(见下面的代码),它有时会比预期的值低/高 1 个值。

不幸的是,看起来 Actions 中可用的方法(dragAndDropBy、moveByOffset 等)仅支持整数作为偏移量。还有另一种方法可以将滑块手柄移动到正确的偏移量,即使它是浮点数而不是整数?

private void setSingleValue(String boundSelector, Integer value) {
    if (value != null) {
        WebElement slider = this.webElement().findElement(By.className("rc-slider"));
        WebElement handle = slider.findElement(By.className(boundSelector));

        float valuePercentage = (float)value / this.maxValue;
        int sliderWidth = slider.getSize().getWidth();

        int xOffset;
        if (value == 0) { // move slider back to beginning
            xOffset = -this.maxValue;
        } else {
            xOffset = (int)(sliderWidth * valuePercentage); // converting to int here, but don't want to if possible
        }

        Actions action = new Actions(getWebDriver());
        action.dragAndDropBy(handle, xOffset, 0);
        action.build().perform();
    }
}

标签: javaselenium

解决方案


推荐阅读