Package core.helpers

Class UtilityHelper


  • public class UtilityHelper
    extends Object
    • Constructor Detail

      • UtilityHelper

        public UtilityHelper()
    • Method Detail

      • generateRandomString

        public static String generateRandomString​(int len)
        generates random string of length len
        Parameters:
        len -
        Returns:
      • generateRandomInteger

        public static String generateRandomInteger​(int len)
        generates random int of length len
        Parameters:
        len -
        Returns:
      • generateRandomNumber

        public static int generateRandomNumber​(int min,
                                               int max)
        generates random number between two numbers, min, max
        Parameters:
        min -
        max -
        Returns:
      • generateUUID

        public static String generateUUID()
        generate uuid
        Returns:
      • generateUUID

        public static String generateUUID​(boolean includeDash)
        generate uuid
        Parameters:
        includeDash -
        Returns:
      • generateUUID

        public static String generateUUID​(int length)
        generate uuid
        Parameters:
        length -
        Returns:
      • generateUUID

        public static String generateUUID​(int length,
                                          boolean includeDash)
        generate uuid
        Parameters:
        length -
        Returns:
      • stringNormalize

        public static String stringNormalize​(String value)
        normalizes string removes space, new line chars, quotes
        Parameters:
        value -
        Returns:
      • stringRemoveLines

        public static String stringRemoveLines​(String value)
        normalizes string removes space, new line chars
        Parameters:
        value -
        Returns:
      • isProcessRunning

        public static boolean isProcessRunning​(String serviceName)
                                        throws Exception
        returns true if process is running
        Parameters:
        serviceName -
        Returns:
        Throws:
        Exception
      • killWindowsProcess

        public static void killWindowsProcess​(String serviceName)
        returns kills the process if running
        Parameters:
        serviceName -
        Throws:
        Exception
      • killMacProcess

        public static void killMacProcess​(String serviceName)
      • executeCommand

        public static ArrayList<String> executeCommand​(String command)
        run command and return results as array list will run bash on linux or mac will run batch command on windows
        Parameters:
        command -
        Returns:
        Throws:
        IOException
      • excuteCommandFromFile

        public static ArrayList<String> excuteCommandFromFile​(String filePath)
        run script file and return results as array list will run bash on linux or mac will run batch command on windows
        Parameters:
        filePath - path from the root directory ( where pom.xml is )
        Returns:
        the results as arraylist
      • runShellCommand

        public static String runShellCommand​(String command,
                                             String timeoutSeconds)
        run shell command
        Parameters:
        command -
        timeoutSeconds -
        Returns:
      • runShellScriptFromFile

        public static String runShellScriptFromFile​(String scriptPath,
                                                    String timeoutSeconds)
        script path relative to root path where pom.xml is located
        Parameters:
        scriptPath -
        timeoutSeconds -
        Returns:
      • copyDirectory

        public static void copyDirectory​(String dirFrom,
                                         String dirTo)
        Copies directory And all content from dirFrom to dirTo overwrites the content
        Parameters:
        dirFrom -
        dirTo -
      • executeJavascript

        public static void executeJavascript​(String script)
      • getAllValuesStartringWith

        public static List<String> getAllValuesStartringWith​(String source,
                                                             String value)
        gets a list of string from 'source' starting with 'value'
        Parameters:
        source -
        value -
        Returns:
      • getValueFromPattern

        public static String getValueFromPattern​(String str,
                                                 String pattern)
        Parameters:
        str -
        pattern - regular expression pattern
        Returns:
      • getValuesFromPattern

        public static List<String> getValuesFromPattern​(String str,
                                                        String pattern)
        Parameters:
        str -
        pattern - regular expression pattern
        Returns:
      • getRootDir

        public static String getRootDir()
        get current project root directory, where pom.xml is
        Returns:
      • isFilenameInDir

        public static boolean isFilenameInDir​(File dir,
                                              String name)
      • slackNotificationWithFile

        public static void slackNotificationWithFile​(String title,
                                                     String comment,
                                                     String filePath)
        TODO: replace slack api with newer library sends slack notification token: generate at: https://api.slack.com/custom-integrations/legacy-tokens for channel id: right click And channel And copy link. the id is attached to the link see properties file for values to use
      • zipDir

        public static String zipDir​(String sourceDir,
                                    String destFilePath)
        zips directory path starts from root pom directory
        Parameters:
        sourceDir - : root path + dir
        destFilePath - : usage: root + dir + outputFilePath
        Returns:
      • getFileListByType

        public static ArrayList<File> getFileListByType​(String directoryPath,
                                                        String type)
        gets the list of files tye: file type. eg. ".csv"
        Returns:
      • getFileListByType

        public static ArrayList<File> getFileListByType​(String directoryPath,
                                                        String type,
                                                        boolean includeSubDir)
        gets the list of files tye: file type. eg. ".csv"
        Returns:
      • getFileList

        public static ArrayList<File> getFileList​(String directoryPath,
                                                  boolean includeSubDir)
        returns the list of files in directory
        Parameters:
        directoryPath -
        Returns:
      • getFileList

        public static ArrayList<File> getFileList​(String directoryPath,
                                                  ArrayList<File> files)
        returns the list of files in directory
        Parameters:
        directoryPath -
        Returns:
      • getFileList

        public static ArrayList<File> getFileList​(String directoryPath)
        returns the list of files in directory
        Parameters:
        directoryPath -
        Returns:
      • getFullPath

        public static String getFullPath​(String path)
        gets full path from relative path relative path is from root directory ( where pom.xml file is located )
        Parameters:
        path -
        Returns:
      • getFile

        public static File getFile​(String directoryPath)
        get file from file path
        Parameters:
        directoryPath -
        Returns:
      • getFileByName

        public static File getFileByName​(String path,
                                         String filename)
        get file by name
        Parameters:
        path -
        filename -
        Returns:
      • getFileByName

        public static File getFileByName​(String path,
                                         String filename,
                                         boolean includeSubDir)
        get file by name
        Parameters:
        path -
        filename -
        Returns:
      • getFileListWithSubfolders

        public static List<File> getFileListWithSubfolders​(String directoryName,
                                                           List<File> files)
      • getFileListWithSubfolders

        public static List<File> getFileListWithSubfolders​(String directoryName,
                                                           String type,
                                                           List<File> files)
        gets list of files including from sub folder based on type. eg. ".csv"
        Returns:
      • readFileContent

        public static String readFileContent​(String absolutePath)
        gets file content as String
        Parameters:
        absolutePath -
        Returns:
      • getFileContent

        public static String getFileContent​(String absolutePath)
        gets file content as String
        Parameters:
        absolutePath -
        Returns:
      • getFileContent

        public static String getFileContent​(String absolutePath,
                                            boolean verifyFileExists)
        gets file content as String
        Parameters:
        absolutePath -
        Returns:
      • createFileFromPath

        public static File createFileFromPath​(String absolutePath)
        create directories and files based on absolute path set permissions to rw-r--r-- set executable to true
        Parameters:
        path -
        Returns:
      • writeFile

        public static void writeFile​(String value,
                                     String directory,
                                     String filename,
                                     String type)
        Create file with path starting from root directory (where pom.xml is) and write to it. eg. writeFile("something","", "myFile", "txt");
        Parameters:
        value - value in file
        directory - directory from root
        filename - name of the file
        type - type of file
      • writeFile

        public static void writeFile​(String value,
                                     String absolutePath)
        Create file (where pom.xml is) and write to it
        Parameters:
        value -
        absolutePath -
      • deleteFile

        public static void deleteFile​(String absolutePath)
        delete file or directory
        Parameters:
        absolutePath -
      • appendToFile

        public static void appendToFile​(String value,
                                        String absolutePath)
        appends to existing file
        Parameters:
        value -
        absolutePath -
      • appendToFile

        public static void appendToFile​(String value,
                                        String directory,
                                        String filename,
                                        String type)
        appends to existing file
        Parameters:
        value -
        directory -
        filename -
        type -
      • captureReportScreenshot

        public static void captureReportScreenshot()
        captures screenshot And attaches to extent test report
      • captureElementPicture

        public static File captureElementPicture​(EnhancedBy element)
        Gets a picture of specific element displayed on the page
        Parameters:
        element - The element
        Returns:
        File
        Throws:
        Exception
      • captureElementPicture

        public static File captureElementPicture​(EnhancedBy element,
                                                 int index)
        Gets a picture of specific element displayed on the page
        Parameters:
        element - The element
        Returns:
        File
        Throws:
        Exception
      • highLightWebElement

        public static void highLightWebElement​(EnhancedBy by,
                                               int index)
        highlights the web element use on clicks and send key elements applicable to web driver
        Parameters:
        by -
        index -
      • isMac

        public static boolean isMac()
        returns true if OS is mac
        Returns:
      • isWindows

        public static boolean isWindows()
        returns true if OS is windows
        Returns:
      • isUnix

        public static boolean isUnix()
        returns true if OS is unix or linux
        Returns:
      • isUUID

        public static boolean isUUID​(String value)
        is the string value UDID
        Parameters:
        value -
        Returns:
      • isBoolean

        public static boolean isBoolean​(String value)
        returns if string is boolean
        Parameters:
        value -
        Returns:
      • isNumeric

        public static boolean isNumeric​(String str)
        return if string is a number
        Parameters:
        str -
        Returns:
      • executeJs

        public static Object executeJs​(String script,
                                       Object... args)
        execute javascript
        Parameters:
        script -
      • executeJsWithListReturn

        public static List<String> executeJsWithListReturn​(String script,
                                                           Object... args)
        execute javascript
        Parameters:
        script -
      • executeJsWithStringReturn

        public static String executeJsWithStringReturn​(String script,
                                                       Object... args)
        execute javascript
        Parameters:
        script -
      • getIntFromString

        public static int getIntFromString​(String value,
                                           boolean isFailOnNoInt)
        get numeric value from string
        Parameters:
        value -
        isFailOnNoInt -
        Returns:
      • getFirstNumber

        public static int getFirstNumber​(String value)
      • getDoubleFromString

        public static double getDoubleFromString​(String value,
                                                 boolean isFailOnNoInt)
        get numeric value from string
        Parameters:
        value -
        isFailOnNoInt -
        Returns:
      • isStringContainOnlyNumber

        public static boolean isStringContainOnlyNumber​(String value)
        does string have only numeric value
        Parameters:
        value -
        Returns:
      • isStringContainNumber

        public static boolean isStringContainNumber​(String value)
        does string have int value
        Parameters:
        value -
        Returns:
      • removeSurroundingQuotes

        public static String removeSurroundingQuotes​(String value)
        remove surrounding double quotes from the string
        Parameters:
        value -
        Returns:
      • convertToUrl

        public static URL convertToUrl​(String url)
        converts url string to url object
        Parameters:
        url -
        Returns:
      • isUrlAbleToConnect

        public static boolean isUrlAbleToConnect​(URL source,
                                                 Proxy proxy)
        check if able to connect to source url
        Parameters:
        source -
        Returns:
      • isProxyRequired

        public static boolean isProxyRequired​(URL url)
        get proxy state from proxy enabled config values: true, false, auto if auto is set, then through proxy detection, we set value to true or false
        Parameters:
        url -
        Returns:
      • setProxyAutoDetection

        public static boolean setProxyAutoDetection​(URL source)
        checks if proxy is required first attempt without proxy, second with proxy, checking the validity of the proxy connection
        Parameters:
        source -
        Returns:
      • getMavenArtifactId

        public static String getMavenArtifactId()
      • getMavenDependencyVersion

        public static String getMavenDependencyVersion​(String dependency)
      • isFileContainString

        public static boolean isFileContainString​(String value,
                                                  File file)
        does file contain the given text value
        Parameters:
        value -
        file -
        Returns:
      • isLineInFileStartWithString

        public static boolean isLineInFileStartWithString​(String value,
                                                          File file)
        is line in file start with string
        Parameters:
        value -
        file -
        Returns:
      • getLinesInFileStartingWith

        public static List<String> getLinesInFileStartingWith​(String value,
                                                              File file)
        get line in file starting with string
        Parameters:
        value -
        file -
        Returns:
      • getLinesInFileContainingWith

        public static List<String> getLinesInFileContainingWith​(String value,
                                                                File file)
        get line in file containing string
        Parameters:
        value -
        file -
        Returns:
      • printPageSource

        public static void printPageSource()
        prints page source to console
      • getPageSource

        public static String getPageSource()
        gets page source
        Returns:
      • getValueBetweenStrings

        public static String getValueBetweenStrings​(String source,
                                                    String value1,
                                                    String value2,
                                                    int position)
        get value between 2 strings. eg. value . and are the 2 values passed.
        Parameters:
        source -
        value1 -
        value2 -
        position -
        Returns:
      • isServerOnline

        public static boolean isServerOnline​(String url)
        checks if server is online
        Parameters:
        url -
        proxyUrl -
        Returns:
      • isServerOnline

        public static boolean isServerOnline​(String url,
                                             String proxyUrl)
        checks if server is online
        Parameters:
        url -
        proxyUrl -
        Returns: