繁体   English   中英

如何在Java,硒测试中读取Excel数据?

[英]How to read excel data in java, selenium testing?

我在Java中有一个硒网络驱动程序支持的代码,用于测试Web应用程序。 我的代码如下:

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();
}
}

我想修改此代码以读取Microsoft Excel数据并通过此代码进行许多测试。 我的excel文件包含各种测试数据。 提前感谢!

您可以使用此api从Java中读取excel行: http : //jexcelapi.sourceforge.net/该教程很棒,可以在以下位置找到: http : //www.andykhan.com/jexcelapi/tutorial.html

另外,请注意在脚本中将用户名和密码发布到stackoverflow;)

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

Java中有许多API可用于从Excel读取数据。

  1. JExcelApi。
  2. 兴趣点

变通

将Excel另存为CSV文件(以防您不想使用任何外部API),并以文本文件的形式正常读取。 但是,请使用逗号作为分隔符。

检查此链接以获取更多信息: 如何在Java中读取和写入Excel文件

我希望这有帮助 :-)

   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;
    }
}

为了读写Excel,我创建了一个名为POIRE的开源库,其中包含许多用户友好的方法。

https://github.com/ssirekumar/POIRE

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

警察

它使用POI类通过Java完成Microsoft Ms-office(Excel,Ms Word,PowerPoint)文件处理。 此API是在POI库的顶部开发的。 它具有多种用于处理Ms-office文件的方法(Excel,Ms Word,PowerPoint)。 借助POI中的NPOIFSFileSystem和OPCPackage类,这些方法将对二进制文件(.xls,.doc,.ppt)和XML(.xlsx,.docx,.pptx)均适用。 使用这些类,可以更快地读取和写入文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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