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 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 Boolean checkElementExist(By by) {
    sleep(2);
    List < WebElement > listElement = driver.findElements(by);

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

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

public static void clickElement(By by) {
    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(TIMEOUT));
    wait.until(ExpectedConditions.visibilityOfElementLocated(by));
    sleep(STEP_TIME);
    driver.findElement(by).click();
    logConsole("Click element: " + by);
}

public static void clickElement(By by, long timeout) {
    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(timeout));
    wait.until(ExpectedConditions.visibilityOfElementLocated(by));
    sleep(STEP_TIME);
    driver.findElement(by).click();
    logConsole("Click element: " + by);
}

public static void setText(By by, String value) {
    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(TIMEOUT));
    wait.until(ExpectedConditions.visibilityOfElementLocated(by));
    sleep(STEP_TIME);
    driver.findElement(by).sendKeys(value);
    logConsole("Set text: " + value + " on element " + by);
}

public static String getElementText(By by) {
    WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(TIMEOUT));
    wait.until(ExpectedConditions.visibilityOfElementLocated(by));
    sleep(STEP_TIME);
    String text = driver.findElement(by).getText();
    logConsole("Get text: " + text);
    return text; //Trả về một giá trị kiểu String
}


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) {
        Assert.fail("Timeout waiting for the element Visible. " + by.toString());
        logConsole("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) {
        Assert.fail("Timeout waiting for the element Visible. " + by.toString());
        logConsole("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) {
        Assert.fail("Element not exist. " + by.toString());
        logConsole("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) {
        Assert.fail("Element not exist. " + by.toString());
        logConsole("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) {
        Assert.fail("Timeout waiting for the element ready to click. " + by.toString());
        logConsole("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) {
        Assert.fail("Timeout waiting for the element ready to click. " + by.toString());
        logConsole("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.");
        }
    }
}

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