Wednesday, July 27, 2016

Getting the name of the current executing method

String methodName = new Object(){}.getClass().getEnclosingMethod().getName();
(OR)
String methodName = Thread.currentThread().getStackTrace()[1].getMethodName();
 

 
 
 
 
 

Monday, July 25, 2016

Currency and Phone Number Format Check?


public String currencyFormatCheck(String location, int currencyValue) {
if (location.equalsIgnoreCase("US")) {
Locale currentLocale = Locale.US;
Double currencyAmount = new Double(currencyValue);
NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance(currentLocale);
return currencyFormatter.format(currencyAmount);
} else if (location.equalsIgnoreCase("UK")) {
Locale currentLocale = Locale.UK;
Double currencyAmount = new Double(currencyValue);
NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance(currentLocale);
return currencyFormatter.format(currencyAmount);
}
return null;
}
 
private static boolean phoneNumberFormatCheck(String phoneNo) {
// validate phone numbers of format "1234567890"
if (phoneNo.matches("\\d{10}"))
return true;
// validating phone number with -, . or spaces
else if (phoneNo.matches("\\d{3}[-\\.\\s]\\d{3}[-\\.\\s]\\d{4}"))
return true;
// validating phone number with extension length from 3 to 5
else if (phoneNo.matches("\\d{3}-\\d{3}-\\d{4}\\s(x|(ext))\\d{3,5}"))
return true;
// validating phone number where area code is in braces ()
else if (phoneNo.matches("\\(\\d{3}\\)-\\d{3}-\\d{4}"))
return true;
// return false if nothing matches the input
else
return false;
}

//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();
}

Tuesday, July 5, 2016

Resizing a web element using movebyoffset

public void resize(WebElement elementToResize, int xOffset, int yOffset) {
        try {
        if (elementToResize.isDisplayed()) {
        Actions action = new Actions(driver);
        action.clickAndHold(elementToResize).moveByOffset(xOffset, yOffset).release().build().perform();
        } else {
        System.out.println("Element was not displayed to drag");
        }
        } catch (StaleElementReferenceException e) {
        System.out.println("Element with " + elementToResize + "is not attached to the page document "  + e.getStackTrace());
        } catch (NoSuchElementException e) {
        System.out.println("Element " + elementToResize + " was not found in DOM " + e.getStackTrace());
        } catch (Exception e) {
        System.out.println("Unable to resize" + elementToResize + " - " + e.getStackTrace());
        }
        }

(OR)

moveToElement(WebElement toElement, int xOffset, int yOffset)
Moves the mouse to an offset from the top-left corner of the element.
 
Actions builder = new Actions(driver);   
builder.moveToElement(knownElement, 10, 25).click().build().perform();