Search in sources :

Example 1 with DatasetException

use of com.seleniumtests.customexception.DatasetException in project seleniumRobot by bhecquet.

the class Filter method dateFilterCase.

private static boolean dateFilterCase(String name, Object[] values, Operator operator, final Map<String, Object> parameters) {
    Date date;
    try {
        date = DateFormat.getDateInstance().parse(parameters.get(name).toString());
    } catch (ParseException e) {
        date = (Date) parameters.get(name);
    }
    Date dateLeft = (Date) values[0];
    Date dateRight;
    switch(operator) {
        case BETWEEN:
            dateRight = (Date) values[1];
            return (dateLeft.before(date) || dateLeft.equals(date)) && (date.before(dateRight) || date.equals(dateRight));
        case LESS_THAN:
            return date.before(dateLeft);
        case GREATER_THAN:
            return date.after(dateLeft);
        default:
            throw new DatasetException(name + "filter NOT implemented yet for dates.");
    }
}
Also used : ParseException(java.text.ParseException) Date(java.util.Date) DatasetException(com.seleniumtests.customexception.DatasetException)

Example 2 with DatasetException

use of com.seleniumtests.customexception.DatasetException in project seleniumRobot by bhecquet.

the class Filter method numberFilterCase.

private static boolean numberFilterCase(String name, Object[] values, Operator operator, final Map<String, Object> parameters) {
    BigDecimal val = new BigDecimal(parameters.get(name).toString());
    BigDecimal leftValue = new BigDecimal(values[0].toString());
    BigDecimal rightValue;
    switch(operator) {
        case BETWEEN:
            rightValue = new BigDecimal(values[1].toString());
            return leftValue.compareTo(val) < 1 && rightValue.compareTo(val) > -1;
        case LESS_THAN:
            return val.compareTo(leftValue) < 0;
        case GREATER_THAN:
            return val.compareTo(leftValue) > 0;
        default:
            throw new DatasetException(name + "filter NOT implemented yet for numbers.");
    }
}
Also used : BigDecimal(java.math.BigDecimal) DatasetException(com.seleniumtests.customexception.DatasetException)

Example 3 with DatasetException

use of com.seleniumtests.customexception.DatasetException in project seleniumRobot by bhecquet.

the class CSVHelper method getDataFromCSVFile.

/**
 * Reads data from csv file.
 * class : null => filename : entire path of file
 * class : this.getClass(), filename : the filename will be search in the same directory of the class
 *
 * @param clazz
 * @param filename
 * @param filter
 * @param readHeaders
 * @param delimiter
 * @param supportDPFilter
 * @return
 */
