Robotium : I want to Do data driven Testing using robotium? - java

I am searching a way I want to test login and it is not possible to write all the function with hard code values So like we have Data driven(parametrization) in QTP etc the tool fetch data from a file and keep entering and at last all our inputs are executed ..
Is it possible to do via Roboitum ?
Please let me know for the same.

yes u can make a data excel file and u can fetch data from excel file
u can use
File storage = Environment.getExternalStorageDirectory();
String filename = "test.xls";
File myfile = new File(storage, filename);
FileInputStream fis = new FileInputStream(myfile);
Workbook ws = null;
ws = Workbook.getWorkbook(fis);
Sheet Wsheet = ws.getSheet(SheetNum);
String returnValue ;
returnValue = Wsheet.getCell(ColmnNum, RowNum).getContents();
same as how we use Excel reading in QTP , It works for me

Related

Text Extraction from xlsx file having multiple sheets based on given sheet name using apache poi

I have an xlsx file which contains around 150 sheets in it. I need to extract text for only 30 of those sheets, I have tried the below code but this extracts the text for all of the sheets.
try (InputStream inp = new FileInputStream(filePath)) {
OPCPackage d=OPCPackage.open(inp);
XSSFWorkbook wb = new XSSFWorkbook(d);
XSSFExcelExtractor extractor = new XSSFExcelExtractor(wb);
extractor.setFormulasNotResults(true);
extractor.setIncludeSheetNames(false);
String text = extractor.getText().replaceAll("\\t"," ").replaceAll("%","");
lines =text.split("\n");
Could someone please help me if there is any method available using which I can extract text by giving the sheet names for which I want to extract data.
Sure
Class Workbook has method getSheet(String name) that returns Sheet instance.
I don't remember correct class name but you can write something like this
List<String> sheetNames = List.of("sheet1", "sheet2", .... );
List<Sheet> sheets = new ArrayList<>();
sheetNames.forEach(nm -> sheets.add(workbook.getSheet(nm)));
then you may want to filter out nulls (when sheet wasn't found)
sheets = sheets.stream().filter(s -> Objects.notNull(s)).collect(Collectors.toList())
here you go

After extracting .xls ole - file is empty with Microsoft Excel but readable with apache-poi

I have a .doc file with an embedded .xls and an embedded .doc.
I can extract both files and save it.
When I want to open the .doc - document everything is fine.
When I want to open the .xls - document it is empty, the editor opens nothing, I also dont see any empty cells nothing.
So I tried to read again with apache-poi the extracted .xls document and when I look at the Sheet-Name or Content of the cells - everything is there.
Do you have any ideas what it is?
My setup is:
apache-poi version 3.15 (I also tried some minor versions)
The word and excel files were created with office 2007.
the code - part:
POIFSFileSystem fs = new POIFSFileSystem(file);
POIOLE2TextExtractor poiole2TextExtractor = ExtractorFactory.createExtractor(fs);
POITextExtractor[] embeddedExtractors = ExtractorFactory.getEmbededDocsTextExtractors(poiole2TextExtractor);
for (POITextExtractor textExtractor : embeddedExtractors) {
// If the embedded object was an Excel spreadsheet.
if (textExtractor instanceof ExcelExtractor) {
ExcelExtractor excelExtractor = (ExcelExtractor) textExtractor;
DirectoryNode directoryNode = (DirectoryNode) excelExtractor.getRoot();
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(directoryNode, true);
File tmp = new File(targetfolder, "test.xls");
FileOutputStream fileOutputStream = new FileOutputStream(tmp);
hssfWorkbook.write(fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
hssfWorkbook.close();
}
Thank you :)
So somehow i found the problem:
For HSSFWorkbook I needed to set the following attribute:
hssfWorkbook.setHidden(false);
For all formats xlsx (2007) if you call that method, you will get an NotImplementedException - so you have to fix that manually... I found the solution as follows:
String workbookContent = new String(ZipFileUtils.getInnerFile(tmp, "xl/workbook.xml"), "UTF-8");
workbookContent = workbookContent.replaceFirst("visibility=\"hidden\"", "");
ZipFileUtils.replaceZippedFile(tmp, "xl/workbook.xml",
workbookContent.getBytes( "UTF-8"), new FileOutputStream(tmp2));
where tmp = My Extracted xlsx File and I save it to an new one at the momement tmp2

Edit an excel file on the server side in a client server architecture

I have an excel book which is shared across all the business users. As part of the process, the excel needs to be updated at the server side. All users have mapped to a network drive which contains the excel book. Earlier we used to update the excel on the client side using activeX controls. Now the problem is that the path for the excel book is not accessible on the server machine and hence it's throwing file not found exception. Is there any way to handle this apart from mapping the network drive on the server machine as well?
Following is the code:
public static String updateExcelFile(String fileLocation, String dataVal,String txnId) throws IOException{
String status="";
//fileLocation = "Z:\\check.xlsx";
//FileInputStream file = new FileInputStream(new File("C:\\JXL\\Checklist OnBoarding1.1.xlsx"));
//fileLocation.rep
FileInputStream file = new FileInputStream(new File(fileLocation));
XSSFWorkbook wb = new XSSFWorkbook (file);
XSSFSheet sheet = wb.getSheetAt(0);
Row r = null;
Cell c = null;
String data[] = dataVal.split(",");
int rowCount = sheet.getPhysicalNumberOfRows();
int rowNum = rowCount;
Iterator<Row> rowInterator =sheet.iterator();
while(rowInterator.hasNext()){
r=rowInterator.next();
Iterator<Cell> cellIterator = r.cellIterator();
while(cellIterator.hasNext()){
c=cellIterator.next();
//System.out.println();
switch(c.getCellType()) {
case Cell.CELL_TYPE_NUMERIC:
System.out.print(c.getNumericCellValue() + "\t\t");
if(c.getNumericCellValue()==Integer.parseInt(txnId)){
int modRow = r.getRowNum();
System.out.println("ModRow"+modRow);
rowNum=modRow;
}
break;
case Cell.CELL_TYPE_STRING:
System.out.print(c.getStringCellValue() + "\t\t");
if(c.getStringCellValue().equals(txnId)){
int modRow = r.getRowNum();
System.out.println("ModRow"+modRow);
rowNum=modRow;
}
break;
}
}
}
if(rowNum==rowCount){
r=sheet.createRow(rowNum+1);
r=sheet.getRow(rowNum+1);
}else{
r=sheet.getRow(rowNum);
}
for(int cellNum =0; cellNum<data.length;cellNum++){
c=r.createCell(cellNum);
c.setCellValue(data[cellNum]);
}
file.close();
//FileOutputStream outFile =new FileOutputStream(new File("C:\\JXL\\Checklist OnBoarding1.1.xlsx"));
FileOutputStream outFile =new FileOutputStream(new File(fileLocation));
wb.write(outFile);
outFile.close();
status="success";
return status;
}
send the excel file to the server when the user clicks the button:
use ajax to upload the file to a servlet,
have the servlet process the file and then respond to the client with a link to the updated file (which sits in a temp folder on the server), which the ajax can then present to the user.
The user can now download the excel and put it onto the network drive, where he and other users can edit it directly.
I guess this approach would require the user to at least accept the save operation.
Also this approach does not protect you from concurrent updates on the Excel: several users can have the server edit a copy of the Excel, and several other users could directly edit the excel on the network drive.
And having the server edit the Excel might be a problem if the Excel in question is large.
Well, I think I answered the question :-).

Save embedded files from .xls document (Apache POI)

I would like to save all attached files from an Excel (xls/HSSF) without extension.
I've been trying for a long time now, and I really don't know if this is even possible. I also tried Apache Tika, but I don't want to use Tika for this, because I need POI for other tasks, anyway.
I tried the sample code from the Busy Developers Guide, but this does not extract files in the old office format (doc, ppt, xls). And it throws an Error when trying to create new SlideShow(new HSLFSlideShow(dn, fs)) Error: (Remove argument to match HSLFSlideShow(dn))
My actual code is:
public static void saveEmbeddedXLS(InputStream fis_param, String embDIR) throws IOException, InvalidFormatException{
//HSSF - XLS
int i = 0;
System.out.println("Starting Embedded Search in xls...");
POIFSFileSystem fs = new POIFSFileSystem(fis_param);//create FileSystem using fileInputStream
HSSFWorkbook workbook = new HSSFWorkbook(fs);
for (HSSFObjectData obj : workbook.getAllEmbeddedObjects()) {
System.out.println("Objects : "+ obj.getOLE2ClassName());//the OLE2 Class Name of the object
String oleName = obj.getOLE2ClassName();//Document Type
DirectoryNode dn = (DirectoryNode) obj.getDirectory();//get Directory Node
//Trying to create an input Stream with the embedded document, argument of createDocumentInputStream should be: String; Where/How can I get this correct parameter for the function?
InputStream is = dn.createDocumentInputStream(dn);//This line is incorrect! How can I do i correctly?
FileOutputStream fos = new FileOutputStream("embDIR" + i);//Outputfilepath + Number
IOUtils.copy(is, fos);//FileInputStream > FileOutput Stream (save File without extension)
i++;
}
}
So my simple question is:
Is it possible to save ALL attachments from an xls file without any extension (as simple as possible)? And can any one provide me a solution? Many Thanks!

How to store and retrieve a pdf file in couchdb using java

How to store and retrieve a pdf file in couchdb using java.I have tried many links but they all only focus on storing and retrieving images but i am concerned about pdf files.
Here is my code:
// Create a BaseDocument
BaseDocument doc = new BaseDocument();
doc.setProperty("site", "www.onabai.com");
doc.setProperty("leitmotif", "Working on a cloud of documents!");
db.createDocument(doc);
System.out.printf("Id: %s rev.: %s\n", doc.getId(), doc.getRevision());
// Add attachment
File file = new File("resources/jcouchdb.svg");
InputStream is = new FileInputStream(file);
String rev = db.createAttachment(doc.getId(), doc.getRevision(),
"logo", "image/svg+xml", is, file.length());
System.out.printf("New rev.: %s\n", rev);
I get an error saying method createAttachment doesn't exist.

Categories