1. 程式人生 > >Java&Selenium 滑鼠鍵盤及滾動條控制相關方法封裝

Java&Selenium 滑鼠鍵盤及滾動條控制相關方法封裝

 一、摘要

本片博文主要展示在使用Selenium with java做web自動化時,一些不得不模擬滑鼠操作、模擬鍵盤操作和控制滾動條的java程式碼

 二、模擬滑鼠操作

package util;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.WebDriver;

public class MouseUtil {
    
/** * 模擬滑鼠左鍵單擊 * @param driver * @param xpathExpression */ public void lefClick(WebDriver driver, String xpathExpression) { Actions actions = new Actions(driver); // 滑鼠左鍵在當前停留的位置做單擊操作 actions.click(); // 滑鼠左鍵點選指定的元素 actions.click(driver.findElement(By.xpath(xpathExpression))); }
/** * 模擬滑鼠右鍵單擊 * @param driver * @param xpathExpression */ public void rightClick(WebDriver driver, String xpathExpression) { Actions actions = new Actions(driver); // 滑鼠右鍵在當前停留的位置做單擊操作 actions.contextClick(); // 滑鼠右鍵點選指定的元素 actions.contextClick(driver.findElement(By.xpath(xpathExpression))); }
/** * 模擬滑鼠左鍵雙擊 * @param driver * @param xpathExpression */ public void doubleClick(WebDriver driver, String xpathExpression) { Actions actions = new Actions(driver); // 滑鼠在當前停留的位置做雙擊操作 actions.doubleClick(); // 滑鼠雙擊指定的元素 actions.doubleClick(driver.findElement(By.xpath(xpathExpression))); } /** * 模擬滑鼠拖拽 * @param driver * @param source * @param target */ public void dragAction(WebDriver driver, WebElement source, WebElement target) { Actions actions = new Actions(driver); // 滑鼠拖拽動作,將 source 元素拖放到 target 元素的位置 actions.dragAndDrop(source, target); } /** * 模擬滑鼠拖拽到某座標 * @param driver * @param source * @param xOffset * @param yOffset */ public void dragtoXY(WebDriver driver, WebElement source, int xOffset, int yOffset) { Actions actions = new Actions(driver); // 滑鼠拖拽動作,將 source 元素拖放到 (xOffset, yOffset) 位置,其中 xOffset 為橫座標,yOffset 為縱座標 actions.dragAndDropBy(source, xOffset, yOffset); } /** * 模擬滑鼠拖拽從元素A到元素B * @param driver * @param source * @param target */ public void dragActionReleaseMouse(WebDriver driver, WebElement source, WebElement target) { Actions actions = new Actions(driver); // 滑鼠拖拽動作,將 source 元素拖放到 target 元素的位置 actions.clickAndHold(source).moveToElement(target).perform(); actions.release(); } /** * 模擬滑鼠單擊並不釋放 * @param driver * @param element */ public void clickAndHole(WebDriver driver, WebElement element) { Actions actions = new Actions(driver); //action.clickAndHold();滑鼠懸停在當前位置,既點選並且不釋放 // 滑鼠懸停在 onElement 元素的位置 actions.clickAndHold(element); } /** * 模擬滑鼠拖拽 * @param driver * @param xOffset * @param yOffset */ public void moveToXY(WebDriver driver, int xOffset, int yOffset){ Actions actions = new Actions(driver); /**將滑鼠移到元素 toElement 的 (xOffset, yOffset) 位置,這裡的 (xOffset, yOffset) 是以元素 toElement 的左上角為 (0,0) 開始的 (x, y) 座標軸 *action.moveToElement(toElement,xOffset,yOffset) *以滑鼠當前位置或者 (0,0) 為中心開始移動到 (xOffset, yOffset) 座標軸*/ actions.moveByOffset(xOffset, yOffset); actions.release();// 釋放滑鼠 } }

 三、模擬鍵盤操作

package util;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;

public class KeyBoardUtil {
    /**Tab鍵封裝*/
    public static void pressTabKey(){
        Robot robot = null;
        try{
            robot = new Robot();
        }catch (AWTException e){
            e.printStackTrace();
        }
        //呼叫keypress方法來實現按下Tab鍵
        assert robot != null;
        robot.keyPress(KeyEvent.VK_TAB);
        //呼叫keyrelease方法來實現釋放Tab鍵
        robot.keyRelease(KeyEvent.VK_TAB);
    }
    /**Enter鍵封裝*/
    public static void pressEnterKey(){
        Robot robot = null;
        try{
            robot = new Robot();
        }catch (AWTException e){
            e.printStackTrace();
        }
        //呼叫keypress方法來實現按下Enter鍵
        assert robot != null;
        robot.keyPress(KeyEvent.VK_ENTER);
        //呼叫keyrelease方法來實現釋放Enter鍵
        robot.keyRelease(KeyEvent.VK_ENTER);
    }
    /**將指定字串設為剪下板內容,執行黏貼操作
     *將頁面焦點切換到輸入框後,呼叫此函式,將指定字串黏貼到輸入框
     */
    public static void setAndctrlVClipboardData(String string){
        StringSelection stringSelection = new StringSelection(string);
        Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
        Robot robot = null;
        try{
            robot = new Robot();
        }catch (AWTException e){
            e.printStackTrace();
        }
        assert robot != null;
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_V);
        robot.keyRelease(KeyEvent.VK_CONTROL);
    }

    /**
     * 鍵盤向下鍵封裝
     */
    public static void pressDownKey(){
        Robot robot = null;
        try{
            robot = new Robot();
        }catch (AWTException e){
            e.printStackTrace();
        }
        assert robot != null;
        robot.keyPress(KeyEvent.VK_DOWN);
        robot.keyRelease(KeyEvent.VK_DOWN);
    }
}

 四、控制滾動條

package util;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;


public class ScrollBarUtil {
    /**
     * 控制滾動條向下拉到底
     * @param driver 瀏覽器驅動
     */
    public static void toBottom(WebDriver driver) {
        JavascriptExecutor js = (JavascriptExecutor) driver;
        //向下拉到底//
        js.executeScript("document.documentElement.scrollTop=10000");
    }

    /**
     * 控制滾動條向上拉到頂
     * @param driver 瀏覽器驅動
     */
    public static void toTop(WebDriver driver) {
        JavascriptExecutor js = (JavascriptExecutor) driver;
        //向上拉到頂
        js.executeScript("document.documentElement.scrollTop=0");
    }

    /**
     * 控制滾動條向下拉到底
     * @param driver 瀏覽器驅動
     */
    public static void scrolltoBottom(WebDriver driver) {
        JavascriptExecutor js = (JavascriptExecutor) driver;
        //向下拉到底
        js.executeScript("window.scrollTo(0,100000)");
    }

    /**
     * 控制滾動條向上拉到頂
     * @param driver 瀏覽器驅動
     */
    public static void scrolltoTop(WebDriver driver) {
        JavascriptExecutor js = (JavascriptExecutor) driver;
        //向上拉到頂
        js.executeScript("window.scrollTo(0,1)");
    }

    /**
     * 控制滾動條拉到中間
     * @param driver 瀏覽器驅動
     */
    public static void verticaltoMiddle(WebDriver driver) {
        JavascriptExecutor js = (JavascriptExecutor) driver;
        //上下拉到中間
        js.executeScript("window.scrollBy(0, 0-document.body.scrollHeight *1/2)");
    }

    /**
     * 控制水平滾動條左右拉到中間
     * @param driver 瀏覽器驅動
     */
    public static void horizontaltoMiddle(WebDriver driver) {
        JavascriptExecutor js = (JavascriptExecutor) driver;
        //左右拉到中間
        js.executeScript("window.scrollBy(0, 0-document.body.scrollWidht *1/2)");
    }

    /**
     * 控制滾動條拉到元素可見
     * @param driver 瀏覽器驅動
     * @param element 頁面元素定位
     */
    public static void scrolltoPresence(WebDriver driver, WebElement element) {
        JavascriptExecutor js = (JavascriptExecutor) driver;
        //移動到元素element物件的“頂端”與當前視窗的“頂部”對齊
        //js.executeScript("arguments[0].scrollIntoView();", element);
        js.executeScript("arguments[0].scrollIntoView(true);", element);
        //移動到元素element物件的“底端”與當前視窗的“底部”對齊
        //js.executeScript("arguments[0].scrollIntoView(false);", element);
    }

    /**
     * 使用JavaScript的ScrollTo函式和document.body.scrollHeight引數
     * 將頁面滾動到最下方
     * @param driver 瀏覽器驅動
     */
    public static void scrollingToBottomofPage(WebDriver driver){
        ((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight)");
        try{
            Thread.sleep(3000);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }

    /**
     * 使用JavaScript的ScrollTo函式,使用0和800的橫縱座標引數
     * 將頁面的滾動條縱向下滑800個畫素
     * @param driver 瀏覽器驅動
     */
    public static void scrollingByCoordinateofPage(WebDriver driver){

        ((JavascriptExecutor) driver).executeScript("window.scrollBy(0,200)");
        try{
            Thread.sleep(3000);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }
}