All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sdl.selenium.utils.AssertUtil Maven / Gradle / Ivy

Go to download

Automated Acceptance Testing. Selenium and Selenium WebDriver test framework for web applications. (optimized for dynamic html, ExtJS, Bootstrap, complex UI, simple web applications/sites)

There is a newer version: 20.08.432.0_b2d2a09
Show newest version
package com.sdl.selenium.utils;

import com.google.common.base.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.*;
import java.util.function.Function;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

@Component
public class AssertUtil {

    private static Storage storage;

    @Autowired
    public void setStorage(Storage storage) {
        if (AssertUtil.storage == null) {
            AssertUtil.storage = storage;
        }
    }

    private > String showValues(T values, boolean transformDate, Function format) {
        if (values == null) {
            return null;
        }
        String s = logValues(values, transformDate, format);
        return s;
    }

    private > String logValues(T values, boolean transformDate, Function format) {
        List> logs = new ArrayList<>();
//        StringBuilder log = new StringBuilder();
//        log.append("\n");
        for (Object list : values) {
            List l = (List) list;
//            log.append(" | ");
            List logTmp = new ArrayList<>();
            for (Object value : l) {
                if ("".equals(value)) {
//                    log.append("[blank]").append(" | ");
                    logTmp.add("[blank]");
                } else if (value == null) {
//                    log.append(" | ");
                } else {
//                    log.append(transformDate ? format.apply(value + "") : value).append(" | ");
                    logTmp.add(transformDate ? format.apply(value + "") : (String) value);
                }
            }
            logs.add(logTmp);
//            log.append("\n");
        }
//        String s = log.toString();
        String s1 = formatLogs(logs);
        return s1;
    }

    private static > String showValue(T values) {
        if (values == null) {
            return null;
        }
        return String.join(", ", (List) values);
    }

