Skip to content

Commit fb8e516

Browse files
author
poncoe
committed
improving exception handler
1 parent 286d2cf commit fb8e516

File tree

5 files changed

+53
-44
lines changed

5 files changed

+53
-44
lines changed

src/main/java/com/coedotzmagic/qatools/util/BrowserHelper.java

Lines changed: 34 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -27,44 +27,49 @@ public class BrowserHelper {
2727
*/
2828
public static boolean checkTabBrowser(String targetUrl) {
2929
WebDriver driver = DriverHelper.GetWebDriver();
30-
String jsCodeNumberWindows = "return window.top.frames.length;";
31-
JavascriptExecutor jsExecutor = (JavascriptExecutor) DriverHelper.GetWebDriver();
32-
jsExecutor.executeScript(jsCodeNumberWindows);
33-
int numberOfOpenWindows = DriverHelper.GetWebDriver().getWindowHandles().size();
34-
List<String> windowHandles = new ArrayList<>(driver.getWindowHandles());
35-
String tabHandle;
30+
try {
31+
String jsCodeNumberWindows = "return window.top.frames.length;";
32+
JavascriptExecutor jsExecutor = (JavascriptExecutor) DriverHelper.GetWebDriver();
33+
jsExecutor.executeScript(jsCodeNumberWindows);
34+
int numberOfOpenWindows = DriverHelper.GetWebDriver().getWindowHandles().size();
35+
List<String> windowHandles = new ArrayList<>(driver.getWindowHandles());
36+
String tabHandle;
3637

37-
// jalankan javascript untuk mendapatkan url sekarang dari masing2 tab
38-
boolean result = false;
39-
for (int i = 0; i < numberOfOpenWindows; i++) {
40-
// arahkan tab ke target
41-
tabHandle = windowHandles.get(i);
42-
driver.switchTo().window(tabHandle);
38+
// jalankan javascript untuk mendapatkan url sekarang dari masing2 tab
39+
boolean result = false;
40+
for (int i = 0; i < numberOfOpenWindows; i++) {
41+
// arahkan tab ke target
42+
tabHandle = windowHandles.get(i);
43+
driver.switchTo().window(tabHandle);
4344

44-
// jalankan javascript untuk mendapatkan url root saat ini
45-
String jsCode = "return window.top.location.href;";
45+
// jalankan javascript untuk mendapatkan url root saat ini
46+
String jsCode = "return window.top.location.href;";
4647

47-
try {
48-
String currentURL = (String) jsExecutor.executeScript(jsCode);
48+
try {
49+
String currentURL = (String) jsExecutor.executeScript(jsCode);
4950

50-
// Periksa apakah URL yang diinginkan ada di URL saat ini
51-
if (currentURL != null && !currentURL.equalsIgnoreCase("")) {
52-
if (currentURL.contains(targetUrl)) {
53-
result = true;
51+
// Periksa apakah URL yang diinginkan ada di URL saat ini
52+
if (currentURL != null && !currentURL.equalsIgnoreCase("")) {
53+
if (currentURL.contains(targetUrl)) {
54+
result = true;
55+
}
5456
}
57+
} catch (Exception e) {
58+
new TellMeWhy("e", TellMeWhy.getTraceInfo(Thread.currentThread().getStackTrace()), TellMeWhy.UNABLE_TO + "get site web :" + e.getMessage());
59+
System.out.println("Error Checking");
5560
}
56-
} catch (Exception e) {
57-
new TellMeWhy("e", TellMeWhy.getTraceInfo(Thread.currentThread().getStackTrace()), TellMeWhy.UNABLE_TO + "get site web :" + e.getMessage());
58-
System.out.println("Error Checking");
5961
}
60-
}
6162

62-
// Arahkan ke tab sebelumnya
63-
tabHandle = windowHandles.iterator().next();
64-
driver.switchTo().window(tabHandle);
63+
// Arahkan ke tab sebelumnya
64+
tabHandle = windowHandles.iterator().next();
65+
driver.switchTo().window(tabHandle);
6566

66-
// kembalikan nilai result
67-
return result;
67+
// kembalikan nilai result
68+
return result;
69+
} catch (Exception e) {
70+
new TellMeWhy("e", TellMeWhy.getTraceInfo(Thread.currentThread().getStackTrace()), TellMeWhy.UNABLE_TO + "Check Tab Browser :" + e.getMessage());
71+
return false;
72+
}
6873
}
6974

7075
/**

src/main/java/com/coedotzmagic/qatools/util/CaptureEvidence.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public static void TakeScreenshot(String folderName) {
7979
FileUtils.copyFile(scrFile, new File("Screenshot/" + title + " - " + timestamp + ".jpg"));
8080
}
8181
} catch (Exception e) {
82-
new TellMeWhy("w", TellMeWhy.getTraceInfo(Thread.currentThread().getStackTrace()), TellMeWhy.UNABLE_TO + "Screenshot this page :" + e.getMessage());
82+
new TellMeWhy("e", TellMeWhy.getTraceInfo(Thread.currentThread().getStackTrace()), TellMeWhy.UNABLE_TO + "Screenshot this page :" + e.getMessage());
8383
}
8484
}
8585

@@ -97,9 +97,13 @@ public static void TakeScreenshot(String folderName) {
9797
public static void TakeScreenshotSpecificPath(String folderName, String xpath) {
9898
WebDriver driver = DriverHelper.GetWebDriver();
9999
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(FailureHandlingHelper.GetTimeoutWait()));
100-
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xpath)));
101-
ElementHelper.currentElement = element;
102-
InteractionsAndKeys.ScrollToElement(element);
103-
TakeScreenshot(folderName);
100+
try {
101+
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xpath)));
102+
ElementHelper.currentElement = element;
103+
InteractionsAndKeys.ScrollToElement(element);
104+
TakeScreenshot(folderName);
105+
} catch (Exception e) {
106+
new TellMeWhy("e", TellMeWhy.getTraceInfo(Thread.currentThread().getStackTrace()), TellMeWhy.UNABLE_TO + "Screenshot this page :" + e.getMessage());
107+
}
104108
}
105109
}

src/main/java/com/coedotzmagic/qatools/util/InteractionsAndKeys.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ public static void CombinationESC() throws AWTException {
110110
*/
111111
public static void ScrollToElement(WebElement element) {
112112
WebDriver driver = DriverHelper.GetWebDriver();
113-
assert driver != null;
114113
try {
114+
assert driver != null;
115115
ElementHelper.currentElement = element;
116116
new Actions(driver).scrollToElement(element).perform();
117117
} catch (Exception e) {
@@ -132,8 +132,8 @@ public static void ScrollToElement(WebElement element) {
132132
*/
133133
public static void ScrollByDistance(int x, int y) {
134134
WebDriver driver = DriverHelper.GetWebDriver();
135-
assert driver != null;
136135
try {
136+
assert driver != null;
137137
try {
138138
new Actions(driver).scrollByAmount(x, y).perform();
139139
} catch (Exception e1) {

src/main/java/com/coedotzmagic/qatools/util/TextUtil.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ public static String getClipboardContent() {
120120
* @since 1.1
121121
*/
122122
public static void InputTextField(String id, String text) {
123+
WebDriver driver = DriverHelper.GetWebDriver();
124+
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(FailureHandlingHelper.GetTimeoutWait()));
123125
try {
124-
WebDriver driver = DriverHelper.GetWebDriver();
125-
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(FailureHandlingHelper.GetTimeoutWait()));
126126
assert driver != null;
127127
element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[contains(@id, '" + id + "') or contains(@name, '" + id + "') or contains(@class, '" + id + "')]")));
128128
ElementHelper.currentElement = element;
@@ -144,9 +144,9 @@ public static void InputTextField(String id, String text) {
144144
* @since 1.2
145145
*/
146146
public static void ClearInputfield (String id) {
147+
WebDriver driver = DriverHelper.GetWebDriver();
148+
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(FailureHandlingHelper.GetTimeoutWait()));
147149
try {
148-
WebDriver driver = DriverHelper.GetWebDriver();
149-
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(FailureHandlingHelper.GetTimeoutWait()));
150150
element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[contains(@id, '" + id + "') or contains(@name, '" + id + "') or contains(@class, '" + id + "')]")));
151151
ElementHelper.currentElement = element;
152152
element.clear();

src/main/java/com/coedotzmagic/qatools/util/VerifyValueHelper.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ public static void VerifyTitlePage(String expect) {
5050
* @since 1.1
5151
*/
5252
public static void VerifyValueElement(String xpath, String expect) {
53+
WebDriver driver = DriverHelper.GetWebDriver();
54+
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(FailureHandlingHelper.GetTimeoutWait()));
5355
String currentValue;
5456
try {
55-
WebDriver driver = DriverHelper.GetWebDriver();
56-
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(FailureHandlingHelper.GetTimeoutWait()));
5757
assert driver != null;
5858
element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xpath)));
5959
currentValue = element.getDomProperty("value");
@@ -81,10 +81,10 @@ public static void VerifyValueElement(String xpath, String expect) {
8181
* @since 1.1
8282
*/
8383
public static void VerifyTextElement(String xpath, String expect) {
84+
WebDriver driver = DriverHelper.GetWebDriver();
85+
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(FailureHandlingHelper.GetTimeoutWait()));
8486
String currentText;
8587
try {
86-
WebDriver driver = DriverHelper.GetWebDriver();
87-
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(FailureHandlingHelper.GetTimeoutWait()));
8888
assert driver != null;
8989
element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xpath)));
9090
currentText = element.getText();

0 commit comments

Comments
 (0)