NỘI DUNG BÀI HỌC
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 chờ đợi trang load xong mới thao tác
/**
* Chờ đợi trang tải xong (Javascript) với thời gian thiết lập sẵn
*/
public static void waitForPageLoaded() {
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(PAGE_LOAD_TIMEOUT), Duration.ofMillis(500));
JavascriptExecutor js = (JavascriptExecutor) driver;
// wait for Javascript to loaded
ExpectedCondition < Boolean > jsLoad = driver -> ((JavascriptExecutor) driver).executeScript("return document.readyState")
.toString().equals("complete");
//Get JS is Ready
boolean jsReady = js.executeScript("return document.readyState").toString().equals("complete");
//Wait Javascript until it is Ready!
if (!jsReady) {
logConsole("Javascript in NOT Ready!");
//Wait for Javascript to load
try {
wait.until(jsLoad);
} catch (Throwable error) {
error.printStackTrace();
Assert.fail("Timeout waiting for page load (Javascript). (" + PAGE_LOAD_TIMEOUT + "s)");
}
}
}
/**
* Chờ đợi JQuery tải xong với thời gian thiết lập sẵn
*/
public static void waitForJQueryLoad() {
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(PAGE_LOAD_TIMEOUT), Duration.ofMillis(500));
JavascriptExecutor js = (JavascriptExecutor) driver;
//Wait for jQuery to load
ExpectedCondition < Boolean > jQueryLoad = driver -> {
assert driver != null;
return ((Long)((JavascriptExecutor) driver)
.executeScript("return jQuery.active") == 0);
};
//Get JQuery is Ready
boolean jqueryReady = (Boolean) js.executeScript("return jQuery.active==0");
//Wait JQuery until it is Ready!
if (!jqueryReady) {
logConsole("JQuery is NOT Ready!");
try {
//Wait for jQuery to load
wait.until(jQueryLoad);
} catch (Throwable error) {
Assert.fail("Timeout waiting for JQuery load. (" + PAGE_LOAD_TIMEOUT + "s)");
}
}
}
//Wait for Angular Load
/**
* Chờ đợi Angular tải xong với thời gian thiết lập sẵn
*/
public static void waitForAngularLoad() {
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(PAGE_LOAD_TIMEOUT), Duration.ofMillis(500));
JavascriptExecutor js = (JavascriptExecutor) driver;
final String angularReadyScript = "return angular.element(document).injector().get('$http').pendingRequests.length === 0";
//Wait for ANGULAR to load
ExpectedCondition < Boolean > angularLoad = driver -> {
assert driver != null;
return Boolean.valueOf(((JavascriptExecutor) driver)
.executeScript(angularReadyScript).toString());
};
//Get Angular is Ready
boolean angularReady = Boolean.parseBoolean(js.executeScript(angularReadyScript).toString());
//Wait ANGULAR until it is Ready!
if (!angularReady) {
logConsole("Angular is NOT Ready!");
//Wait for Angular to load
try {
//Wait for jQuery to load
wait.until(angularLoad);
} catch (Throwable error) {
Assert.fail("Timeout waiting for Angular load. (" + PAGE_LOAD_TIMEOUT + "s)");
}
}
}
Các hàm chờ đợi Element
//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());
}
}
Các hàm xử lý
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 element) {
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].scrollIntoView(true);", getWebElement(element));
}
public static void scrollToElement(WebElement element) {
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].scrollIntoView(true);", element);
}
public static void scrollToPosition(int X, int Y) {
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollTo(" + X + "," + Y + ");");
}
public static boolean moveToElement(By toElement) {
try {
Actions action = new Actions(driver);
action.moveToElement(getWebElement(toElement)).release(getWebElement(toElement)).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);
}