    public  String showObjects(T object, boolean transformDate) {
        if (object == null) {
            return null;
        }
        Class aClass = object.getClass();
        Field[] fields = aClass.getDeclaredFields();
        StringBuilder log = new StringBuilder();
        List fieldsList = new ArrayList<>();
        log.append("\n| ");
        for (Field f : fields) {
            String name = f.getName();
            log.append(name).append(" | ");
            String finalName = name.substring(0, 1).toUpperCase() + name.substring(1);
            String typeName = f.getType().getSimpleName();
            if ("String".equals(typeName) || "List".equals(typeName)) {
                fieldsList.add("get" + finalName);
            } else if ("boolean".equals(typeName)) {
                fieldsList.add("is" + finalName);
            }
        }
        log.append("\n");
        log.append(" | ");
        for (String name : fieldsList) {
            try {
                Method method = object.getClass().getDeclaredMethod(name);
                Object value = method.invoke(object);
                if (value instanceof List) {
                    log.append(value.toString().replace("[", "").replace("]", "")).append(" | ");
                } else {
                    Object format = transformDate ? format(value) : value;
                    log.append(format).append(" | ");
                }
            } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
                e.printStackTrace();
            }
        }
        log.append("\n");
        return log.toString();
    }

    public > String showObjectValues(T lists, boolean transformDate, Function function) {
        if (lists == null || lists.isEmpty()) {
            return null;
        }
        Class aClass;
        try {
            aClass = lists.get(0).getClass();
            if (aClass.getName().contains("List")) {
                return showValues(lists, transformDate, function);
            } else if (lists.getClass().getName().contains("List") && lists.get(0).getClass().getName().contains("String")) {
                return showValue(lists);
            } else if (aClass.getDeclaredFields()[0].getType().getSimpleName().contains("Map")) {
                return showMapValue(lists);
            }
        } catch (IndexOutOfBoundsException e) {
            return null;
        }
        Field[] fields = aClass.getDeclaredFields();
//        StringBuilder log = new StringBuilder();
        List> logs = new ArrayList<>();
        List logTmp = new ArrayList<>();
        List fieldsList = new ArrayList<>();
//        log.append("\n| ");
        for (Field f : fields) {
            String name = f.getName();
//            log.append(name).append(" | ");
            logTmp.add(name);
            String finalName = name.substring(0, 1).toUpperCase() + name.substring(1);
            String typeName = f.getType().getSimpleName();
            if ("String".equals(typeName) || "Boolean".equals(typeName)) {
                fieldsList.add("get" + finalName);
            } else if ("boolean".equals(typeName)) {
                fieldsList.add("is" + finalName);
            }
        }
        logs.add(logTmp);
//        log.append("\n");
        for (Object o : lists) {
//            log.append(" | ");
            logTmp = new ArrayList<>();
            E workFlow = (E) o;
            for (String name : fieldsList) {
                try {
                    Method method = workFlow.getClass().getDeclaredMethod(name);
                    String value = (String) method.invoke(workFlow);
                    Object format;
                    if ("".equals(value)) {
                        format = "[blank]";
                    } else if (value == null) {
                        format = "";
                    } else {
                        format = transformDate ? function.apply(value) : value;
                    }
//                    log.append(format).append(" | ");
                    logTmp.add(format.toString());
                } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
                    e.printStackTrace();
                }
            }
            logs.add(logTmp);
//            log.append("\n");
        }
        return formatLogs(logs);
    }

    private String formatLogs(List> logs) {
        List maxCharacterLength = findMaxCharacterLength(logs);
        List> adjustsLogs = adjusts(logs, maxCharacterLength);
        StringBuilder log = new StringBuilder();
        log.append("\n");
        for (List adjustsLog : adjustsLogs) {
            log.append("| ").append(String.join(" | ", adjustsLog)).append(" |\n");
        }
        log.append("\n");
        return log.toString();
    }

    private List findMaxCharacterLength(List> logs) {
//        List maxPositions = new ArrayList<>();
//        int columnSize = logs.get(0).size();
//        for (int i = 0; i < columnSize; i++) {
//            int maxColumn = 0;
//            for (List row : logs) {
//                String item = row.get(i);
//                int length = item.length();
//                maxColumn = Math.max(maxColumn, length);
//            }
//            maxPositions.add(maxColumn);
//        }
//        return maxPositions;
        return IntStream.range(0, logs.isEmpty() ? 0 : logs.get(0).size())
                .mapToObj(i -> logs.stream().mapToInt(row -> {
                    int count;
                    try {
                        count = row.get(i).length();
                    } catch (IndexOutOfBoundsException e) {
                        count = 0;
                    }
                    return count;
                }).max().orElse(0))
                .collect(Collectors.toList());
    }

    private List> adjusts(List> logs, List columnsSize) {
//        List> logsAdjusts = new ArrayList<>();
//        for (List log : logs) {
//            List logAdjusts = new ArrayList<>();
//            for (int i = 0; i < log.size(); i++) {
//                String item = log.get(i);
//                int length = item.length();
//                Integer expectedLength = columnsSize.get(i);
//                int repeatInt = expectedLength - length;
//                logAdjusts.add(item + " ".repeat(repeatInt));
//            }
//            logsAdjusts.add(logAdjusts);
//        }
//        return logsAdjusts;
        return logs.stream()
                .map(log -> IntStream.range(0, log.size())
                        .mapToObj(i -> {
                            String item = log.get(i);
                            int length = item.length();
                            int repeatInt = columnsSize.get(i) - length;
                            return item + " ".repeat(Math.max(0, repeatInt));
                        })
                        .collect(Collectors.toList()))
                .collect(Collectors.toList());
    }


    private > String showMapValue(T lists) {
        StringBuilder log = new StringBuilder();
        Class aClass = lists.get(0).getClass();
        Field[] fields = aClass.getDeclaredFields();
        List fieldsList = new ArrayList<>();
        for (Field f : fields) {
            String name = f.getName();
            String finalName = name.substring(0, 1).toUpperCase() + name.substring(1);
            String typeName = f.getType().getSimpleName();
            if (typeName.contains("Map")) {
                fieldsList.add("get" + finalName);
            }
        }
        LinkedHashSet keys = new LinkedHashSet<>();
        for (Object o : lists) {
            try {
                Method getKeysMethod = aClass.getMethod("getMap");
                keys.addAll(((LinkedHashMap) getKeysMethod.invoke(o)).keySet());
            } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
                e.printStackTrace();
            }
        }
        log.append("\n| ");
        for (String key : keys) {
            log.append(key).append(" | ");
        }
        log.append("\n");

        String name = fieldsList.get(0);
        Map> maps = new HashMap<>();
        for (String key : keys) {
            for (E o : lists) {
                try {
                    Method method = o.getClass().getDeclaredMethod(name, String.class);
                    Object value = method.invoke(o, key);
                    if (value != null) {
                        maps.put(key, (List) value);
                        break;
                    }
                } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
                    e.printStackTrace();
                }
            }
        }

        int maxSize = 0;
        for (String key : keys) {
            List list = maps.get(key);
            maxSize = Math.max(list.size(), maxSize);
        }

        for (int i = 0; i < maxSize; i++) {
            log.append(" | ");
            for (String key : keys) {
                List values = maps.get(key);
                String val = "";
                try {
                    val = values.get(i);
                } catch (IndexOutOfBoundsException e) {
                    // nothing to do
                }
                log.append(val).append(" | ");
            }
            log.append("\n");
        }
        return log.toString();
    }

    public Function getFormat() {
        return this::format;
    }

    private  O format(O dates) {
        if (dates instanceof String) {
            String date = (String) dates;
            if (!Strings.isNullOrEmpty(date)) {
                Pattern pattern = Pattern.compile("\\d{2,4}[-\\sa-zA-Z0-9]{4,6}\\d{2,4}|(Today)");
                // Accept format 'dd MMM yyyy', 'yyyy MMM dd', 'dd MM yyyy', 'dd-MM-yyyy'
                java.util.regex.Matcher matcher = pattern.matcher(date);
                if (matcher.find()) {
                    LocalDate now = LocalDate.now();
                    String group = matcher.group();
                    if (group.contains("Today")) {
                        return (O) "today dd MMM yyyy";
                    }
                    try {
                        LocalDate newDate = LocalDate.parse(group, DateTimeFormatter.ofPattern("dd MMM yyyy", Locale.ENGLISH));
                        if (now.minusDays(7L).isEqual(newDate)) {
                            return (O) "1WeekAgo dd MMM yyyy";
                        } else if (now.minusDays(3L).isEqual(newDate)) {
                            return (O) "3DaysAgo dd MMM yyyy";
                        } else if (now.minusDays(2L).isEqual(newDate)) {
                            return (O) "2DaysAgo dd MMM yyyy";
                        } else if (now.minusDays(1L).isEqual(newDate)) {
                            return (O) "yesterday dd MMM yyyy";
                        } else if (now.isEqual(newDate)) {
                            return (O) "today dd MMM yyyy";
                        } else if (now.plusDays(1L).isEqual(newDate)) {
                            return (O) "tomorrow dd MMM yyyy";
                        } else if (now.plusDays(2L).isEqual(newDate)) {
                            return (O) "in2Days dd MMM yyyy";
                        } else if (now.plusDays(3L).isEqual(newDate)) {
                            return (O) "in3Days dd MMM yyyy";
                        } else if (now.plusDays(4L).isEqual(newDate)) {
                            return (O) "in4Days dd MMM yyyy";
                        } else if (now.plusDays(5L).isEqual(newDate)) {
                            return (O) "in5Days dd MMM yyyy";
                        } else if (now.plusDays(6L).isEqual(newDate)) {
                            return (O) "in6Days dd MMM yyyy";
                        } else if (now.plusDays(7L).isEqual(newDate)) {
                            return (O) "nextWeek dd MMM yyyy";
                        } else if (now.plusDays(8L).isEqual(newDate)) {
                            return (O) "nextWeekAnd1Day dd MMM yyyy";
                        } else if (now.plusDays(9L).isEqual(newDate)) {
                            return (O) "nextWeekAnd2Days dd MMM yyyy";
                        } else if (now.plusDays(10L).isEqual(newDate)) {
                            return (O) "nextWeekAnd3Days dd MMM yyyy";
                        } else if (now.plusDays(11L).isEqual(newDate)) {
                            return (O) "nextWeekAnd4Days dd MMM yyyy";
                        } else if (now.plusDays(12L).isEqual(newDate)) {
                            return (O) "nextWeekAnd5Days dd MMM yyyy";
                        } else if (now.plusDays(13L).isEqual(newDate)) {
                            return (O) "nextWeekAnd6Days dd MMM yyyy";
                        } else if (now.plusDays(14L).isEqual(newDate)) {
                            return (O) "next2Weeks dd MMM yyyy";
                        } else if (now.plusDays(21L).isEqual(newDate)) {
                            return (O) "next3Weeks dd MMM yyyy";
                        } else if (now.plusDays(28L).isEqual(newDate)) {
                            return (O) "next4Weeks dd MMM yyyy";
                        } else if (now.plusMonths(1L).minusDays(1L).isEqual(newDate)) {
                            return (O) "nextMonth1DayAgo dd MMM yyyy";
                        } else if (now.plusMonths(1L).minusDays(2L).isEqual(newDate)) {
                            return (O) "nextMonth2DaysAgo dd MMM yyyy";
                        } else if (now.plusMonths(1L).minusDays(3L).isEqual(newDate)) {
                            return (O) "nextMonth3DaysAgo dd MMM yyyy";
                        } else if (now.plusMonths(1L).plusDays(1L).isEqual(newDate)) {
                            return (O) "nextMonthAnd1Day dd MMM yyyy";
                        } else if (now.plusMonths(1L).plusDays(2L).isEqual(newDate)) {
                            return (O) "nextMonthAnd2Days dd MMM yyyy";
                        } else if (now.plusMonths(1L).isEqual(newDate)) {
                            return (O) "nextMonth dd MMM yyyy";
                        } else if (now.plusMonths(6L).minusDays(1L).isEqual(newDate)) {
                            return (O) "next6Months1DayAgo dd MMM yyyy";
                        } else if (now.plusMonths(6L).minusDays(2L).isEqual(newDate)) {
                            return (O) "next6Months2DaysAgo dd MMM yyyy";
                        } else if (now.plusYears(1L).minusDays(1L).isEqual(newDate)) {
                            return (O) "nextYear1DayAgo dd MMM yyyy";
                        } else if (now.plusYears(1L).isEqual(newDate)) {
                            return (O) "nextYear dd MMM yyyy";
                        }
                    } catch (DateTimeParseException e) {
                        String format = getKeyFromStorage(date);
                        return (O) format;
                    }
                } else {
                    String format = getKeyFromStorage(date);
                    return (O) format;
                }
            }
        }
        return dates;
    }

    public String getKeyFromStorage(String date) {
        String valueTmp;
        boolean isCopied = false;
        if (date.equals(".")) {
            return date;
        } else if (date.contains(".")) {
            valueTmp = date.split("\\.")[0];
            isCopied = true;
        } else {
            valueTmp = date;
        }
        if (storage != null) {
            String key = storage.getKey(valueTmp);
            if (Strings.isNullOrEmpty(key)) {
                key = storage.getKey(storage.getKey(valueTmp));
            }
            String value;
            if (key == null) {
                value = date;
            } else {
                if (date.contains(".")) {
                    if (isCopied) {
                        value = date.replace(valueTmp, "{" + key + "}");
                    } else {
                        value = date.replace(key, "{" + key + "}");
                    }
                } else {
                    value = "{" + key + "}";
                }
            }
            return value;
        } else {
            return date;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy