简体   繁体   中英

How to read excel data in java, selenium testing?

I have a selenium webdriver backed code in java for testing an web application. My code is given below:

package testcases;

import com.thoughtworks.selenium.Selenium;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverBackedSelenium;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
import java.util.regex.Pattern;

public class Untitled {
private Selenium selenium;

@Before
public void setUp() throws Exception {
        WebDriver driver = new FirefoxDriver();
        String baseUrl = "http://www.himalayanpalmistry.com/";
        selenium = new WebDriverBackedSelenium(driver, baseUrl);
}

@Test
public void testUntitled() throws Exception {
        selenium.open("/mabiz/");
        selenium.select("id=register_type", "label=für Unternehmen");
        selenium.click("id=rgst");
        selenium.waitForPageToLoad("30000");
        selenium.type("id=username", "javatesting1");
        selenium.type("id=password", "12345678");
        selenium.type("id=confirm_password", "12345678");
        selenium.type("id=name", "java testing");
        selenium.type("id=position", "java testing");
        selenium.type("id=company", "java testing");
        selenium.type("id=address", "java testing");
        selenium.type("id=zipcode", "12345");
        selenium.type("id=city", "safdj");
        selenium.type("id=phone", "kfajs");
        selenium.type("id=email", "tsandesh23@hotmail.com");
        selenium.click("id=show_contact_info");
        selenium.click("id=product_select3");
        selenium.click("id=term_condition1");
        selenium.click("name=submit_one");
        selenium.waitForPageToLoad("30000");
}

@After
public void tearDown() throws Exception {
        selenium.stop();
}
}

I want to modify this code for reading microsoft excel data and do many tests by this code. My excel file contains various test data. Thanx in advance!!

You can use this api to read excel rows from java: http://jexcelapi.sourceforge.net/ The tutorial is great and can be found here: http://www.andykhan.com/jexcelapi/tutorial.html

Also, be careful about posting usernames and passwords in your scripts to stackoverflow ;)

您可以使用Apache POI访问Excel http://poi.apache.org/spreadsheet/index.html

There are many API's in java available to read data from Excel.

  1. JExcelApi.
  2. POI

Work Around

Save Excel as CSV files (in case you don't want to use any external API)and read them normally as a text file. But use Comma as Separator.

Check this link for more information: How to read and write excel file in java

I hope this helps :-)

   1.Read excel example


    InputStream stream = new FileInputStream(projectFile.getPath());
    ExcelWorkbookContainer container = new ExcelWorkbookContainer(stream);
    Iterator<ExcelRecord> recordIterator = container.getRecordIterator(0);
    //NO TITLE
    recordIterator.next();

    while(recordIterator.hasNext()) {
        ExcelRecord record = recordIterator.next();
         clusterObj.setBrand(record.getValue(ClusterColum.BRAND.getColumIndex()));
    }
    stream.close();

///// poi example

public class ExcelWorkbookContainer {

    private String filename;
    private XSSFWorkbook workbook;

    /**
     * @param filename
     * @throws FileNotFoundException, IOException
     */
    public ExcelWorkbookContainer(String filename)
            throws FileNotFoundException, IOException {
        this(new FileInputStream(filename));
        this.filename = filename;
    }

    /**
     * @param inputStream
     * @throws FileNotFoundException, IOException
     */
    public ExcelWorkbookContainer(InputStream inputStream)
            throws IOException {
        if (inputStream == null) {
            throw new NullPointerException("Input Stream is null");
        }

        workbook = new XSSFWorkbook(inputStream);
    }

    /**
     * Create a composite iterator on the excel records
     * 
     * @param sheetId the id of the sheet for data extraction
     * @return composite iterator on the excel records
     */
    public Iterator<ExcelRecord> getRecordIterator(int sheetId) {
        return new ExcelRecordIterator(workbook.getSheetAt(sheetId));
    }

    /**
     * Create a composite iterator on the excel records
     * 
     * @param sheetId the id of the sheet for data extraction
     * @return composite iterator on the excel records
     */
    public Iterator<ExcelRecord> getRecordIterator(int sheetId, String[] headers) {
        return new ExcelRecordIterator(workbook.getSheetAt(sheetId), headers);
    }

    public String getFilename() {
        return filename;
    }

    public XSSFWorkbook getWorkbook() {
        return workbook;
    }
}

For reading and writing Excel I created an open source library called POIRE which includes many user-friendly methods.

https://github.com/ssirekumar/POIRE

https://github.com/ssirekumar/POIRE/releases

POIRE

It does Microsoft Ms-office(Excel,Ms Word,PowerPoint) file handling with Java using the POI classes. This API was developed on the top of the POI library. It has many methods for working with the Ms-office files(Excel,Ms Word,PowerPoint). These methods will work on the both the binary(.xls, .doc, .ppt) and XML(.xlsx, .docx, .pptx) with the help of the NPOIFSFileSystem & OPCPackage classes in the POI. With these classes the reading and writing into a file would be faster.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM