MarkusBernhardt / robotframework-selenium2library-java

Java port of the Selenium 2 (WebDriver) Python library for Robot Framework
Apache License 2.0
46 stars 51 forks source link

Data Driven testing using .xlsx or .csv #69

Closed sunil31925 closed 9 years ago

sunil31925 commented 9 years ago

I am using your robotframework-selenium2library-java writing my test case, I am unable to create Data driven test case. kinldy let me know how to create data driven test case using this project(MarkusBernhardt/robotframework-selenium2library-java ).

kinldy provide sample which Library file need to import in my test case. If possible kindly provide one example.

Any help is going to be appreciated.

asitishere commented 9 years ago

Hi Sunil,

I not yet tried the scenario but soon I have to cove it in my Test and I will share the code.

Solution Options 1- Built in Library and use for CSV "csvLibrary" 2- Use Apache POI or to drive your excel ( By creating you own Keyword and pass the variable , but you have to make the call static so that you can retain the count in robot and Java)

Hopefully today I will try and share the code, you also let know on any success. No worry It will be done need to fit to the Framework

Regards Asit

asitishere commented 9 years ago

Hi Sunil,

I tried multiple options but got success in 1 and other one is under investigation

1: you can use default written CSV lib for Robot in http://jspringbot.org/ 2: I have created a keyword to create file and read file in Java

Example for Create File

@ArgumentNames({ "fileName" }) @RobotKeyword public void WriteExcelFile(String fileName) throws IOException, InterruptedException{

    XSSFWorkbook workbook = new XSSFWorkbook();
    XSSFSheet spreadsheet = workbook.createSheet(fileName);

    XSSFRow row;
    List<String> columnName = new ArrayList<>();
    columnName.add("User");
    columnName.add("Contract");
    columnName.add("SectionHeading");
    columnName.add("Field");
    columnName.add("Value");

    int rowId = 0;

    row = spreadsheet.createRow(rowId++);
    int startCell = 0;
    for (int columnCount = 0; columnCount < columnName.size(); columnCount++) {

        Cell cellHeader = row.createCell(startCell++);
        cellHeader.setCellValue(columnName.get(columnCount));
    }

    FileOutputStream out = new FileOutputStream(new File(
            fileName+".xlsx"));
    workbook.write(out);
    out.close();
    System.out.println("Writesheet.xlsx written successfully");

}

You need Apache POI so add that in your POM file in dependency

org.apache.poi poi 3.11
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>3.11</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml-schemas</artifactId>
        <version>3.11</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-scratchpad</artifactId>
        <version>3.11</version>
    </dependency>

Then copy the above code inside any existing file inside "keywords" folder or create a new file and add the code.

Now you able to use "Write Excel File" as keyword in your Robot Test

The same way you can create a keyword for Read and as per the XLSX or CSV designed by you.

For Read the Return can be a List in Java and in Robot you can add Collection library to manipulate a list. You can read the column as List and work on that over a for loop.

I will make the keyword for read Monday and share with you if you are interested.

Regards Asit

sunil31925 commented 9 years ago

I am only looking for ReadExcel, becuase for my all testcase is very huge and need to feed lots of data for that I put all my test data into the Excel. Ofcource I am intereseted for the ReadExcel. Please share the code for the ReadExcel if possible

Thanks & Regards, Sunil

asitishere commented 9 years ago

You can change the POI to Read the excel and pass Column as a list to robot test cases .

Copy your template of the excel you want to read or if you have any specific need. It can be written in Java and pass to Robot as Keyword.

sunil31925 commented 9 years ago

thank you, I have create the readExcel keyword and work for me. thanks for the help