Kiểm thử tự động - Thao tác chuột

Mouse actions trong Selenium

ưới đây là một số hành động chính của chuột mà các lập trình viên sẽ gặp phải trong hầu hết các ứng dụng:

  • Click: Thực hiện nhấp chuột. Chúng tôi cũng có thể thực hiện một nhấp chuộ
    void click(WebElement onElement)
    void contextClick(WebElement onElement)
    void doubleClick(WebElement onElement)
    void moveToElement(WebElement toElement)
    void moveToElement(WebElement toElement, long xOffset, long yOffset
    t dựa trên tọa độ.
  • contextClick: Thực hiện ngữ cảnh nhấp / nhấp chuột phải vào một phần tử hoặc dựa trên các tọa độ
  • doubleClick: Thực hiện nhấp đúp vào webelement hoặc dựa trên tọa độ. Nếu để trống, nó thực hiện nhấp đúp vào vị trí hiện tại.
  • moveToElement: Di chuyển chuột đến phần tử được chỉ định hoặc dựa trên tọa độ.

Dưới đây là cú pháp để gọi mouse actions trong Selenium bằng cách sử dụng Selenium WebDriver:

Ví dụ

Dưới đây là ví dụ gọi mouse actions trong Selenium, mở trang web viettuts.vn, di chuyển chuột đến menu 'web', click vào link 'JavaScript'.

package com.hiepsiit.selenium;
 
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
 
public class MouseDemo {
    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", 
                "D:\\SeleniumWebdriver\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
 
        // Open website
        driver.get("");
 
        // Maximize the browser
        driver.manage().window().maximize();
         
        WebElement webMenu = driver.findElement(By.xpath(".//a[@href='/web']"));
         
        // move mouse to webMenu element
        Actions actions = new Actions(driver);
        actions.moveToElement(webMenu).perform();
    }
}

Kết quả:

 

Ví dụ Mouse actions trong Selenium