Skip to content

Commit

Permalink
HPCC-31857 - Seperated logging for test failures as error logs and ex…
Browse files Browse the repository at this point in the history
…ception as exception logs.
  • Loading branch information
Nisha-Bagdwal committed Jul 16, 2024
1 parent 40971ba commit 7944f7b
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 52 deletions.
6 changes: 4 additions & 2 deletions esp/src/test-ui/tests/framework/TestRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ public static void main(String[] args) {
Common.driver.quit();
}

Common.printNumOfErrorsAndExceptions();

} catch (Exception e) {
Common.logError("Exception occurred in TestRunner.java: " + e.getMessage());
Common.logException("Exception occurred in TestRunner class: " + e.getMessage(), e);
}
}

Expand Down Expand Up @@ -69,7 +71,7 @@ private static Class<?>[] loadClasses() {
try {
classes.add(Class.forName(testClass.getPath()));
} catch (Exception e) {
Common.logError("Failure: Error in loading classes: " + e.getMessage());
Common.logException("Failure: Error in loading classes: " + e.getMessage(), e);
}
}

Expand Down
1 change: 1 addition & 0 deletions esp/src/test-ui/tests/framework/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
public class Config {

public static final String LOG_FILE_ERROR = "error_ecl_test.log";
public static final String LOG_FILE_EXCEPTION = "exception_ecl_test.log";
public static final String LOG_FILE_DEBUG = "debug_ecl_test.log";
public static final String LOG_FILE_DETAIL = "detail_ecl_test.log";
public static final String LOCAL_OS = "Windows";
Expand Down
8 changes: 5 additions & 3 deletions esp/src/test-ui/tests/framework/pages/ActivitiesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ public class ActivitiesTest {

@Test
public void testActivitiesPage() {
Common.openWebPage(urlMap.get(URLConfig.NAV_ACTIVITIES).getUrl());
if(!Common.openWebPage(urlMap.get(URLConfig.NAV_ACTIVITIES).getUrl())){
return;
}

Common.logDebug("Tests started for: Activities page.");

Expand Down Expand Up @@ -52,7 +54,7 @@ private void testForNavigationLinks(List<NavigationWebElement> navWebElements) {
Common.logError(errorMsg);
}
} catch (Exception ex) {
Common.logError("Failure: Exception in Navigation Link for " + element.name() + ". URL : " + element.hrefValue() + " Error: " + ex.getMessage());
Common.logException("Failure: Exception in Navigation Link for " + element.name() + ". URL : " + element.hrefValue() + " Error: " + ex.getMessage(), ex);
}
}
}
Expand Down Expand Up @@ -105,7 +107,7 @@ private List<NavigationWebElement> getNavWebElements() {

urlMap.put(navName, new URLMapping(navName, hrefValue));
} catch (Exception ex) {
Common.logError("Failure: Activities Page for Navigation Element: " + navName + ": Error: " + ex.getMessage());
Common.logException("Failure: Activities Page for Navigation Element: " + navName + ": Error: " + ex.getMessage(), ex);
}
}

Expand Down
32 changes: 17 additions & 15 deletions esp/src/test-ui/tests/framework/pages/BaseTableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ public abstract class BaseTableTest<T> {
protected List<T> jsonObjects;

protected void testPage() {
Common.openWebPage(getPageUrl());
if(!Common.openWebPage(getPageUrl())){
return;
}

try {
Common.logDebug("Tests started for: " + getPageName() + " page.");
Expand All @@ -92,7 +94,7 @@ protected void testPage() {
Common.logDebug("Tests finished for: " + getPageName() + " page.");

} catch (Exception ex) {
Common.logError("Error: " + getPageName() + ": Exception: " + ex.getMessage());
Common.logException("Error: " + getPageName() + ": Exception: " + ex.getMessage(), ex);
}
}

Expand Down Expand Up @@ -127,7 +129,7 @@ private void testTabClickOnDetailsPage() {
javaScriptElementClick(element);
testPresenceOfColumnNames(getColumnNamesForTabsDetailsPage().get(tabValue), tabValue);
} catch (Exception ex) {
Common.logError("Error: " + getPageName() + " Details Page. Testing tab click on: " + tabValue + " Error: " + ex.getMessage());
Common.logException("Error: " + getPageName() + " Details Page. Testing tab click on: " + tabValue + " Error: " + ex.getMessage(), ex);
}
}

Expand Down Expand Up @@ -211,7 +213,7 @@ private void testLinksInTable() {
Common.logError(dropdownErrorMsg);
}
} catch (Exception ex) {
Common.logError("Failure: " + getPageName() + ": Exception in testing links for column: " + columnKey + " value: " + values.get(i) + " Error: " + ex.getMessage());
Common.logException("Failure: " + getPageName() + ": Exception in testing links for column: " + columnKey + " value: " + values.get(i) + " Error: " + ex.getMessage(), ex);
}
}
}
Expand Down Expand Up @@ -251,7 +253,7 @@ private void testDetailsContentPage(String name) {
try {
waitToLoadDetailsPage(); // sleep until the specific detail fields have loaded
} catch (Exception ex) {
Common.logError("Error: " + getPageName() + ": Exception in waitToLoadDetailsPage: " + getUniqueKeyName() + " : " + name + " Exception: " + ex.getMessage());
Common.logException("Error: " + getPageName() + ": Exception in waitToLoadDetailsPage: " + getUniqueKeyName() + " : " + name + " Exception: " + ex.getMessage(), ex);
}

boolean pass = true;
Expand Down Expand Up @@ -281,7 +283,7 @@ private void testDetailsContentPage(String name) {
}

} catch (Exception ex) {
Common.logError("Error: Details " + getPageName() + "Page, for: " + getUniqueKeyName() + " : " + name + " Error: " + ex.getMessage());
Common.logException("Error: Details " + getPageName() + "Page, for: " + getUniqueKeyName() + " : " + name + " Error: " + ex.getMessage(), ex);
}
}

