Programs
Programs
sendKeys("SoftwareTestingHelp");
element.sendKeys(Keys.ENTER);
java.util.List<WebElement> link =
driver.findElements(By.tagName("a"));
System.out.println(link.size());
package codes;
public class SecondHighestNumberInArray {
public static void main(String[] args)
{
int arr[] = { 100,14, 46, 47, 94, 94, 52, 86, 36, 94, 89 };
int largest = 0;
int secondLargest = 0;
System.out.println("The given array is:");
for (int i = 0; i < arr.length; i++)
{
System.out.print(arr[i] + "\t");
}
for (int i = 0; i < arr.length; i++)
{
if (arr[i] > largest)
{
secondLargest = largest;
largest = arr[i];
}
else if (arr[i] > secondLargest)
{
secondLargest = arr[i];
}
}
System.out.println("\nSecond largest number is:" + secondLargest);
System.out.println("Largest Number is: " +largest);
}
}
class Armstrong{
public static void main(String[] args) {
int c=0,a,temp;
int n=153;//It is the number to check Armstrong
temp=n;
while(n>0)
{
a=n%10;
n=n/10;
c=c+(a*a*a);
}
if(temp==c)
System.out.println("armstrong number");
else
System.out.println("Not armstrong number");
}
}
class RemoveWhiteSpaces
{
public static void main(String[] args)
{
String str1 = "Saket Saurav is a QualityAna list";
System.out.println(str2);
}
}
}
class RemoveWhiteSpaces
{
public static void main(String[] args)
{
String str1 = "Saket Saurav is an Autom ation Engi ne er";
@Test
public void ReadData() throws IOException
{
// Import excel sheet from a webdriver directory which is inside c drive.
//DataSource is the name of the excel
File src=new File("C:\\webdriver\\DataSource.xls");
//This step is to load the workbook of the excel which is done by global
HSSFWorkbook in which we have
//passed finput as an argument.
workbook = new HSSFWorkbook(finput);
}
}
Log 4j
PropertyConfigurator.configure("./src/test/resources/properties/
log4j.properties");
Page Object
public HomePageLocators home;
public HomePage(){
this.home = new HomePageLocators();
AjaxElementLocatorFactory factory = new
AjaxElementLocatorFactory(driver,10);
PageFactory.initElements(factory, this.home);
Fluent Wait
.withTimeout(30, TimeUnit.SECONDS)
.pollingEvery(5, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
1. presenceOfAllElementsLocatedBy()
2. presenceOfElementLocated()
3. textToBePresentInElement()
4. textToBePresentInElementLocated()
Explicit
File screenshot =
((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshot, new
File(".//screenshot//"+scrShotName));
package com.basicconcepts.java;
import java.util.HashMap;
package com.basicconcepts.java;
import java.util.Scanner;
stringReverseSplit();
System.out.println(str3);
String str;
Scanner in = new Scanner(System.in);
System.out.println("Enter your String");
str = in.nextLine();
String[] token = str.split(""); // used split method to print in
reverse order
for (int i = token.length - 1; i >= 0; i--) {
System.out.print(token[i] + "");
}
}
if(map.containsKey(split[i])) {
int count = map.get(split[i]);
System.out.println(count+split[i]);
map.put(split[i], count + 1);
}else {
map.put(split[i], 1);
}
map.entrySet().iterator();
}
System.out.println(map);
}
}
for(Map.Entry me2: map.entrySet()) {
System.out.println("Key is: " + me2.getKey() + " Value is: " +
me2.getValue());
}
}
public void getCountString() {
Scanner in = new Scanner(System.in);
System.out.println("Enter teh string");
String str = in.nextLine();
int count=0;
for(int i=0;i<str.length();i++) {
if(str.charAt(i)!=' ') {
count++;
}
}
System.out.println(count);
System.out.println(str.length());
}
str1 = str1.toLowerCase();
str2 = str2.toLowerCase();
Arrays.sort(char1);
Arrays.sort(char2);
}else {
System.out.println("The string is not anagram");
}
}
Excel
package com.w2a.utilities;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFCreationHelper;
import org.apache.poi.xssf.usermodel.XSSFFont;
import org.apache.poi.xssf.usermodel.XSSFHyperlink;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
this.path=path;
try {
sheet = workbook.getSheetAt(0);
fis.close();
} catch (Exception e) {
e.printStackTrace();
if(index==-1)
return 0;
else{
sheet = workbook.getSheetAt(index);
int number=sheet.getLastRowNum()+1;
return number;
try{
if(rowNum <=0)
return "";
int col_Num=-1;
if(index==-1)
return "";
sheet = workbook.getSheetAt(index);
row=sheet.getRow(0);
for(int i=0;i<row.getLastCellNum();i++){
//System.out.println(row.getCell(i).getStringCellValue().trim());
if(row.getCell(i).getStringCellValue().trim().equals(colName.trim()))
col_Num=i;
if(col_Num==-1)
return "";
sheet = workbook.getSheetAt(index);
row = sheet.getRow(rowNum-1);
if(row==null)
return "";
cell = row.getCell(col_Num);
if(cell==null)
return "";
if(cell.getCellType()==Cell.CELL_TYPE_STRING)
return cell.getStringCellValue();
else if(cell.getCellType()==Cell.CELL_TYPE_NUMERIC ||
cell.getCellType()==Cell.CELL_TYPE_FORMULA ){
if (HSSFDateUtil.isCellDateFormatted(cell)) {
double d = cell.getNumericCellValue();
cal.setTime(HSSFDateUtil.getJavaDate(d));
cellText =
(String.valueOf(cal.get(Calendar.YEAR))).substring(2);
cal.get(Calendar.MONTH)+1 + "/" +
cellText;
}
return cellText;
}else if(cell.getCellType()==Cell.CELL_TYPE_BLANK)
return "";
else
return String.valueOf(cell.getBooleanCellValue());
catch(Exception e){
e.printStackTrace();
return "row "+rowNum+" or column "+colName +" does not exist in xls";
try{
if(rowNum <=0)
return "";
if(index==-1)
return "";
sheet = workbook.getSheetAt(index);
row = sheet.getRow(rowNum-1);
if(row==null)
return "";
cell = row.getCell(colNum);
if(cell==null)
return "";
if(cell.getCellType()==Cell.CELL_TYPE_STRING)
return cell.getStringCellValue();
else if(cell.getCellType()==Cell.CELL_TYPE_NUMERIC ||
cell.getCellType()==Cell.CELL_TYPE_FORMULA ){
if (HSSFDateUtil.isCellDateFormatted(cell)) {
double d = cell.getNumericCellValue();
cal.setTime(HSSFDateUtil.getJavaDate(d));
cellText =
(String.valueOf(cal.get(Calendar.YEAR))).substring(2);
cal.get(Calendar.DAY_OF_MONTH) + "/" +
cellText;
}
return cellText;
}else if(cell.getCellType()==Cell.CELL_TYPE_BLANK)
return "";
else
return String.valueOf(cell.getBooleanCellValue());
catch(Exception e){
e.printStackTrace();
return "row "+rowNum+" or column "+colNum +" does not exist in xls";
try{
if(rowNum<=0)
return false;
int colNum=-1;
if(index==-1)
return false;
sheet = workbook.getSheetAt(index);
row=sheet.getRow(0);
for(int i=0;i<row.getLastCellNum();i++){
//System.out.println(row.getCell(i).getStringCellValue().trim());
if(row.getCell(i).getStringCellValue().trim().equals(colName))
colNum=i;
if(colNum==-1)
return false;
sheet.autoSizeColumn(colNum);
row = sheet.getRow(rowNum-1);
if (row == null)
row = sheet.createRow(rowNum-1);
cell = row.getCell(colNum);
if (cell == null)
cell = row.createCell(colNum);
cell.setCellValue(data);
workbook.write(fileOut);
fileOut.close();
catch(Exception e){
e.printStackTrace();
return false;
return true;
try{
if(rowNum<=0)
return false;
int index = workbook.getSheetIndex(sheetName);
int colNum=-1;
if(index==-1)
return false;
sheet = workbook.getSheetAt(index);
row=sheet.getRow(0);
for(int i=0;i<row.getLastCellNum();i++){
if(row.getCell(i).getStringCellValue().trim().equalsIgnoreCase(colName))
colNum=i;
if(colNum==-1)
return false;
sheet.autoSizeColumn(colNum);
row = sheet.getRow(rowNum-1);
if (row == null)
row = sheet.createRow(rowNum-1);
cell = row.getCell(colNum);
if (cell == null)
cell = row.createCell(colNum);
cell.setCellValue(data);
hlink_font.setUnderline(XSSFFont.U_SINGLE);
hlink_font.setColor(IndexedColors.BLUE.getIndex());
hlink_style.setFont(hlink_font);
//hlink_style.setWrapText(true);
link.setAddress(url);
cell.setHyperlink(link);
cell.setCellStyle(hlink_style);
workbook.write(fileOut);
fileOut.close();
catch(Exception e){
e.printStackTrace();
return false;
return true;
}
// returns true if sheet is created successfully else false
FileOutputStream fileOut;
try {
workbook.createSheet(sheetname);
workbook.write(fileOut);
fileOut.close();
} catch (Exception e) {
e.printStackTrace();
return false;
return true;
// returns true if sheet is removed successfully else false if sheet does not exist
if(index==-1)
return false;
FileOutputStream fileOut;
try {
workbook.removeSheetAt(index);
workbook.write(fileOut);
fileOut.close();
} catch (Exception e) {
e.printStackTrace();
return false;
return true;
try{
if(index==-1)
return false;
style.setFillForegroundColor(HSSFColor.GREY_40_PERCENT.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
sheet=workbook.getSheetAt(index);
row = sheet.getRow(0);
if (row == null)
row = sheet.createRow(0);
if(row.getLastCellNum() == -1)
cell = row.createCell(0);
else
cell = row.createCell(row.getLastCellNum());
cell.setCellValue(colName);
cell.setCellStyle(style);
workbook.write(fileOut);
fileOut.close();
}catch(Exception e){
e.printStackTrace();
return false;
return true;
try{
if(!isSheetExist(sheetName))
return false;
sheet=workbook.getSheet(sheetName);
style.setFillForegroundColor(HSSFColor.GREY_40_PERCENT.index);
style.setFillPattern(HSSFCellStyle.NO_FILL);
for(int i =0;i<getRowCount(sheetName);i++){
row=sheet.getRow(i);
if(row!=null){
cell=row.getCell(colNum);
if(cell!=null){
cell.setCellStyle(style);
row.removeCell(cell);
workbook.write(fileOut);
fileOut.close();
catch(Exception e){
e.printStackTrace();
return false;
return true;
}
if(index==-1){
index=workbook.getSheetIndex(sheetName.toUpperCase());
if(index==-1)
return false;
else
return true;
else
return true;
if(!isSheetExist(sheetName))
return -1;
sheet = workbook.getSheet(sheetName);
row = sheet.getRow(0);
if(row==null)
return -1;
return row.getLastCellNum();
url=url.replace('\\', '/');
if(!isSheetExist(sheetName))
return false;
sheet = workbook.getSheet(sheetName);
for(int i=2;i<=getRowCount(sheetName);i++){
if(getCellData(sheetName, 0, i).equalsIgnoreCase(testCaseName)){
break;
return true;
}
for(int i=2;i<=getRowCount(sheetName);i++){
if(getCellData(sheetName,colName , i).equalsIgnoreCase(cellValue)){
return i;
return -1;
}
@DataProvider(name="dp")
public Object[][] getData(Method m) {
// data[0][0]
table.put(excel.getCellData(sheetName, colNum, 1),
excel.getCellData(sheetName, colNum, rowNum));
data[rowNum - 2][0] = table;
}
return data;
Prime or not
import java.util.Scanner;
Array list
import java.util.*;
Event Reports
package extentlisteners;
import java.util.Arrays;
import java.util.Date;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
import com.aventstack.extentreports.Status;
import com.aventstack.extentreports.markuputils.ExtentColor;
import com.aventstack.extentreports.markuputils.Markup;
import com.aventstack.extentreports.markuputils.MarkupHelper;
testReport.set(test);
String methodName=result.getMethod().getMethodName();
testReport.get().pass(m);
}
public void onTestFailure(ITestResult result) {
String excepionMessage=Arrays.toString(result.getThrowable().getStackTrace());
/* try {
ExtentManager.captureScreenshot();
MediaEntityBuilder.createScreenCaptureFromPath(ExtentManager.screenshotName)
.build());
} catch (IOException e) {
}*/
testReport.get().log(Status.FAIL, m);
}
public void onTestSkipped(ITestResult result) {
String methodName=result.getMethod().getMethodName();
testReport.get().skip(m);
if (extent != null) {
extent.flush();
}
package extentlisteners;
import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
import com.aventstack.extentreports.reporter.configuration.Theme;
htmlReporter.config().setTheme(Theme.STANDARD);
htmlReporter.config().setDocumentTitle(fileName);
htmlReporter.config().setEncoding("utf-8");
htmlReporter.config().setReportName(fileName);
extent.attachReporter(htmlReporter);
extent.setSystemInfo("Organization", "Way2Automation");
try {
} catch (IOException e) {
e.printStackTrace();
}*/
}
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
@DataProvider(name="dp")
public Object[][] getData(Method m) {
// data[0][0]
table.put(excel.getCellData(sheetName, colNum, 1),
excel.getCellData(sheetName, colNum, rowNum));
data[rowNum - 2][0] = table;
}
return data;
String sheetName="test_suite";
int rows = excel.getRowCount(sheetName);
for(int rNum=2; rNum<=rows; rNum++){
if(testCase.equalsIgnoreCase(testName)){
if(runmode.equalsIgnoreCase("Y"))
return true;
else
return false;
}
}
return false;
}