Package core.helpers
Class UtilityHelper
- java.lang.Object
-
- core.helpers.UtilityHelper
-
public class UtilityHelper extends Object
-
-
Constructor Summary
Constructors Constructor Description UtilityHelper()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidaddFileToZip(String path, String srcFile, ZipOutputStream zip)static voidappendToFile(String value, String absolutePath)appends to existing filestatic voidappendToFile(String value, String directory, String filename, String type)appends to existing filestatic FilecaptureElementPicture(EnhancedBy element)Gets a picture of specific element displayed on the pagestatic FilecaptureElementPicture(EnhancedBy element, int index)Gets a picture of specific element displayed on the pagestatic voidcaptureReportScreenshot()captures screenshot And attaches to extent test reportstatic StringconvertListToString(ArrayList<String> list)static StringconvertListToString(ArrayList<String> list, String separator)static URLconvertToUrl(String url)converts url string to url objectstatic voidcopyDirectory(String dirFrom, String dirTo)Copies directory And all content from dirFrom to dirTo overwrites the contentstatic FilecreateFileFromPath(String absolutePath)create directories and files based on absolute path set permissions to rw-r--r-- set executable to truestatic ZipOutputStreamcreateZip(String destZipFile)static voiddeleteFile(String absolutePath)delete file or directorystatic 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 windowsstatic 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 windowsstatic voidexecuteJavascript(String script)static ObjectexecuteJs(String script, Object... args)execute javascriptstatic List<String>executeJsWithListReturn(String script, Object... args)execute javascriptstatic StringexecuteJsWithStringReturn(String script, Object... args)execute javascriptstatic StringgenerateRandomInteger(int len)generates random int of length lenstatic intgenerateRandomNumber(int min, int max)generates random number between two numbers, min, maxstatic StringgenerateRandomString(int len)generates random string of length lenstatic StringgenerateUUID()generate uuidstatic StringgenerateUUID(boolean includeDash)generate uuidstatic StringgenerateUUID(int length)generate uuidstatic StringgenerateUUID(int length, boolean includeDash)generate uuidstatic List<String>getAllValuesStartringWith(String source, String value)gets a list of string from 'source' starting with 'value'static booleangetConfigBooleanValue(String value)static doublegetConfigDoubleValue(String value)static intgetConfigIntValue(String value)static ObjectgetConfigObjectValue(String value)static StringgetConfigValue(String value)static doublegetDoubleFromString(String value, boolean isFailOnNoInt)get numeric value from stringstatic FilegetFile(String directoryPath)get file from file pathstatic FilegetFileByName(String path, String filename)get file by namestatic FilegetFileByName(String path, String filename, boolean includeSubDir)get file by namestatic StringgetFileContent(String absolutePath)gets file content as Stringstatic StringgetFileContent(String absolutePath, boolean verifyFileExists)gets file content as Stringstatic ArrayList<File>getFileList(String directoryPath)returns the list of files in directorystatic ArrayList<File>getFileList(String directoryPath, boolean includeSubDir)returns the list of files in directorystatic ArrayList<File>getFileList(String directoryPath, ArrayList<File> files)returns the list of files in directorystatic ArrayList<File>getFileListByType(String directoryPath, String type)gets the list of files tye: file type.static ArrayList<File>getFileListByType(String directoryPath, String type, boolean includeSubDir)gets the list of files tye: file type.static List<File>getFileListWithSubfolders(String directoryName, String type, List<File> files)gets list of files including from sub folder based on type.static List<File>getFileListWithSubfolders(String directoryName, List<File> files)static intgetFirstNumber(String value)static StringgetFullPath(String path)gets full path from relative path relative path is from root directory ( where pom.xml file is located )static intgetIntFromString(String value, boolean isFailOnNoInt)get numeric value from stringstatic List<String>getLinesInFileContainingWith(String value, File file)get line in file containing stringstatic List<String>getLinesInFileStartingWith(String value, File file)get line in file starting with stringstatic StringgetMavenArtifactId()static StringgetMavenDependencyVersion(String dependency)static StringgetPageSource()gets page sourcestatic StringgetRootDir()get current project root directory, where pom.xml isstatic StringgetValueBetweenStrings(String source, String value1, String value2, int position)get value between 2 strings.static StringgetValueFromPattern(String str, String pattern)static List<String>getValuesFromPattern(String str, String pattern)static voidhighLightWebElement(EnhancedBy by, int index)highlights the web element use on clicks and send key elements applicable to web driverstatic booleanisBoolean(String value)returns if string is booleanstatic booleanisFileContainString(String value, File file)does file contain the given text valuestatic booleanisFilenameInDir(File dir, String name)static booleanisLineInFileStartWithString(String value, File file)is line in file start with stringstatic booleanisMac()returns true if OS is macstatic booleanisNumeric(String str)return if string is a numberstatic booleanisProcessRunning(String serviceName)returns true if process is runningstatic booleanisProxyRequired(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 falsestatic booleanisServerOnline(String url)checks if server is onlinestatic booleanisServerOnline(String url, String proxyUrl)checks if server is onlinestatic booleanisStringContainNumber(String value)does string have int valuestatic booleanisStringContainOnlyNumber(String value)does string have only numeric valuestatic booleanisUnix()returns true if OS is unix or linuxstatic booleanisUrlAbleToConnect(URL source, Proxy proxy)check if able to connect to source urlstatic booleanisUUID(String value)is the string value UDIDstatic booleanisWindows()returns true if OS is windowsstatic voidkillMacProcess(String serviceName)static voidkillWindowsProcess(String serviceName)returns kills the process if runningstatic voidprintPageSource()prints page source to consolestatic StringreadFileContent(String absolutePath)gets file content as Stringstatic StringremoveSurroundingQuotes(String value)remove surrounding double quotes from the stringstatic booleansetProxyAutoDetection(URL source)checks if proxy is required first attempt without proxy, second with proxy, checking the validity of the proxy connectionstatic voidslackNotificationWithFile(String title, String comment, String filePath)sends slack notification token: generate at: https://api.slack.com/tutorials/tracks/getting-a-token.static voidslackNotificationWithFile(String title, String passed, String failed, String skipped, String filePath)static voidslackNotificationWithFile(String title, String passed, String failed, String skipped, String comment, String filePath)static voidslackNotificationWithFile(String token, String channelId, String title, String passed, String failed, String skipped, String comment, String filePath)slack notification with and without file attachmentstatic StringstringNormalize(String value)normalizes string removes space, new line chars, quotesstatic StringstringRemoveLines(String value)normalizes string removes space, new line charsstatic voiduploadSlackFile(String token, List<String> channel, String filePath, String message)static voidwriteFile(String value, String absolutePath)Create file (where pom.xml is) and write to itstatic voidwriteFile(String value, String directory, String filename, String type)Create file with path starting from root directory (where pom.xml is) and write to it.static StringzipDir(String sourceDir, String destFilePath)zips directory path starts from root pom directorystatic ZipOutputStreamzipFolder(String srcFolder, String destZipFile)zip folder
-
-
-
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
-
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)
-
zipFolder
public static ZipOutputStream zipFolder(String srcFolder, String destZipFile) throws Exception
zip folder- Parameters:
srcFolder-destZipFile-- Returns:
- Throws:
Exception
-
createZip
public static ZipOutputStream createZip(String destZipFile) throws FileNotFoundException
- Throws:
FileNotFoundException
-
addFileToZip
public static void addFileToZip(String path, String srcFile, ZipOutputStream zip) throws Exception
- Throws:
Exception
-
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:
-
slackNotificationWithFile
public static void slackNotificationWithFile(String title, String comment, String filePath)
sends slack notification token: generate at: https://api.slack.com/tutorials/tracks/getting-a-token. slack token starts with xoxb. create app, go to authorization properties file for values to use
-
slackNotificationWithFile
public static void slackNotificationWithFile(String title, String passed, String failed, String skipped, String filePath)
-
slackNotificationWithFile
public static void slackNotificationWithFile(String title, String passed, String failed, String skipped, String comment, String filePath)
-
slackNotificationWithFile
public static void slackNotificationWithFile(String token, String channelId, String title, String passed, String failed, String skipped, String comment, String filePath)
slack notification with and without file attachment- Parameters:
token-channelId-title-passed-failed-skipped-comment-filePath-
-
uploadSlackFile
public static void uploadSlackFile(String token, List<String> channel, String filePath, String message) throws IOException, com.slack.api.methods.SlackApiException
- Throws:
IOExceptioncom.slack.api.methods.SlackApiException
-
zipDir
public static String zipDir(String sourceDir, String destFilePath)
zips directory path starts from root pom directory- Parameters:
sourceDir- : root path + dirdestFilePath- : 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 filedirectory- directory from rootfilename- name of the filetype- 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()
-
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:
-
convertListToString
public static String convertListToString(ArrayList<String> list, String separator)
-
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:
-
getConfigBooleanValue
public static boolean getConfigBooleanValue(String value)
-
getConfigIntValue
public static int getConfigIntValue(String value)
-
getConfigDoubleValue
public static double getConfigDoubleValue(String value)
-
-