This article is intended to help readers introduce how to use excle to achieve data-driven
This article is the continuation of the above https://www.cnblogs.com/xuezhezlr/p/9096063.html, if you do not see the text suggested yourself to look at, to understand this article has a great help
The program above is a more primitive data-driven, although the implementation of data-driven, but in essence, the data written in code to read, this approach is only to achieve data driven to do data-driven, meaning is not big and improve the level of code requirements, improve the complexity of code, but rather than the original code logic strong, This article is to undertake the above to introduce
public static Collection Zlrshiyan () {
Return arrays.aslist (New object[][]{{2,6},{3,5}});
}
This is part of the code above the source of the fragment of the data, we can not help but think that if the data from the Excle or database data storage, and then processed into the original string 2-dimensional data, you can use this method, so that our data source is excle, and the code is in another place, run time to read it
Below is the Read method of the Excle
public static string[][] Getxlsdata (String file, int ignorerows,string a)
Throws FileNotFoundException, ioexception{
File File1 = new file (file);
Return Getxlsdata (File1,ignorerows,a);
}
public static string[][] Getxlsdata (file file, int ignorerows,string a)
Throws FileNotFoundException, IOException {
list<string[]> result = new arraylist<string[]> ();
int rowsize = 0;
Bufferedinputstream in = new Bufferedinputstream (New FileInputStream (
file));
Open Hssfworkbook
A = A.replaceall ("/", "_");//Replace A for XLS acceptable type
A=a.substring (1,a.length ()). Trim ();//Get Rid of the first _
Poifsfilesystem fs = new Poifsfilesystem (in);
Hssfworkbook wb = new Hssfworkbook (FS);
Hssfcell cell = null;
int sheetindex;
for (sheetindex = 0; Sheetindex < wb.getnumberofsheets (); sheetindex++) {
if (Wb.getsheetname (Sheetindex). Equals (a))
{
Break
}
}
Hssfsheet st = Wb.getsheetat (Sheetindex);
First act title, not take
for (int rowIndex = ignorerows; RowIndex <= st.getlastrownum (); rowindex++) {
Hssfrow row = St.getrow (RowIndex);
if (row = = null) {
Continue
}
int temprowsize = Row.getlastcellnum ();
if (Temprowsize > Rowsize) {
Rowsize = temprowsize;
}
String[] values = new String[rowsize];
Arrays.fill (Values, "");
Boolean hasValue = false;
for (short columnindex = 0; columnindex <= row.getlastcellnum ()-1; columnindex++) {
String value = "";
Cell = Row.getcell (columnindex);
if (cell! = null) {
Note: Must be set to this, or it may appear garbled
Cell.setencoding (HSSFCELL.ENCODING_UTF_16);
Switch (Cell.getcelltype ()) {
Case hssfcell.cell_type_string:
Value = Cell.getstringcellvalue ();
Break
Case Hssfcell.cell_type_numeric:
if (hssfdateutil.iscelldateformatted (cell)) {
Date date = Cell.getdatecellvalue ();
if (date! = null) {
Value = new SimpleDateFormat ("Yyyy-mm-dd")
. Format (date);
} else {
Value = "";
}
} else {
Value = new DecimalFormat ("0"). Format (cell
. Getnumericcellvalue ());
}
Break
Case Hssfcell.cell_type_formula:
No value when data generated for a formula is imported
if (!cell.getstringcellvalue (). Equals ("")) {
Value = Cell.getstringcellvalue ();
} else {
Value = Cell.getnumericcellvalue () + "";
}
Break
Case Hssfcell.cell_type_blank:
Break
Case HSSFCELL.CELL_TYPE_ERROR:
Value = "";
Break
Case Hssfcell.cell_type_boolean:
Value = (Cell.getbooleancellvalue () = = true? Y
: "N");
Break
Default
Value = "";
}
}
if (ColumnIndex = = 0 && value.trim (). Equals ("")) {
Break
}
Values[columnindex] = Righttrim (value);
HasValue = true;
}
if (HasValue) {
Result.add (values);
}
}
In.close ();
string[][] ReturnArray = new String[result.size ()][rowsize];
for (int i = 0; i < returnarray.length; i++) {
Returnarray[i] = (string[]) result.get (i);
}
for (int i=1;i<returnarray.length;i++) {
RETURNARRAY[I][2] = returnarray[0][2] + "=" + returnarray[i][2];
for (int j = 3; J < Returnarray[0].length; J + +) {
RETURNARRAY[I][2] = returnarray[i][2] + "&" + returnarray[0][j] + "=" + Urlencoder.encode (Returnarray[i][j], "Utf-8") ;
}
}
string[][] ReturnArray2 = new String[result.size () -1][3];
for (int i=0;i<returnarray.length-1;i++)
for (int j = 0; J <= 2; j + +) {
RETURNARRAY2[I][J]=RETURNARRAY[I+1][J];
}
return returnArray2;
}
/**
* Remove the space to the right of the string
* @param str to process the string
* @return The processed string
*/
public static string Righttrim (String str) {
if (str = = null) {
Return "";
}
int length = Str.length ();
for (int i = length-1; I >= 0; i--) {
if (Str.charat (i)! = 0x20) {
Break
}
length--;
}
Return str.substring (0, length);
}
The main writing in the above is the public static string[][] Getxlsdata (String file, int ignorerows,string a) This method, in which file is the path to write Excle, Ignorerows refers to the excle in the top of the number of lines, the code will directly ignore not read, and A is a string, refers to the name of the sheet page specified in the excle, so that the code will read only the sheet page data, it is gratifying that the code will convert the data into a two-dimensional array , that's the data structure we need in the original code.
is a picture of excle in the article
such as the excle, in the code will become a 3*5 2-bit array form, and then the same as the original initialization, you can complete the initialization
private int A;
Private int B;
Private int C;
Private int D;
@Parameterized. Parameters
@SuppressWarnings ("unchecked")
public static Collection Zlrshiyan () throws IOException {
File File = new file ("/users/zlr/desktop/testauto_integration/exceldemo.xls");
Object[][] Object = getxlsdata (file, 0, "/a");
return Arrays.aslist (object);
//Return Arrays.aslist (New object[][]{{2,6},{3,5}});
}
Public Zlrshiyan (String b,string d,string e) {
System.out.print (b+d+e);
this.b = Integer.parseint (b);
this.d= Integer.parseint (d);
System.out.print (b+d);
}
@Before
public void Testinit () throws exception{
A=1;
c=3;
}
@Test
public void Test1 () throws exception{
Assert.assertequals (a+b+c, d);
}
}
In the code above, object is the two-dimensional array of 3*5 that the file is converted to by a sheet page named a, and then the same effect as before
This kind of code can be reused, how to reuse it? To tell the truth, a little low, then I came to the big company, Learning Spring, learning mybits, learned to read from the database code rather than this, from the Excle read code, but the old, the idea is very similar, In this article, I will introduce the results of my work in the company and the automation idea.
As can be seen in the code above, the data and code execution logic has been separated, then in fact, if some code processing logic is different and the data number and parameter name are very similar, different only the parameter value, then you can use a excle sheet page to store the test data, And the code of the processing logic is unchanged ~ ~ This makes me think of the interface test, the same interface, often parameter design and some calls even return the assertion logic is constant, and the change is often passed in the parameters or interface start environment (online, test, even pre-hair environment), these sometimes really, More suitable and I actually verify, you can use the above code to achieve, but also the data carrier for the database, etc., but the idea is exactly the same, a sheet page is actually a table in the database Ah, very similar
On the other hand, we will think of a problem, that is how to do multiple interfaces. Due to the different parameters of each interface, and the code call method of each interface, the check logic is not the same, so the test between different interfaces is often not the same, resulting in a class file interface, and the data source, whether it is excle or database, the design of the program is the simpler the better, So often also, is a library a sheet page an interface, like the same
Let's talk about this set of frameworks.
I build automation tools in multiple companies, the framework is basically the interface Automation framework I used the mainstream framework, I said that the interface automation, not the kind of semi-automation people do, but fully automated, of course, can be based on this framework to develop a variety of front-end code, Allows testers to use non-SQL statements, such as page clicks, and so on to modify the values in the document and sheet pages, and so on, but the essence of those ideas have not changed, are in the framework of the expansion
In general, this data-driven automation framework, fundamentally solves the problem, how to separate the code and test data, so that testers or other personnel, on the basis of the interface logic is not changed, only through a number of methods, modify the database or document can be implemented in the test mode of control, This is really very good code thinking, and, fundamentally also convenient for people who do not understand the code to automate, directly reduce the cost of automation, but, in my view, in fact, often the company's automation is by a few colleagues, and other people do not understand the code at all,,, And often do not understand the code of the tester is not so strong ambition to understand the code, so I am now on the so-called let the people who do not code to do the original intention to do this is to express doubt, really
If the original intention instead, let the test developers, write automation is often because of the replacement of the version and do not need to modify, at this time, the developer of automation code, liquidity is very strong, then our code, readability really, also good actually, the handover is very easy, even if there is no handover, the newcomers to understand is often easy
The above is a talk, in short, to write an interface Automation background code, this set of code, in the actual operation has encountered a variety of problems ah,,, the next article will be explained
Java Automation-data-driven JUnit demo, next