Class Palindrom
{
int r,sum=0,temp;
int n=454;
temp=n;
while(n>0)
{
r=n%10;
sum=(sum*10)+r;
n=n/10;
}
if(temp==sum)
s.o.p("palidrom number);
else
s,op("not palindrom);
Reverse String:
public class ReverseString()
{
public static String reverseString(String str)
{
StringBuilder sb= new StringBuilder(str);
sb.reverse();
return sb.toString();
}}
Anothor Method:
public class ReverseString()
{
Public static void main(String args[])
{
string s="selenium";
int len=s.length();
String rev=" ";
for (int i=len-1;i>0;i__);
{
rev=rev+charAt(i);
}
System.out.priintln(rev);
}}
Remove Duplicate Character from a String
Class RevmoceDupliacates
{
static void removeDuplicates(string str)
{
LinkedHashSet<Character> set=new LinkedHashset<>();
for (int i=0;i<str.length();i++)
set.add(str.charAt(i));
for(Charachet ch:set)
S.o.p(ch);
Public static void main(String srgs[])
{
String str="removeword";
RevmoceDupliacates r= new RevmoceDupliacates();
r.removeSuplicates(str)
}
4.
public class CountOccurences
{
public static void main(String args[])
{
String input = "aaaabbccAAdd";
char search = 'a'; // Character to search is 'a'.
int count=0;
for(int i=0; i<input.length(); i++)
{
if(input.charAt(i) == search)
count++;
}
System.out.println("The Character '"+search+"' appears "+count+" times.");
}
}
5)Write code to write results in excel sheet.
public void Excelwrite()
{
File file=new File("D:\\output.xlsx");
XSSFWorkbook wb =new XSSFWoekbook();
XSSFSheet sh=wb.createSheet();
sh.createRow(0).createCell(0).setCellValue("Name");
sh.getRow(0).createCell(1).setCellValue(69);
try
{
FileOutputStream fos=new FileOutputtream(file);
wb.write(fos);
}
Catch(Exception e)
{
e.printStackTrace();
}
How to read the entire Excel sheet data:
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class ApachePOI {
public static void main(String args[]) throws IOException {
//Create an object of File class to open xlsx file
File file = new File("E:\\TestData\\TestData.xls");
//Create an object of FileInputStream class to read excel file
FileInputStream inputStream = new FileInputStream(file);
//creating workbook instance that refers to .xls file
HSSFWorkbook wb=new HSSFWorkbook(inputStream);
//creating a Sheet object
HSSFSheet sheet=wb.getSheet("STUDENT_DATA");
//get all rows in the sheet
int rowCount=sheet.getLastRowNum()-sheet.getFirstRowNum();
//iterate over all the row to print the data present in each cell.
for(int i=0;i<=rowCount;i++){
//get cell count in a row
int cellcount=sheet.getRow(i).getLastCellNum();
//iterate over each cell to print its value
System.out.println("Row"+ i+" data is :");
for(int j=0;j<cellcount;j++){
System.out.print(sheet.getRow(i).getCell(j).getStringCellValue()
+",");
}
System.out.println();
}
}
}
wite data in excel sheet:
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
public class WriteToExcel {
public static void main(String args[]) throws IOException {
//set the ChromeDriver path
System.setProperty("webdriver.chrome.driver","E:\\Projects\\
chromedriver.exe");
//Create an object of File class to open xls file
File file = new File("E:\\TestData\\TestData.xls");
//Create an object of FileInputStream class to read excel file
FileInputStream inputStream = new FileInputStream(file);
//creating workbook instance that refers to .xls file
HSSFWorkbook wb=new HSSFWorkbook(inputStream);
//creating a Sheet object
HSSFSheet sheet=wb.getSheet("STUDENT_DATA");
//get all rows in the sheet
int rowCount=sheet.getLastRowNum()-sheet.getFirstRowNum();
//Creating an object of ChromeDriver
WebDriver driver = new ChromeDriver();
//Navigate to the URL
driver.get("https://demoqa.com/automation-practice-form");
//Identify the WebElements for the student registration form
WebElement firstName=driver.findElement(By.id("firstName"));
WebElement lastName=driver.findElement(By.id("lastName"));
WebElement email=driver.findElement(By.id("userEmail"));
WebElement genderMale= driver.findElement(By.id("gender-radio-1"));
WebElement mobile=driver.findElement(By.id("userNumber"));
WebElement address=driver.findElement(By.id("currentAddress"));
WebElement submitBtn=driver.findElement(By.id("submit"));
//iterate over all the rows in Excel and put data in the form.
for(int i=1;i<=rowCount;i++) {
//Enter the values read from Excel in
firstname,lastname,mobile,email,address
firstName.sendKeys(sheet.getRow(i).getCell(0).getStringCellValue());
lastName.sendKeys(sheet.getRow(i).getCell(1).getStringCellValue());
email.sendKeys(sheet.getRow(i).getCell(2).getStringCellValue());
mobile.sendKeys(sheet.getRow(i).getCell(4).getStringCellValue());
address.sendKeys(sheet.getRow(i).getCell(5).getStringCellValue());
//Click on the gender radio button using javascript
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", genderMale);
//Click on submit button
submitBtn.click();
//Verify the confirmation message
WebElement confirmationMessage =
driver.findElement(By.xpath("//div[text()='Thanks for submitting the form']"));
//create a new cell in the row at index 6
HSSFCell cell = sheet.getRow(i).createCell(6);
//check if confirmation message is displayed
if (confirmationMessage.isDisplayed()) {
// if the message is displayed , write PASS in the excel sheet
cell.setCellValue("PASS");
} else {
//if the message is not displayed , write FAIL in the excel sheet
cell.setCellValue("FAIL");
}
// Write the data back in the Excel file
FileOutputStream outputStream = new FileOutputStream("E:\\TestData\\
TestData.xls");
wb.write(outputStream);
//close the confirmation popup
WebElement closebtn = driver.findElement(By.id("closeLargeModal"));
closebtn.click();
//wait for page to come back to registration page after close button is
clicked
driver.manage().timeouts().implicitlyWait(2000, TimeUnit.SECONDS);
}
//Close the workbook
wb.close();
//Quit the driver
driver.quit();
}
}
link -https://www.toolsqa.com/selenium-webdriver/excel-in-selenium/
read data from excel:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class BrowserStackReadExcelTest {
public static void main (String [] args) throws IOException{
//Path of the excel file
FileInputStream fs = new FileInputStream("D:\\DemoFile.xlsx");
//Creating a workbook
XSSFWorkbook workbook = new XSSFWorkbook(fs);
XSSFSheet sheet = workbook.getSheetAt(0);
Row row = sheet.getRow(0);
Cell cell = row.getCell(0);
System.out.println(sheet.getRow(0).getCell(0));
Row row1 = sheet.getRow(1);
Cell cell1 = row1.getCell(1);
System.out.println(sheet.getRow(0).getCell(1));
Row row2 = sheet.getRow(1);
Cell cell2 = row2.getCell(1);
System.out.println(sheet.getRow(1).getCell(0));
Row row3 = sheet.getRow(1);
Cell cell3 = row3.getCell(1);
System.out.println(sheet.getRow(1).getCell(1));
//String cellval = cell.getStringCellValue();
//System.out.println(cellval);
}
}
File scr=((Takescreenshot)driver).getScreenshotAs(OUTPUTType.FILE);
FIleUtils.copyGFile(scr.