Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
182 changes: 126 additions & 56 deletions src/main/java/com/anhtester/keywords/WebUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ public static void deleteAllFileInDirectory(String pathDirectory) {
@Step("Verify File Downloaded With JS [Equals]: {0}")
public static boolean verifyFileDownloadedWithJS_Equals(String fileName) {
openWebsite("chrome://downloads");
sleep(3);
sleep(1);
JavascriptExecutor js = (JavascriptExecutor) DriverManager.getDriver();
String element = (String) js.executeScript("return document.querySelector('downloads-manager').shadowRoot.querySelector('#downloadsList downloads-item').shadowRoot.querySelector('#show').getAttribute('title')");
File file = new File(element);
Expand All @@ -343,7 +343,7 @@ public static boolean verifyFileDownloadedWithJS_Equals(String fileName) {
@Step("Verify File Downloaded With JS [Contains]: {0}")
public static boolean verifyFileDownloadedWithJS_Contains(String fileName) {
openWebsite("chrome://downloads");
sleep(3);
sleep(1);
JavascriptExecutor js = (JavascriptExecutor) DriverManager.getDriver();
String element = (String) js.executeScript("return document.querySelector('downloads-manager').shadowRoot.querySelector('#downloadsList downloads-item').shadowRoot.querySelector('#show').getAttribute('title')");
File file = new File(element);
Expand Down Expand Up @@ -386,7 +386,7 @@ public static void getToUrlAuthentication(String url, String username, String pa
LogUtils.info("getToUrlAuthentication with Password: " + password);
// Load the application url
openWebsite(url);
sleep(3);
waitForPageLoaded();
}

//Handle HTML5 validation message and valid value
Expand Down Expand Up @@ -614,7 +614,7 @@ public static void uploadFileWithLocalForm(By by, String filePath) {
Actions action = new Actions(DriverManager.getDriver());
//Click to open form upload
action.moveToElement(getWebElement(by)).click().perform();
sleep(2);
sleep(1);

// Create Robot class
Robot robot = null;
Expand Down Expand Up @@ -2514,16 +2514,33 @@ public static void navigateToUrl(String URL) {
@Step("Set text on text box")
public static void setText(By by, String value) {
smartWait();
waitForElementVisible(by).sendKeys(value);
LogUtils.info("Set text " + value + " on " + by.toString());

if (ExtentTestManager.getExtentTest() != null) {
ExtentReportManager.pass("Set text " + value + " on " + by);
}
AllureManager.saveTextLog("Set text " + value + " on " + by);
waitForElementVisible(by);

addScreenshotToReport(Thread.currentThread().getStackTrace()[1].getMethodName() + "_" + DateUtils.getCurrentDateTime());
int maxRetry = 3;
for (int i = 0; i < maxRetry; i++) {
try {
getWebElement(by).sendKeys(value);
LogUtils.info("Set text " + value + " on " + by.toString());

if (ExtentTestManager.getExtentTest() != null) {
ExtentReportManager.pass("Set text " + value + " on " + by);
}
AllureManager.saveTextLog("Set text " + value + " on " + by);

addScreenshotToReport(Thread.currentThread().getStackTrace()[1].getMethodName() + "_" + DateUtils.getCurrentDateTime());
return;
} catch (StaleElementReferenceException e) {
if (i == maxRetry - 1) {
LogUtils.error("Failed to set text on element " + by + " after " + maxRetry + " retries.");
throw e;
}
sleep(0.5); // Wait a bit before retry
LogUtils.info("Retry setting text on element " + by + " (" + (i + 1) + ")");
} catch (Exception e) {
LogUtils.error("Failed to set text on element " + by);
throw e;
}
}
}

/**
Expand All @@ -2536,16 +2553,33 @@ public static void setText(By by, String value) {
@Step("Set text on text box and press key")
public static void setText(By by, String value, Keys keys) {
smartWait();
waitForElementVisible(by).sendKeys(value, keys);
LogUtils.info("Set text " + value + " on " + by + " and press key " + keys.name());

if (ExtentTestManager.getExtentTest() != null) {
ExtentReportManager.pass("Set text " + value + " on " + by + " and press key " + keys.name());
}
AllureManager.saveTextLog("Set text " + value + " on " + by + " and press key " + keys.name());
waitForElementVisible(by);

addScreenshotToReport(Thread.currentThread().getStackTrace()[1].getMethodName() + "_" + DateUtils.getCurrentDateTime());
int maxRetry = 3;
for (int i = 0; i < maxRetry; i++) {
try {
getWebElement(by).sendKeys(value, keys);
LogUtils.info("Set text " + value + " on " + by + " and press key " + keys.name());

if (ExtentTestManager.getExtentTest() != null) {
ExtentReportManager.pass("Set text " + value + " on " + by + " and press key " + keys.name());
}
AllureManager.saveTextLog("Set text " + value + " on " + by + " and press key " + keys.name());

addScreenshotToReport(Thread.currentThread().getStackTrace()[1].getMethodName() + "_" + DateUtils.getCurrentDateTime());
return;
} catch (StaleElementReferenceException e) {
if (i == maxRetry - 1) {
LogUtils.error("Failed to set text on element " + by + " after " + maxRetry + " retries.");
throw e;
}
sleep(0.5); // Wait a bit before retry
LogUtils.info("Retry setting text on element " + by + " (" + (i + 1) + ")");
} catch (Exception e) {
LogUtils.error("Failed to set text on element " + by);
throw e;
}
}
}

/**
Expand Down Expand Up @@ -2664,16 +2698,34 @@ public static void clearAndFillText(By by, String value) {
@Step("Click on the element {0}")
public static void clickElement(By by) {
smartWait();
waitForElementClickable(by).click();
LogUtils.info("Clicked on the element " + by.toString());

if (ExtentTestManager.getExtentTest() != null) {
ExtentReportManager.pass("Clicked on the element " + by);
}
AllureManager.saveTextLog("Clicked on the element " + by);
waitForElementVisible(by);
waitForElementClickable(by);

addScreenshotToReport(Thread.currentThread().getStackTrace()[1].getMethodName() + "_" + DateUtils.getCurrentDateTime());
int maxRetry = 3;
for (int i = 0; i < maxRetry; i++) {
try {
getWebElement(by).click();
LogUtils.info("Clicked on the element " + by.toString());

if (ExtentTestManager.getExtentTest() != null) {
ExtentReportManager.pass("Clicked on the element " + by);
}
AllureManager.saveTextLog("Clicked on the element " + by);

addScreenshotToReport(Thread.currentThread().getStackTrace()[1].getMethodName() + "_" + DateUtils.getCurrentDateTime());
return;
} catch (StaleElementReferenceException | ElementClickInterceptedException e) {
if (i == maxRetry - 1) {
LogUtils.error("Failed to click element " + by + " after " + maxRetry + " retries.");
throw e;
}
sleep(0.5); // Wait a bit before retry
LogUtils.info("Retry clicking element " + by + " (" + (i + 1) + ")");
} catch (Exception e) {
LogUtils.error("Failed to click element " + by);
throw e;
}
}
}

/**
Expand All @@ -2684,16 +2736,34 @@ public static void clickElement(By by) {
@Step("Click on the element {0} with timeout {1}s")
public static void clickElement(By by, int timeout) {
smartWait();
waitForElementClickable(by, timeout).click();
LogUtils.info("Clicked on the element " + by.toString());

if (ExtentTestManager.getExtentTest() != null) {
ExtentReportManager.pass("Clicked on the element " + by);
}
AllureManager.saveTextLog("Clicked on the element " + by);
waitForElementVisible(by, timeout);
waitForElementClickable(by, timeout);

addScreenshotToReport(Thread.currentThread().getStackTrace()[1].getMethodName() + "_" + DateUtils.getCurrentDateTime());
int maxRetry = 3;
for (int i = 0; i < maxRetry; i++) {
try {
getWebElement(by).click();
LogUtils.info("Clicked on the element " + by.toString());

if (ExtentTestManager.getExtentTest() != null) {
ExtentReportManager.pass("Clicked on the element " + by);
}
AllureManager.saveTextLog("Clicked on the element " + by);

addScreenshotToReport(Thread.currentThread().getStackTrace()[1].getMethodName() + "_" + DateUtils.getCurrentDateTime());
return;
} catch (StaleElementReferenceException | ElementClickInterceptedException e) {
if (i == maxRetry - 1) {
LogUtils.error("Failed to click element " + by + " after " + maxRetry + " retries.");
throw e;
}
sleep(0.5); // Wait a bit before retry
LogUtils.info("Retry clicking element " + by + " (" + (i + 1) + ")");
} catch (Exception e) {
LogUtils.error("Failed to click element " + by);
throw e;
}
}
}

/**
Expand Down Expand Up @@ -3206,19 +3276,19 @@ public static void waitForPageLoaded() {
// 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");
try {
//Get JS is Ready
boolean jsReady = js.executeScript("return document.readyState").toString().equals("complete");

//Wait Javascript until it is Ready!
if (!jsReady) {
//LogUtils.info("Javascript in NOT Ready!");
//Wait for Javascript to load
try {
//Wait Javascript until it is Ready!
if (!jsReady) {
//LogUtils.info("Javascript in NOT Ready!");
//Wait for Javascript to load
wait.until(jsLoad);
} catch (Throwable error) {
LogUtils.error("Timeout waiting for page load. (" + FrameworkConstants.WAIT_PAGE_LOADED + "s)");
Assert.fail("Timeout waiting for page load. (" + FrameworkConstants.WAIT_PAGE_LOADED + "s)");
}
} catch (Throwable error) {
LogUtils.error("Timeout waiting for page load. (" + FrameworkConstants.WAIT_PAGE_LOADED + "s)");
Assert.fail("Timeout waiting for page load. (" + FrameworkConstants.WAIT_PAGE_LOADED + "s)");
}
}

Expand All @@ -3232,19 +3302,19 @@ public static void waitForPageLoaded(int timeOut) {
// 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");
try {
//Get JS is Ready
boolean jsReady = js.executeScript("return document.readyState").toString().equals("complete");

//Wait Javascript until it is Ready!
if (!jsReady) {
LogUtils.info("Javascript in NOT Ready!");
//Wait for Javascript to load
try {
//Wait Javascript until it is Ready!
if (!jsReady) {
LogUtils.info("Javascript in NOT Ready!");
//Wait for Javascript to load
wait.until(jsLoad);
} catch (Throwable error) {
LogUtils.error("Timeout waiting for page load. (" + FrameworkConstants.WAIT_PAGE_LOADED + "s)");
Assert.fail("Timeout waiting for page load. (" + FrameworkConstants.WAIT_PAGE_LOADED + "s)");
}
} catch (Throwable error) {
LogUtils.error("Timeout waiting for page load. (" + FrameworkConstants.WAIT_PAGE_LOADED + "s)");
Assert.fail("Timeout waiting for page load. (" + FrameworkConstants.WAIT_PAGE_LOADED + "s)");
}
}

Expand Down
Loading