Java uses POI to manipulate Excel and test framework to build and test the development of some ideas

Source: Internet
Author: User

Whether it's UI Automation testing or interface Automation testing that requires data-driven, one common way is to use Excel to manage the data, then there's some code for Excel, which we've previously introduced to excel with CSV, but it's not powerful enough. For example, when the interface Automation test framework is built, we use Excel to do data-driven, use Excel to manage the use case and test the results of statistics, then we need to read and write to Excel, if you do more comprehensive, but also to test the results of a statistical.

Let's talk about how to use Excel to drive data. In our company's interface Automation testing Framework for example, we use Excel to design and output comparison, input table is the request input data, ouput table is expected output data, the result table is the actual output results, can be processed to these use cases more intuitive to see the test results.

Here also want to talk about the construction of the interface test framework, has been in the forum to see some of the Great God's framework design, from the technical aspects of the technology involved are very similar, basically with some of the relevant language testing framework plus some data analysis, assertions, data requests. I think the interface test framework, or UI Automation testing framework is based on the requirements, from the project itself to consider how to build, not from a technical point of view, can not say that I do automated testing interface testing will use these techniques to build a framework, to see the characteristics of the project itself, See how much the overall project development test specification needs to be done, and then consider whether you need to build a framework. Because the interface test itself is used by many tools to meet the test needs, if the project itself does not involve some special requirements, such as:

1. Whether special encryption is considered

2. Whether to consider the interface duplication of data cleansing and other issues (tools will need manual processing of test data)

3. Whether to consider test batch execution, test report generation and mail sending issues

4. Whether to consider issues such as continuous integration

I think that when considering these problems, we need to consider the framework for testing, instead of having the interface test task to find out the various techniques to build the framework. I have this feeling of the reason is also in learning a variety of test technology, interface Ah, performance Ah, automation ah and so on, found in the actual project is not necessarily used, then we learn the meaning of these technologies? There are reasons for this in order to get a job raise. But I think it should be back to the test engineer this position, our task is what, I more and more from the mind to jump out of the test of the prison, I hope I will learn everything is based on the quality of software quality control, rather than a variety of technology to stack, In the end found to master a variety of technology but do not know how to test, how to be a qualified test, a qualified QA. Although I will always strengthen the study of technology, to the development of testing development, but from the depths of the mind or firm his position, whether it is testing, QA, test development, all the work should be to ensure the quality of the system software, or can be in the quality control of the work to provide appropriate technical support, to provide testing efficiency, Auxiliary development to improve the efficiency and quality of development, I think this is the test should do the work, rather than blindly learning technology to develop closer to improve their competitiveness in the market.

Okay, crap. Not much to say, simply poi create, read, modify write under the small demo record:

Package Com.javapoi;import Java.io.file;import Java.io.fileinputstream;import java.io.filenotfoundexception;import Java.io.fileoutputstream;import Java.io.ioexception;import Java.io.inputstream;import Java.io.OutputStream;import Java.util.hashmap;import Java.util.map;import Org.apache.poi.hssf.usermodel.hssfworkbook;import Org.apache.poi.ss.usermodel.cell;import Org.apache.poi.ss.usermodel.row;import Org.apache.poi.ss.usermodel.Sheet ; Import Org.apache.poi.ss.usermodel.workbook;import Org.apache.poi.ss.usermodel.workbookfactory;public class poitest {public static void main (string[] args) {//TODO auto-generated method Stubpoitest.createexcel ();// Poitest.readexcel (); Poitest.writetoexcel (); map<string, string> student = new hashmap<string, string> (); Student.put ("Name", "Xiaoming"); Student.put ("Gender", "male") Student.put ("Address", "Earth"), Student.put ("Score", "good"); String FilePath = "/users/macbookpro/desktop/poitest/workbook.xls"; Poitest.writetoexcel (FilePath, student, "new sheet"); public static VOID Createexcel () {//create worksheet Workbook Wbook = new Hssfworkbook ();//Create Sheet page Sheet sh = wbook.createsheet ("New Sheet");    Row row cell grid row Row1 = Sh.createrow (0);    Create cell cell cell = Row1.createcell (0);        Set the grid display Cell.setcellvalue ("name");    Row1.createcell (1). Setcellvalue ("gender");        Row1.createcell (2). Setcellvalue ("Address");    Create file FileOutputStream fos = null;     try {fos = new FileOutputStream ("/users/macbookpro/desktop/poitest/workbook.xls"); Wbook.write (FOS);} catch (FileNotFoundException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} Finally{try {fos.close ();} catch (IOException e) {e.printstacktrace ();}}} public static void Readexcel () {inputstream ips = null;try {//Get file input stream ips = new FileInputStream ("/users/macbookpro/desktop /poitest/workbook.xls ");//Create Workbook object based on input stream Workbook Wbook = workbookfactory.create (IPs);//Get First sheet page sheet SH1 = Wbook.getsheetat (0);//Traverse row for (row row:sh1) {for (cell cell:row) {System.out.print (cell. toString () + "");}}} catch (Exception e) {e.printstacktrace ();} finally{try {Ips.close ();} catch (IOException e) {e.printstacktrace ()}}} public static void Writetoexcel () {outputstream ops = null;inputstream ips = null;try {ips = new FileInputStream ("/users/ma Cbookpro/desktop/poitest/workbook.xls ");//Create Workbook object based on output stream workbook Wbook = workbookfactory.create (IPs);//  Get Sheet page Sheet sh = wbook.getsheetat (0); for (Row Row:sh) {for (Cell cell:row) {String str = cell.tostring ();//change name to name if (Str.equals ("name")) {cell.setcellvalue ("name");}} Add a field Row.createcell (3). Setcellvalue ("Score");} OPS = new FileOutputStream ("/users/macbookpro/desktop/poitest/workbook.xls"); Wbook.write (OPS);}   catch (Exception e) {e.printstacktrace ();} finally{try{ops.close (); Ips.close ();} catch (Exception e) {e.printstacktrace ();}}} public static void Writetoexcel (String filepath,map<string, string>student,string sheetname) {OutputStream ops =            Null;inputstream ips = null; try {ips = new FileInputStream (New FIle (FilePath)); Workbook Wbook = workbookfactory.create (IPS); Sheet sh = wbook.getsheet (sheetname);//Get Sheet line number plus 1int Shrowcount = Sh.getlastrownum () +1; System.out.println (Shrowcount);//Gets the number of table header columns int shcolumn = sh.getrow (0). Getlastcellnum (); System.out.println (Shcolumn); Row Titlerow = sh.getrow (0);//new row row NewRow = Sh.createrow (Shrowcount); for (int i = 0; i < Shcolumn; i++) {//Get column name Strin G Columnstr = Titlerow.getcell (i). toString (); System.out.println (COLUMNSTR); Newrow.createcell (i). Setcellvalue (Student.get (COLUMNSTR));} OPS = new FileOutputStream (new File (FilePath)); Wbook.write (OPS);} catch (Exception e) {e.printstacktrace ();} finally{try{ops.close (); Ips.close ();} catch (Exception e) {e.printstacktrace ();}}}}

View results:

To build the framework, it is necessary to encapsulate the relevant Excel operations, this two days with our automated test framework for example to share how to build.

Java uses POI to manipulate Excel and test framework to build and test the development of some ideas

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.