Expand Down Expand Up @@ -309,7 +311,7 @@ private void testContentAndSortingOrder() {
try {
testTheSortingOrderForOneColumn(getColumnKeys()[i], getColumnNames()[i]);
} catch (Exception ex) {
Common.logError("Error: " + getPageName() + ": Exception while testing sort order for column: "+getColumnNames()[i]+": Exception:" + ex.getMessage());
Common.logException("Error: " + getPageName() + ": Exception while testing sort order for column: " + getColumnNames()[i] + ": Exception:" + ex.getMessage(), ex);
}
}
}
Expand Down Expand Up @@ -347,12 +349,12 @@ private String getCurrentSortingOrder(String columnKey) {

String oldSortOrder = columnHeader.getAttribute("aria-sort");

columnHeader.click();
javaScriptElementClick(columnHeader);

return waitToLoadChangedSortOrder(oldSortOrder, columnKey);

} catch (Exception ex) {
Common.logError("Failure: " + getPageName() + " Exception in getting sort order for column: " + columnKey + " Error: " + ex.getMessage());
Common.logException("Failure: " + getPageName() + " Exception in getting sort order for column: " + columnKey + " Error: " + ex.getMessage(), ex);
}

return null;
Expand Down Expand Up @@ -401,7 +403,7 @@ protected List<Object> getDataFromUIUsingColumnKey(String columnKey) {
}

} catch (Exception ex) {
Common.logError("Failure: " + getPageName() + ": Error in getting data from UI using column key: " + columnKey + "Error: " + ex.getMessage());
Common.logException("Failure: " + getPageName() + ": Error in getting data from UI using column key: " + columnKey + "Error: " + ex.getMessage(), ex);
}

return columnData;
Expand Down Expand Up @@ -431,7 +433,7 @@ void ascendingSortJson(String columnKey) {
try {
jsonObjects.sort(Comparator.comparing(jsonObject -> (Comparable<Object>) getColumnDataFromJson(jsonObject, columnKey)));
} catch (Exception ex) {
Common.logError("Failure: " + getPageName() + ": Exception in sorting JSON in ascending order using column key: " + columnKey + " Error: " + ex.getMessage());
Common.logException("Failure: " + getPageName() + ": Exception in sorting JSON in ascending order using column key: " + columnKey + " Error: " + ex.getMessage(), ex);
}
}

Expand All @@ -440,7 +442,7 @@ void descendingSortJson(String columnKey) {
try {
jsonObjects.sort(Comparator.comparing((Function<T, Comparable<Object>>) jsonObject -> (Comparable<Object>) getColumnDataFromJson(jsonObject, columnKey)).reversed());
} catch (Exception ex) {
Common.logError("Failure: " + getPageName() + ": Exception in sorting JSON in descending order using column key: " + columnKey + " Error: " + ex.getMessage());
Common.logException("Failure: " + getPageName() + ": Exception in sorting JSON in descending order using column key: " + columnKey + " Error: " + ex.getMessage(), ex);
}
}

