Page Object Model (POM)-2-Read/Write Excel
--
Adım-1:Maven Projesi oluşturma
Adım-2:pom.xml dosyasına projemizde kullanacağımız depenciesleri ekleyelim. Bizim projemizde kullanılacak dependiciesler;
Selenium-java
Webdriver Manager
TestNG
poi
poi-ooxml
Adım-3:test/java altında resources adında paket oluşturalım ve içine örnek bir excel dosyası oluşturalım.Bizim dosyamız ülkeler ve dillerinden oluşmaktadır.
Adım-4:Şimdi resources altındaki excel dosyamızı okuyacak tests altında ReadExcel adından java class oluşturalım.
Adım-5:ReadExcel classımızın içeriğini oluşturalım. Aşağıda kodlar verilmiştir. İstersek projemizi daha dinamik yapmak için configuration.properties dosyasını kullanabiliriz.
public class ReadExcel {
@Test
public void readExcel() throws IOException {
String excelPath=”src/test/java/resources/languages.xlsx”; //yol
FileInputStream fileInputStream=new FileInputStream(excelPath);
Workbook workBook = WorkbookFactory.create(fileInputStream);
Sheet sheet=workBook.getSheet(“Sheet1”);
Row row = sheet.getRow(3);
Cell cell=row.getCell(1); //French
System.out.println(cell);
}
@Test
public void readExcel2() throws IOException {
String excelPath=”src/test/java/resources/languages.xlsx”;
FileInputStream fileInputStream=new FileInputStream(excelPath);
Workbook workbook=WorkbookFactory.create(fileInputStream);
Cell cell=workbook.
getSheet(“Sheet1”).
getRow(3).
getCell(1); //Integer.parseInt(ConfigReader.getProperty(“1”))
System.out.println(cell);
System.out.println(cell.toString());
System.out.println(workbook.getSheet(“Sheet1”).getLastRowNum());
System.out.println(workbook.getSheet(“Sheet1”).getPhysicalNumberOfRows());
// getPhysicalNumberOfRows() give us row number which include data
int lastIndex=workbook.getSheetAt(0).getLastRowNum();
for (int index=1 ; index<=lastIndex ; index++){
String language= workbook.getSheetAt(0).getRow(index).getCell(1).toString();
System.out.println(language);
}
Map<String,String> countryMap = new HashMap<>();
String key=””;
String value=””;
for (int index=1 ; index<=lastIndex ; index++){
key=workbook.getSheetAt(0).getRow(index).getCell(0).toString();
value= workbook.getSheetAt(0).getRow(index).getCell(1).toString();
countryMap.put(key,value);
}
System.out.println(countryMap);
fileInputStream.close();
workbook.close();
}
}
Adım-6:Şimdi de resources altındaki excel dosyamıza veri yazalım. Bunun için tests altında WriteExcel adında java class oluşturalım. Dosyanın içeriği aşağıda verilmiştir.
public class WriteExcel {
@Test
public void writeExcel() throws IOException {
String path=”src/test/java/resources/languages.xlsx”;
FileInputStream fileInputStream = new FileInputStream(path);
Workbook workbook= WorkbookFactory.create(fileInputStream);
workbook.getSheetAt(0).getRow(0).createCell(2).setCellValue(“Continent”);
workbook.getSheetAt(0).getRow(1).createCell(2).setCellValue(“Europa”);
workbook.getSheetAt(0).getRow(2).createCell(2).setCellValue(“Europa”);
workbook.getSheetAt(0).getRow(3).createCell(2).setCellValue(“Europa”);
FileOutputStream fileOutputStream=new FileOutputStream(path);
workbook.write(fileOutputStream);
}
}
Not:Tüm projenin kodlarına https://github.com/zoomokul/POMproject adresinden ulaşabilirsiniz
#Kaynak: https://github.com/bulutluoz