Monday 25 May 2020

Enums in java:

Enums in java:
---------------
An enum is a special "class" that represents a group of constants (unchangeable variables, like final variables).

To create an enum, use the enum keyword (instead of class or interface), and separate the constants with a comma.

public enum TestCaseActivity {

PASS("1"),//public static final TCSTATUS PASS;
FAIL("2"),
SKIP("3"),
MEDIUM_WAIT("3000"),
HIGH_WAIT("6000"),
LOW_WAIT("1000"),
IE("ie"),
CHROME("chrome"),
FIREFOX("firefox"),
EDGE("edge"),
PHANTOMJS("phantom");
private String activity;
private TestCaseActivity(String activity){
this.activity = activity;
}
public String getActivity() {
return activity;
}
}

RegEx Validations utility for PhNum:

RegEx Validations utility for PhNum:
------------------------------------
public interface RegExPhNum {

public static boolean isValidPhNum(String phNum){
boolean flag = true;
String pattern = "(0|91)?[6-9][0-9]{9}";
Pattern pattern2 = Pattern.compile(pattern);
Matcher matcher = pattern2.matcher(phNum);
if (matcher.find()&&matcher.group().equalsIgnoreCase(phNum)) {
System.out.println("It is valid ph num");
}
else{
System.out.println("It is invalid ph num");
flag = false;
}
return flag;
}
}

Current Date Utility Function:

Current Date Utility Function:
------------------------------
public class DateUtility{

static public void selectCurDate(String xpath) throws InterruptedException {
      List<WebElement> dates = getDriver().findElements(By.xpath(xpath));
Date date = new Date();
String curDate = date.getDate()+"";
for(WebElement ele : dates){
String dateTxt = ele.getText();
if (curDate.equalsIgnoreCase(dateTxt)) {
ele.click();
break;
}
}
}
}

Screenshot Utility Function:

Screenshot Utility Function:
----------------------------

In order to take a screenshot we need the following classes & interfaces
1. TakesScreenshot(I)
2. WebDriver(I) | RemoteWebDriver(C)
3. OutputType(I)
4. FileUtils(C)

From java1.8 version onwards inside interface we can take implemented methods as well in the form of default methods and static methods.

public interface CaptureScreenshot {

public static String captureImage(String tcName){
String imgLoc = BaseTest.getProjctPath()+"\\Screenshots\\"+tcName+".jpeg";
TakesScreenshot ts = (TakesScreenshot) BaseTest.getDriver();
File image = ts.getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(image, new File(imgLoc));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return imgLoc;
}
public static void captureImage(String tcName,String description){
TakesScreenshot ts = (TakesScreenshot) BaseTest.getDriver();
File image = ts.getScreenshotAs(OutputType.FILE);
try {
FileUtils.copyFile(image, new File(BaseTest.getProjctPath()+"\\Screenshots\\"+tcName+".jpeg"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}