Expand Down Expand Up @@ -477,7 +479,7 @@ private List<T> getAllObjectsFromJson() {
try {
return parseJson(filePath);
} catch (Exception ex) {
Common.logError("Failure: " + getPageName() + " Unable to parse JSON File: Exception: " + ex.getMessage());
Common.logException("Failure: " + getPageName() + " Unable to parse JSON File: Exception: " + ex.getMessage(), ex);
}

return null;
Expand Down Expand Up @@ -555,7 +557,7 @@ private void clickDropdown(int numOfItemsJSON) {
Common.driver.navigate().refresh();
Common.sleep();
} catch (Exception ex) {
Common.logError("Failure: " + getPageName() + ": Error in clicking dropdown: " + ex.getMessage());
Common.logException("Failure: " + getPageName() + ": Error in clicking dropdown: " + ex.getMessage(), ex);
}
}

Expand All @@ -564,7 +566,7 @@ private String getSelectedDropdownValue() {
WebElement dropdown = Common.waitForElement(By.id("pageSize"));
return dropdown.getText().trim();
} catch (Exception ex) {
Common.logError("Failure: " + getPageName() + ": Error in getting dropdown value: " + ex.getMessage());
Common.logException("Failure: " + getPageName() + ": Error in getting dropdown value: " + ex.getMessage(), ex);
}

return "";
Expand Down
15 changes: 10 additions & 5 deletions esp/src/test-ui/tests/framework/pages/ECLWorkUnitsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ protected String getPageName() {

@Override
protected String getPageUrl() {
return URLConfig.urlMap.get(URLConfig.NAV_ECL).getUrlMappings().get(URLConfig.TAB_ECL_WORKUNITS).getUrl();
try {
return URLConfig.urlMap.get(URLConfig.NAV_ECL).getUrlMappings().get(URLConfig.TAB_ECL_WORKUNITS).getUrl();
} catch (Exception ex){
Common.logException("Error in getting page URL "+ getPageName() +": Exception: "+ ex.getMessage(), ex);
}
return "";
}

@Override
Expand Down Expand Up @@ -167,7 +172,7 @@ private void testDescriptionUpdateFunctionality(String wuName) {
updateDescriptionAndSave(oldDescription);

} catch (Exception ex) {
Common.logError("Error: " + getPageName() + " Details page for WUID: " + wuName + ", Exception occurred while testing for description. Exception: " + ex.getMessage());
Common.logException("Error: " + getPageName() + " Details page for WUID: " + wuName + ", Exception occurred while testing for description. Exception: " + ex.getMessage(), ex);
}
}

Expand Down Expand Up @@ -228,7 +233,7 @@ private void testProtectedButtonFunctionality(String wuName) {
}

} catch (Exception ex) {
Common.logError("Error: ECL WorkUnits: Exception in testing the protected functionality for WUID: " + wuName + ": Error: " + ex.getMessage());
Common.logException("Error: ECL WorkUnits: Exception in testing the protected functionality for WUID: " + wuName + ": Error: " + ex.getMessage(), ex);
}
}

Expand Down Expand Up @@ -371,7 +376,7 @@ protected Object parseDataUIValue(Object dataUIValue, Object dataJSONValue, Stri
dataUIValue = ((String) dataUIValue).trim();
}
} catch (Exception ex) {
Common.logError("Failure: " + getPageName() + " Error in parsing UI value: " + dataUIValue + " for column: " + columnName + " ID: " + dataIDUIValue + " Error: " + ex.getMessage());
Common.logException("Failure: " + getPageName() + " Error in parsing UI value: " + dataUIValue + " for column: " + columnName + " ID: " + dataIDUIValue + " Error: " + ex.getMessage(), ex);
}

return dataUIValue;
Expand Down Expand Up @@ -410,7 +415,7 @@ protected String getCurrentPage() {
return element.getAttribute("title");
}
} catch (Exception ex) {
Common.logError("Error: " + getPageName() + ex.getMessage());
Common.logException("Error: " + getPageName() + ex.getMessage(), ex);
}
return "Invalid Page";
}
Expand Down
63 changes: 36 additions & 27 deletions esp/src/test-ui/tests/framework/utility/Common.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@

