NỘI DUNG BÀI HỌC

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

Các bạn tạo 1 class để làm keyword ví dụ WebUI thì cần khai báo driver trước nhé. Vì chúng ta đang tuân theo mô hình Page Object Model.

private static WebDriver driver;

public WebUI(WebDriver driver) {
    WebUI.driver = driver;
}

 

Các biến giá trị thời gian thiết lập mặc định

private static int TIMEOUT = 10;
private static double STEP_TIME = 0.5;
private static int PAGE_LOAD_TIMEOUT = 20;

 

Tất cả các hàm trong keyword cần có trạng thái là static để lấy tên class chấm gọi chứ không cần phải khởi tạo đối tượng trang


Các hàm chờ đợi Element để thao tác cho ổn định

//Wait for Element

public static void waitForElementVisible(By by) {
    try {
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(TIMEOUT), Duration.ofMillis(500));
        wait.until(ExpectedConditions.visibilityOfElementLocated(by));
    } catch (Throwable error) {
        logConsole("Timeout waiting for the element Visible. " + by.toString());
        Assert.fail("Timeout waiting for the element Visible. " + by.toString());
    }
}

public static void waitForElementVisible(By by, int timeOut) {
    try {
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(timeOut), Duration.ofMillis(500));
        wait.until(ExpectedConditions.visibilityOfElementLocated(by));
    } catch (Throwable error) {
        logConsole("Timeout waiting for the element Visible. " + by.toString());
        Assert.fail("Timeout waiting for the element Visible. " + by.toString());
    }
}

public static void waitForElementPresent(By by) {
    try {
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(TIMEOUT), Duration.ofMillis(500));
        wait.until(ExpectedConditions.presenceOfElementLocated(by));
    } catch (Throwable error) {
        logConsole("Element not exist. " + by.toString());
        Assert.fail("Element not exist. " + by.toString());
    }
}

public static void waitForElementPresent(By by, int timeOut) {
    try {
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(timeOut), Duration.ofMillis(500));
        wait.until(ExpectedConditions.presenceOfElementLocated(by));
    } catch (Throwable error) {
        logConsole("Element not exist. " + by.toString());
        Assert.fail("Element not exist. " + by.toString());
    }
}

public static void waitForElementClickable(By by) {
    try {
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(TIMEOUT), Duration.ofMillis(500));
        wait.until(ExpectedConditions.elementToBeClickable(getWebElement(by)));
    } catch (Throwable error) {
        logConsole("Timeout waiting for the element ready to click. " + by.toString());
        Assert.fail("Timeout waiting for the element ready to click. " + by.toString());
    }
}

public static void waitForElementClickable(By by, int timeOut) {
    try {
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(timeOut), Duration.ofMillis(500));
        wait.until(ExpectedConditions.elementToBeClickable(getWebElement(by)));
    } catch (Throwable error) {
        logConsole("Timeout waiting for the element ready to click. " + by.toString());
        Assert.fail("Timeout waiting for the element ready to click. " + by.toString());
    }
}


Chờ đợi Trang load xong mới thao tác

 

//Chờ đợi trang load xong mới thao tác
public void waitForPageLoaded() {
    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(30), Duration.ofMillis(500));
    JavascriptExecutor js = (JavascriptExecutor) driver;

    //Wait for Javascript to load
    ExpectedCondition < Boolean > jsLoad = new ExpectedCondition < Boolean > () {
        @Override
        public Boolean apply(WebDriver driver) {
            return js.executeScript("return document.readyState").toString().equals("complete");
        }
    };

    //Check JS is Ready
    boolean jsReady = js.executeScript("return document.readyState").toString().equals("complete");

    //Wait Javascript until it is Ready!
    if (!jsReady) {
        //System.out.println("Javascript is NOT Ready.");
        //Wait for Javascript to load
        try {
            wait.until(jsLoad);
        } catch (Throwable error) {
            error.printStackTrace();
            Assert.fail("FAILED. Timeout waiting for page load.");
        }
    }
}

 

Các hàm xử lý cơ bản

public static void sleep(double second) {
    try {
        Thread.sleep((long)(1000 * second));
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}

public static void logConsole(Object message) {
    System.out.println(message);
}

public static WebElement getWebElement(By by) {
    return driver.findElement(by);
}

public static List<WebElement> getWebElements(By by) {
    return driver.findElements(by);
}

public static Boolean checkElementExist(By by) {
    List<WebElement> listElement = getWebElements(by);

    if (listElement.size() > 0) {
        System.out.println("checkElementExist: " + true + " --- " + by);
        return true;
    } else {
        System.out.println("checkElementExist: " + false + " --- " + by);
        return false;
    }
}

// Hàm kiểm tra sự tồn tại của phần tử với lặp lại nhiều lần
public static boolean checkElementExist(By by, int maxRetries, int waitTimeMillis) {
    int retryCount = 0;

    while (retryCount < maxRetries) {
        try {
            WebElement element = getWebElement(by);
            if (element != null) {
                System.out.println("Tìm thấy phần tử ở lần thử thứ " + (retryCount + 1));
                return true; // Phần tử được tìm thấy
            }
        } catch (NoSuchElementException e) {
            System.out.println("Không tìm thấy phần tử. Thử lại lần " + (retryCount + 1));
            retryCount++;
            try {
                Thread.sleep(waitTimeMillis); // Chờ trước khi thử lại
            } catch (InterruptedException ie) {
                ie.printStackTrace();
            }
        }
    }

    // Trả về false nếu không tìm thấy phần tử sau maxRetries lần
    logConsole("Không tìm thấy phần tử sau " + maxRetries + " lần thử.");
    return false;
}

public static void openURL(String url) {
    driver.get(url);
    sleep(STEP_TIME);
    logConsole("Open URL:  " + url);
}

public static void clickElement(By by) {
    waitForElementClickable(by);
    sleep(STEP_TIME);
    getWebElement(by).click();
    logConsole("Click on element " + by);
}

public static void clickElement(By by, int timeout) {
    waitForElementClickable(by, timeout);
    sleep(STEP_TIME);
    getWebElement(by).click();
    logConsole("Click on element " + by);
}

public static void setText(By by, String value) {
    waitForElementVisible(by);
    sleep(STEP_TIME);
    getWebElement(by).sendKeys(value);
    logConsole("Set text " + value + " on element " + by);
}

public static String getElementText(By by) {
    waitForElementVisible(by);
    sleep(STEP_TIME);
    logConsole("Get text of element " + by);
    String text = getWebElement(by).getText();
    logConsole("==> TEXT: " + text);
    return text; //Trả về một giá trị kiểu String
}

public static String getElementAttribute(By by, String attributeName) {
    waitForElementVisible(by);
    System.out.println("Get attribute of element " + by);
    String value = getWebElement(by).getAttribute(attributeName);
    System.out.println("==> Attribute value: " + value);
    return value;
}

public static String getElementCssValue(By by, String cssPropertyName) {
    waitForElementVisible(by);
    System.out.println("Get CSS value " + cssPropertyName + " of element " + by);
    String value = getWebElement(by).getCssValue(cssPropertyName);
    System.out.println("==> CSS value: " + value);
    return value;
}

 

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