一、场景:在编写脚本过程中需要定位的元素,在界面的底部,需要拖拽下拉滚动条,再进行定位元素。
实现思路:用Selenium 里面的 scrollTo 方法实现
二、脚本示例:
1. 用例设计
@Then("^input the keyWords (\\d+) and edit leaveCode (.+)$")
public void input_the_keyWords_and_edit_leaveCode_PL(String keyWords, String leaveCode, DataTable data) throws Throwable {
HashMap<String, String> hash = DataTableUtils.toHashMap(data);
String minimumLeaveNumber = hash.get("最小请假数");
String minimumLeaveUnit = hash.get("请假最少单位");
vp.editLeaveCodeSet(keyWords, leaveCode, minimumLeaveNumber, minimumLeaveUnit);
}
2.封装滚动条方法:
public static void scrollTo(WebElement element, WebDriver driver) {
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].scrollIntoView(false);", element);
}
3.实现滚动条下拉关键代码:
String xpath = "//button[text()='保存']";
WebElement saveButton = waitFor(By.xpath(xpath));
WebDriverUtils.scrollTo(saveButton, driver);
waitLoading();
find(By.xpath(xpath)).click();