public static Iterator<Object[]> getDataFromCSVFile(final Class<?> clazz, final String filename, Filter filter, final boolean readHeaders, final String delimiter, final boolean supportDPFilter) {
    Filter newFilter = filter;
    try (InputStream is = (clazz != null) ? clazz.getResourceAsStream(filename) : new FileInputStream(filename)) {
        if (is == null) {
            return new ArrayList<Object[]>().iterator();
        }
        // Get the sheet
        String[][] csvData = read(is, delimiter);
        if (csvData == null) {
            return new ArrayList<Object[]>().iterator();
        }
        List<Object[]> sheetData = new ArrayList<>();
        if (readHeaders) {
            List<Object> rowData = new ArrayList<>();
            for (int j = 0; j < csvData[0].length; j++) {
                rowData.add(csvData[0][j]);
            }
            sheetData.add(rowData.toArray(new Object[rowData.size()]));
        }
        // Check for blank rows first
        // First row is the header
        StringBuilder sbBlank = new StringBuilder();
        if (sbBlank.length() > 0) {
            sbBlank.deleteCharAt(sbBlank.length() - 1);
            throw new CustomSeleniumTestsException("Blank TestTitle found on Row(s) " + sbBlank.toString() + ".");
        }
        // Support include tags and exclude tags
        if (supportDPFilter) {
            Filter dpFilter = SpreadSheetHelper.getDPFilter();
            if (dpFilter != null) {
                if (newFilter == null) {
                    newFilter = dpFilter;
                } else {
                    newFilter = Filter.and(newFilter, dpFilter);
                }
            }
        }
        // The first row is the header data
        for (int i = 1; i < csvData.length; i++) {
            Map<String, Object> rowDataMap = new HashMap<>();
            List<Object> rowData = new ArrayList<>();
            // Create the mapping between headers and column data
            for (int j = 0; j < csvData[i].length; j++) {
                rowDataMap.put(csvData[0][j], csvData[i][j]);
            }
            for (int j = 0; j < csvData[0].length; j++) {
                // expected.
                if (csvData[i].length > j) {
                    rowData.add(csvData[i][j]);
                } else {
                    rowData.add(null);
                }
            }
            // To support include tags and exclude tags
            if (supportDPFilter) {
                SpreadSheetHelper.formatDPTags(rowDataMap);
            }
            if (newFilter == null || newFilter.match(rowDataMap)) {
                sheetData.add(rowData.toArray(new Object[rowData.size()]));
            }
        }
        if ((!readHeaders && sheetData.isEmpty()) || (readHeaders && sheetData.size() <= 1)) {
            logger.warn("No matching data found on csv file: " + filename + " with filter criteria: " + newFilter.toString());
        }
        return sheetData.iterator();
    } catch (Exception e) {
        throw new DatasetException(e.getMessage());
    }
}
Also used : HashMap(java.util.HashMap) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) CustomSeleniumTestsException(com.seleniumtests.customexception.CustomSeleniumTestsException) ArrayList(java.util.ArrayList) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) CustomSeleniumTestsException(com.seleniumtests.customexception.CustomSeleniumTestsException) DatasetException(com.seleniumtests.customexception.DatasetException) DatasetException(com.seleniumtests.customexception.DatasetException) Filter(com.seleniumtests.core.Filter)

Example 4 with DatasetException

use of com.seleniumtests.customexception.DatasetException in project seleniumRobot by bhecquet.

the class CSVHelper method getHeaderFromCSVFile.

/**
 * Get headers from a csv file.
 *
 * @param   clazz      - null means use the absolute file path, otherwise use relative path under the class
 * @param   filename
 * @param   delimiter  - null means ","
 *
 * @return
 */
public static List<String> getHeaderFromCSVFile(final Class<?> clazz, final String filename, String delimiter) {
    String newDelimiter = delimiter == null ? "," : delimiter;
    try (InputStream is = clazz != null ? clazz.getResourceAsStream(filename) : new FileInputStream(filename)) {
        if (is == null) {
            return new ArrayList<>();
        }
        // Get the sheet
        String[][] csvData = read(is, newDelimiter);
        if (csvData == null) {
            return new ArrayList<>();
        }
        ArrayList<String> rowData = new ArrayList<>();
        for (int j = 0; j < csvData[0].length; j++) {
            rowData.add(csvData[0][j]);
        }
        return rowData;
    } catch (Exception e) {
        throw new DatasetException(e.getMessage());
    }
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) CustomSeleniumTestsException(com.seleniumtests.customexception.CustomSeleniumTestsException) DatasetException(com.seleniumtests.customexception.DatasetException) DatasetException(com.seleniumtests.customexception.DatasetException)

Example 5 with DatasetException

use of com.seleniumtests.customexception.DatasetException in project seleniumRobot by bhecquet.

the class ReplayAction method replay.

/**
 * Replay all actions annotated by ReplayOnError if the class is not a subclass of
 * HtmlElement
 * @param joinPoint
 * @throws Throwable
 */