import framework.config.Config;
import framework.config.URLConfig;
import org.openqa.selenium.*;
import org.openqa.selenium.By;
import org.openqa.selenium.TimeoutException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.IOException;
import java.time.Duration;
import java.util.Arrays;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand All @@ -20,7 +23,10 @@ public class Common {

public static WebDriver driver;
public static Logger errorLogger = setupLogger("error");
public static Logger exceptionLogger = setupLogger("exception");
public static Logger specificLogger;
public static int num_exceptions = 0;
public static int num_errors = 0;

public static void checkTextPresent(String text, String page) {
try {
Expand All @@ -35,29 +41,31 @@ public static void checkTextPresent(String text, String page) {
}
}

public static void openWebPage(String url) {
public static boolean openWebPage(String url) {
try {
driver.get(url);
driver.manage().window().maximize();
sleep();
return true;
} catch (Exception ex) {
Common.logError("Error in opening web page: " + url);
Common.logException("Error in opening web page: " + url, ex);
return false;
}
}

public static void sleep() {
try {
Thread.sleep(Duration.ofSeconds(Config.WAIT_TIME_IN_SECONDS));
} catch (InterruptedException e) {
Common.logError("Error in sleep: " + e.getMessage());
Common.logException("Error in sleep: " + e.getMessage(), e);
}
}

public static void sleepWithTime(int seconds) {
try {
Thread.sleep(Duration.ofSeconds(seconds));
} catch (InterruptedException e) {
Common.logError("Error in sleep: " + e.getMessage());
Common.logException("Error in sleep: " + e.getMessage(), e);
}
}

Expand Down Expand Up @@ -89,6 +97,16 @@ public static void waitForElementToBeDisabled(WebElement element) {
public static void logError(String message) {
System.err.println(message);
errorLogger.severe(message);
num_errors++;
}

public static void logException(String message, Exception ex) {

message += Arrays.toString(ex.getStackTrace());

System.err.println(message);
exceptionLogger.severe(message);
num_exceptions++;
}

public static void logDebug(String message) {
Expand Down Expand Up @@ -116,6 +134,11 @@ public static void initializeLoggerAndDriver() {
driver = setupWebDriver();
}

public static void printNumOfErrorsAndExceptions() {
System.out.println("Total number of exceptions recorded: " + num_exceptions);
System.out.println("Total number of errors recorded: " + num_errors);
}

private static WebDriver setupWebDriver() {

try {
Expand All @@ -132,16 +155,9 @@ private static WebDriver setupWebDriver() {
System.setProperty("webdriver.chrome.driver", Config.PATH_GH_ACTION_CHROME_DRIVER);
}

RemoteWebDriver driver = new ChromeDriver(chromeOptions);

Capabilities caps = driver.getCapabilities();
String browserName = caps.getBrowserName();
String browserVersion = caps.getBrowserVersion();
System.out.println(browserName + " " + browserVersion);

return driver;
return new ChromeDriver(chromeOptions);
} catch (Exception ex) {
errorLogger.severe("Failure: Error in setting up web driver: " + ex.getMessage());
logException("Failure: Error in setting up web driver: " + ex.getMessage(), ex);
}

return null;
Expand All @@ -162,6 +178,11 @@ private static Logger setupLogger(String logLevel) {
errorFileHandler.setFormatter(new SimpleFormatter());
logger.addHandler(errorFileHandler);
logger.setLevel(Level.SEVERE);
} else if (logLevel.equalsIgnoreCase("exception")) {
FileHandler errorFileHandler = new FileHandler(Config.LOG_FILE_EXCEPTION);
errorFileHandler.setFormatter(new SimpleFormatter());
logger.addHandler(errorFileHandler);
logger.setLevel(Level.SEVERE);
} else if (logLevel.equalsIgnoreCase("debug")) {
FileHandler debugFileHandler = new FileHandler(Config.LOG_FILE_DEBUG);
debugFileHandler.setFormatter(new SimpleFormatter());
Expand All @@ -180,16 +201,4 @@ private static Logger setupLogger(String logLevel) {
Logger.getLogger("org.openqa.selenium").setLevel(Level.OFF); // Turn off all logging from the Selenium WebDriver.
return logger;
}

public static String[] getAttributeList(WebElement webElement, String pageName) {
try {
JavascriptExecutor executor = (JavascriptExecutor) driver;
Object attributes = executor.executeScript("var items = {}; for (index = 0; index < arguments[0].attributes.length; ++index) { items[arguments[0].attributes[index].name] = arguments[0].attributes[index].value }; return items;", webElement);
return attributes.toString().replaceAll("[{}]", "").split(", ");
} catch (Exception ex) {
Common.logError("Failure: " + pageName + ": Error in getting arguments list for web element: " + webElement.getText() + "Error: " + ex.getMessage());
}

return new String[0];
}
}

0 comments on commit 7944f7b

Please sign in to comment.