In many of the test scenarios, we need to validate the UI text where the text or partial of it is dynamic. In such cases, we need to use regular expressions to validate the text. Below are few example which can help you.
Example - 1
Suppose you want to validate a pattern 'CHICAGO, IL 60603 (1 mi)'. Where 'CHICAGO, IL' is the location and it follows with a zip and in braces single digit indicating distance in miles. If in this string the location string is as per the preferences or settings that you have made in your application and other part of the string you need to validate, you can write a regular expression as below:
Example - 2
If you want to validate a phone number text in the UI with a certain format, for example '(866) 825-3227' then you can use below regular expression
Lets assume you are validating some string with days and time, example 'Mon - Fri: 7:00 AM - 6:30 PM', you can use the regular expression in the following way
For more information, please refer here and also you can check the Pattern class for other available functionalities.
Example - 1
Suppose you want to validate a pattern 'CHICAGO, IL 60603 (1 mi)'. Where 'CHICAGO, IL' is the location and it follows with a zip and in braces single digit indicating distance in miles. If in this string the location string is as per the preferences or settings that you have made in your application and other part of the string you need to validate, you can write a regular expression as below:
public static boolean validateLocation(String location, String locationWithZipAndDistance) { String locationPattern = location + " [0-9]{5} \\([0-9]{1} mi\\)"; // e.g. CHICAGO, IL 60603 (1 mi) // where value of 'location' is 'CHICAGO, IL' Pattern pattern = Pattern.compile(locationPattern, Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(locationWithZipAndDistance); return matcher.matches(); }Usage:
String searchLocation = "CHICAGO, IL 60603 (1 mi)"; String location = "CHICAGO, IL"; System.out.println(validateLocation(searchLocation, location));// output: trueIn the above example it will return true if the input string is in either case as it has compiled with CASE_INSENSITIVE pattern.
Example - 2
If you want to validate a phone number text in the UI with a certain format, for example '(866) 825-3227' then you can use below regular expression
public static boolean validatePhone(String phone) { String phonePattern = "\\([0-9]{3}\\) [0-9]{3}\\-[0-9]{4}"; // e.g. (866) 825-3227 Pattern pattern = Pattern.compile(phonePattern); Matcher matcher = pattern.matcher(phone); return matcher.matches(); }Usage:
String ph = "(866) 825-3227"; System.out.println(validatePhone(ph));// output: trueExample-3
Lets assume you are validating some string with days and time, example 'Mon - Fri: 7:00 AM - 6:30 PM', you can use the regular expression in the following way
public static boolean validateOfficehours(String officeHours) { String officeHourPattern = "(?:Sun|Mon|Tue|Wed|Thu|Fri|Sat) - (?:Sun|Mon|Tue|Wed|Thu|Fri|Sat): (?:([0-9]{1,2}:[0-9]{2} (?:AM|PM) - [0-9]{1,2}:[0-9]{2} (?:AM|PM))|Closed)"; // e.g. Mon - Fri: 7:00 AM - 6:30 PM // e.g. Sat - Sun: Closed Pattern pattern = Pattern.compile(officeHourPattern); Matcher matcher = pattern.matcher(officeHours); return matcher.matches(); }Usage:
String officeHoursWeekday = "Mon - Fri: 7:00 AM - 6:30 PM"; String officeHoursWeekend = "Sat - Sun: Closed"; System.out.println(validateOfficehours(officeHoursWeekday));// output: true System.out.println(validateOfficehours(officeHoursWeekend));// output: trueThese are very basic scenarios, using these you can validate maximum of the dynamic texts. Feel free to ask me if you face any different scenarios which need my help.
For more information, please refer here and also you can check the Pattern class for other available functionalities.