@Around("!execution(public * com.seleniumtests.uipage.htmlelements.HtmlElement+.* (..))" + "&& execution(@com.seleniumtests.uipage.ReplayOnError public * * (..)) && @annotation(replay)")
public Object replay(ProceedingJoinPoint joinPoint, ReplayOnError replay) throws Throwable {
    int replayDelayMs = replay != null ? replay.replayDelayMs() : 100;
    Instant end = systemClock.instant().plusSeconds(SeleniumTestsContextManager.getThreadContext().getReplayTimeout());
    Object reply = null;
    String targetName = joinPoint.getTarget().toString();
    TestAction currentAction = null;
    if (joinPoint.getTarget() instanceof GenericPictureElement) {
        String methodName = joinPoint.getSignature().getName();
        List<String> pwdToReplace = new ArrayList<>();
        String actionName = String.format("%s on %s %s", methodName, targetName, LogAction.buildArgString(joinPoint, pwdToReplace, new HashMap<>()));
        currentAction = new TestAction(actionName, false, pwdToReplace);
        // order of steps is the right one (first called is first displayed)
        if (TestStepManager.getParentTestStep() != null) {
            TestStepManager.getParentTestStep().addAction(currentAction);
        }
    }
    boolean actionFailed = false;
    Throwable currentException = null;
    try {
        while (end.isAfter(systemClock.instant())) {
            // raised if action cannot be performed
            if (((CustomEventFiringWebDriver) WebUIDriver.getWebDriver(false)).getBrowserInfo().getBrowser() == BrowserType.CHROME || ((CustomEventFiringWebDriver) WebUIDriver.getWebDriver(false)).getBrowserInfo().getBrowser() == BrowserType.EDGE) {
                updateScrollFlagForElement(joinPoint, true, null);
            }
            try {
                reply = joinPoint.proceed(joinPoint.getArgs());
                WaitHelper.waitForMilliSeconds(200);
                break;
            // do not replay if error comes from scenario
            } catch (ScenarioException | ConfigurationException | DatasetException e) {
                throw e;
            } catch (MoveTargetOutOfBoundsException | InvalidElementStateException e) {
                updateScrollFlagForElement(joinPoint, null, e);
            } catch (Throwable e) {
                if (end.minusMillis(200).isAfter(systemClock.instant())) {
                    WaitHelper.waitForMilliSeconds(replayDelayMs);
                    continue;
                } else {
                    throw e;
                }
            }
        }
        return reply;
    } catch (Throwable e) {
        actionFailed = true;
        currentException = e;
        throw e;
    } finally {
        if (currentAction != null && TestStepManager.getParentTestStep() != null) {
            currentAction.setFailed(actionFailed);
            scenarioLogger.logActionError(currentException);
            if (joinPoint.getTarget() instanceof GenericPictureElement) {
                currentAction.setDurationToExclude(((GenericPictureElement) joinPoint.getTarget()).getActionDuration());
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) Instant(java.time.Instant) ArrayList(java.util.ArrayList) InvalidElementStateException(org.openqa.selenium.InvalidElementStateException) ProceedingJoinPoint(org.aspectj.lang.ProceedingJoinPoint) MoveTargetOutOfBoundsException(org.openqa.selenium.interactions.MoveTargetOutOfBoundsException) TestAction(com.seleniumtests.reporter.logger.TestAction) GenericPictureElement(com.seleniumtests.uipage.htmlelements.GenericPictureElement) DatasetException(com.seleniumtests.customexception.DatasetException) ConfigurationException(com.seleniumtests.customexception.ConfigurationException) ScenarioException(com.seleniumtests.customexception.ScenarioException) Around(org.aspectj.lang.annotation.Around)

Aggregations

DatasetException (com.seleniumtests.customexception.DatasetException)5 ArrayList (java.util.ArrayList)3 CustomSeleniumTestsException (com.seleniumtests.customexception.CustomSeleniumTestsException)2 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 HashMap (java.util.HashMap)2 Filter (com.seleniumtests.core.Filter)1 ConfigurationException (com.seleniumtests.customexception.ConfigurationException)1 ScenarioException (com.seleniumtests.customexception.ScenarioException)1 TestAction (com.seleniumtests.reporter.logger.TestAction)1 GenericPictureElement (com.seleniumtests.uipage.htmlelements.GenericPictureElement)1 BigDecimal (java.math.BigDecimal)1 ParseException (java.text.ParseException)1 Instant (java.time.Instant)1 Date (java.util.Date)1 ProceedingJoinPoint (org.aspectj.lang.ProceedingJoinPoint)1 Around (org.aspectj.lang.annotation.Around)1 InvalidElementStateException (org.openqa.selenium.InvalidElementStateException)1 MoveTargetOutOfBoundsException (org.openqa.selenium.interactions.MoveTargetOutOfBoundsException)1