Package core.helpers
Class UtilityHelper
java.lang.Object
core.helpers.UtilityHelper
public class UtilityHelper extends Object
-
Constructor Summary
Constructors Constructor Description UtilityHelper() -
Method Summary
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 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)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.static StringstringNormalize(String value)normalizes string removes space, new line chars, quotesstatic StringstringRemoveLines(String value)normalizes string removes space, new line charsstatic 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
-
Constructor Details
-
UtilityHelper
public UtilityHelper()
-
-
Method Details
-
generateRandomString
generates random string of length len- Parameters:
len-- Returns:
-
generateRandomInteger
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
generate uuid- Returns:
-
generateUUID
generate uuid- Parameters:
includeDash-- Returns:
-
generateUUID
generate uuid- Parameters:
length-- Returns:
-
generateUUID
generate uuid- Parameters:
length-- Returns:
-
stringNormalize
normalizes string removes space, new line chars, quotes- Parameters:
value-- Returns:
-
stringRemoveLines
normalizes string removes space, new line chars- Parameters:
value-- Returns:
-
isProcessRunning
returns true if process is running- Parameters:
serviceName-- Returns:
- Throws:
Exception
-
killWindowsProcess
returns kills the process if running- Parameters:
serviceName-- Throws:
Exception
-
killMacProcess
-
executeCommand
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
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
Copies directory And all content from dirFrom to dirTo overwrites the content- Parameters:
dirFrom-dirTo-
-
executeJavascript
-
zipFolder
zip folder- Parameters:
srcFolder-destZipFile-- Returns:
- Throws:
Exception
-
createZip
- Throws:
FileNotFoundException
-
addFileToZip
- Throws:
Exception
-
getAllValuesStartringWith
gets a list of string from 'source' starting with 'value'- Parameters:
source-value-- Returns:
-
getValueFromPattern
- Parameters:
str-pattern- regular expression pattern- Returns:
-
getValuesFromPattern
- Parameters:
str-pattern- regular expression pattern- Returns:
-
getRootDir
get current project root directory, where pom.xml is- Returns:
-
isFilenameInDir
-
slackNotificationWithFile
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
zips directory path starts from root pom directory- Parameters:
sourceDir- : root path + dirdestFilePath- : usage: root + dir + outputFilePath- Returns:
-
getFileListByType
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
returns the list of files in directory- Parameters:
directoryPath-- Returns:
-
getFileList
returns the list of files in directory- Parameters:
directoryPath-- Returns:
-
getFileList
returns the list of files in directory- Parameters:
directoryPath-- Returns:
-
getFullPath
gets full path from relative path relative path is from root directory ( where pom.xml file is located )- Parameters:
path-- Returns:
-
getFile
get file from file path- Parameters:
directoryPath-- Returns:
-
getFileByName
get file by name- Parameters:
path-filename-- Returns:
-
getFileByName
get file by name- Parameters:
path-filename-- Returns:
-
getFileListWithSubfolders
-
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
gets file content as String- Parameters:
absolutePath-- Returns:
-
getFileContent
gets file content as String- Parameters:
absolutePath-- Returns:
-
getFileContent
gets file content as String- Parameters:
absolutePath-- Returns:
-
createFileFromPath
create directories and files based on absolute path set permissions to rw-r--r-- set executable to true- Parameters:
path-- Returns:
-
writeFile
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
Create file (where pom.xml is) and write to it- Parameters:
value-absolutePath-
-
deleteFile
delete file or directory- Parameters:
absolutePath-
-
appendToFile
appends to existing file- Parameters:
value-absolutePath-
-
appendToFile
appends to existing file- Parameters:
value-directory-filename-type-
-
captureReportScreenshot
public static void captureReportScreenshot()captures screenshot And attaches to extent test report -
captureElementPicture
Gets a picture of specific element displayed on the page- Parameters:
element- The element- Returns:
- File
- Throws:
Exception
-
captureElementPicture
Gets a picture of specific element displayed on the page- Parameters:
element- The element- Returns:
- File
- Throws:
Exception
-
highLightWebElement
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
is the string value UDID- Parameters:
value-- Returns:
-
isBoolean
returns if string is boolean- Parameters:
value-- Returns:
-
isNumeric
return if string is a number- Parameters:
str-- Returns:
-
executeJs
execute javascript- Parameters:
script-
-
executeJsWithListReturn
execute javascript- Parameters:
script-
-
executeJsWithStringReturn
execute javascript- Parameters:
script-
-
getIntFromString
get numeric value from string- Parameters:
value-isFailOnNoInt-- Returns:
-
getFirstNumber
-
getDoubleFromString
get numeric value from string- Parameters:
value-isFailOnNoInt-- Returns:
-
isStringContainOnlyNumber
does string have only numeric value- Parameters:
value-- Returns:
-
isStringContainNumber
does string have int value- Parameters:
value-- Returns:
-
removeSurroundingQuotes
remove surrounding double quotes from the string- Parameters:
value-- Returns:
-
convertToUrl
converts url string to url object- Parameters:
url-- Returns:
-
isUrlAbleToConnect
check if able to connect to source url- Parameters:
source-- Returns:
-
isProxyRequired
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
checks if proxy is required first attempt without proxy, second with proxy, checking the validity of the proxy connection- Parameters:
source-- Returns:
-
getMavenArtifactId
-
getMavenDependencyVersion
-
isFileContainString
does file contain the given text value- Parameters:
value-file-- Returns:
-
isLineInFileStartWithString
is line in file start with string- Parameters:
value-file-- Returns:
-
getLinesInFileStartingWith
get line in file starting with string- Parameters:
value-file-- Returns:
-
getLinesInFileContainingWith
get line in file containing string- Parameters:
value-file-- Returns:
-
printPageSource
public static void printPageSource()prints page source to console -
getPageSource
gets page source- Returns:
-
convertListToString
-
convertListToString
-
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
checks if server is online- Parameters:
url-proxyUrl-- Returns:
-
isServerOnline
checks if server is online- Parameters:
url-proxyUrl-- Returns:
-