NỘI DUNG BÀI HỌC

Thực hành viết hàm xử lý chung nâng cao để dùng lại
 


Mấy cái hàm này các bạn copy dán vào class WebUI keyword hay bất kỳ class nào theo ý các bạn nhen. Chú ý mấy cái field bị thiếu thì khai báo thêm vào. Và class nào thiếu thì import vào.

Các hàm xử lý Element

public static void setTextAndKey(By by, String value, Keys key) {
    waitForPageLoaded();
    getWebElement(by).sendKeys(value, key);
    System.out.println("Set text: " + value + " on element " + by);
}

public static void scrollToElement(By by) {
    JavascriptExecutor js = (JavascriptExecutor) driver;
    js.executeScript("arguments[0].scrollIntoView(false);", getWebElement(by));
}

public static void scrollToElement(WebElement element) {
    JavascriptExecutor js = (JavascriptExecutor) driver;
    js.executeScript("arguments[0].scrollIntoView(false);", element);
}

public static void scrollToElementAtTop(By by) {
    JavascriptExecutor js = (JavascriptExecutor) DriverManager.getDriver();
    js.executeScript("arguments[0].scrollIntoView(true);", getWebElement(by));
}

public static void scrollToElementAtBottom(By by) {
    JavascriptExecutor js = (JavascriptExecutor) DriverManager.getDriver();
    js.executeScript("arguments[0].scrollIntoView(false);", getWebElement(by));
}

public static void scrollToElementAtTop(WebElement element) {
    JavascriptExecutor js = (JavascriptExecutor) DriverManager.getDriver();
    js.executeScript("arguments[0].scrollIntoView(true);", element);
}

public static void scrollToElementAtBottom(WebElement element) {
    JavascriptExecutor js = (JavascriptExecutor) DriverManager.getDriver();
    js.executeScript("arguments[0].scrollIntoView(false);", element);
}

public static void scrollToPosition(int X, int Y) {
    JavascriptExecutor js = (JavascriptExecutor) driver;
    js.executeScript("window.scrollTo(" + X + "," + Y + ");");
}

public static boolean moveToElement(By by) {
    try {
        Actions action = new Actions(driver);
        action.moveToElement(getWebElement(by)).release(getWebElement(by)).build().perform();
        return true;
    } catch (Exception e) {
        logConsole(e.getMessage());
        return false;
    }
}

public static boolean moveToOffset(int X, int Y) {
    try {
        Actions action = new Actions(driver);
        action.moveByOffset(X, Y).build().perform();
        return true;
    } catch (Exception e) {
        logConsole(e.getMessage());
        return false;
    }
}

public static boolean hoverElement(By by) {
    try {
        Actions action = new Actions(driver);
        action.moveToElement(getWebElement(by)).perform();
        return true;
    } catch (Exception e) {
        return false;
    }
}

public static boolean mouseHover(By by) {
    try {
        Actions action = new Actions(driver);
        action.moveToElement(getWebElement(by)).perform();
        return true;
    } catch (Exception e) {
        return false;
    }
}

public static boolean dragAndDrop(By fromElement, By toElement) {
    try {
        Actions action = new Actions(driver);
        action.dragAndDrop(getWebElement(fromElement), getWebElement(toElement)).perform();
        //action.clickAndHold(getWebElement(fromElement)).moveToElement(getWebElement(toElement)).release(getWebElement(toElement)).build().perform();
        return true;
    } catch (Exception e) {
        logConsole(e.getMessage());
        return false;
    }
}

public static boolean dragAndDropElement(By fromElement, By toElement) {
    try {
        Actions action = new Actions(driver);
        action.clickAndHold(getWebElement(fromElement)).moveToElement(getWebElement(toElement)).release(getWebElement(toElement)).build().perform();
        return true;
    } catch (Exception e) {
        logConsole(e.getMessage());
        return false;
    }
}

public static boolean dragAndDropOffset(By fromElement, int X, int Y) {
    try {
        Actions action = new Actions(driver);
        //Tính từ vị trí click chuột đầu tiên (clickAndHold)
        action.clickAndHold(getWebElement(fromElement)).pause(1).moveByOffset(X, Y).release().build().perform();
        return true;
    } catch (Exception e) {
        logConsole(e.getMessage());
        return false;
    }
}

public static boolean pressENTER() {
    try {
        Robot robot = new Robot();
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.keyRelease(KeyEvent.VK_ENTER);
        return true;
    } catch (Exception e) {
        return false;
    }
}

public static boolean pressESC() {
    try {
        Robot robot = new Robot();
        robot.keyPress(KeyEvent.VK_ESCAPE);
        robot.keyRelease(KeyEvent.VK_ESCAPE);
        return true;
    } catch (Exception e) {
        return false;
    }
}

public static boolean pressF11() {
    try {
        Robot robot = new Robot();
        robot.keyPress(KeyEvent.VK_F11);
        robot.keyRelease(KeyEvent.VK_F11);
        return true;
    } catch (Exception e) {
        return false;
    }
}

/**
 * @param by truyền vào đối tượng element dạng By
 * @return Tô màu viền đỏ cho Element trên website
 */
public static WebElement highLightElement(By by) {
    // Tô màu border ngoài chính element chỉ định - màu đỏ (có thể đổi màu khác)
    if (driver instanceof JavascriptExecutor) {
        ((JavascriptExecutor) driver).executeScript("arguments[0].style.border='3px solid red'", getWebElement(by));
        sleep(1);
    }
    return getWebElement(by);
}

 

Các hàm Verify và Assert

public static boolean verifyEquals(Object actual, Object expected) {
    waitForPageLoaded();
    System.out.println("Verify equals: " + actual + " and " + expected);
    boolean check = actual.equals(expected);
    return check;
}

public static void assertEquals(Object actual, Object expected, String message) {
    waitForPageLoaded();
    System.out.println("Assert equals: " + actual + " and " + expected);
    Assert.assertEquals(actual, expected, message);
}

public static boolean verifyContains(String actual, String expected) {
    waitForPageLoaded();
    System.out.println("Verify contains: " + actual + " and " + expected);
    boolean check = actual.contains(expected);
    return check;
}

public static void assertContains(String actual, String expected, String message) {
    waitForPageLoaded();
    System.out.println("Assert contains: " + actual + " and " + expected);
    boolean check = actual.contains(expected);
    Assert.assertTrue(check, message);
}

Teacher

Teacher

Anh Tester

Software Quality Engineer

Đường dẫu khó chân vẫn cần bước đi
Đời dẫu khổ tâm vẫn cần nghĩ thấu

Cộng đồng Automation Testing Việt Nam:

🌱 Telegram Automation Testing:   Cộng đồng Automation Testing
🌱 
Facebook Group Automation: Cộng đồng Automation Testing Việt Nam
🌱 
Facebook Fanpage: Cộng đồng Automation Testing Việt Nam - Selenium
🌱 Telegram
Manual Testing:   Cộng đồng Manual Testing
🌱 
Facebook Group Manual: Cộng đồng Manual Testing Việt Nam

Chia sẻ khóa học lên trang

Bạn có thể đăng khóa học của chính bạn lên trang Anh Tester để kiếm tiền

Danh sách bài học