对于一些功能组合键,如 Fn + NmLk 来关闭或者打开数字键,或者 Alt+PrtSC 来抓取当前屏幕的活动窗口并保存到图片,通过 WebDriver 的 Keys 是没办法操作的。 这个时候我们就需要用到 Java 的 Robot 类来实现对这类组合键的操作了。同样Robot调用需要步骤就是调用按下键还需调用释放键
//模拟键盘输入字符,win、mac都适用
static void keyboardString(String string) {
Actions action = new Actions(driver);
action.sendKeys(string).perform();
}
// 按 Tab 键的封装方法,win适用
static void pressTab() {
Robot robot = null;
try {
robot = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
assert robot != null;
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
}
// 按Enter 的封装,win适用
static void pressEnter() {
Actions action = new Actions(driver);
action.sendKeys(Keys.ENTER).perform();
}
// 指定字符串为剪贴板内容,然后粘贴,win适用
static void pressClipboard(String string) {