Installation Environment
My environment is Mac + firefox42 + Selenium 2.9.1
Download link for Firefox history version: http://ftp.mozilla.org/pub/firefox/releases/
This experiment requires downloading a large number of jar packages, as shown in
The students who need to download the link can private me--
Experimental process
- Open Selenium, record a series of actions, my operation is to visit https://psych.liebes.top/st this URL, enter your own account and password, and then click Out of the GitHub link. Export the recorded operation as a Java file.
- Open Eclipse, create a new Java MAVEN project (Mac's new version of Eclipse comes with Maven), and then paste the Java code of the recorded operation in.
- Next we need to read the xlsx file, first we want to introduce some packages
import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.util.ArrayList;import java.util.LinkedHashMap;import java.util.List;import java.util.Map;import java.util.Map.Entry;import java.util.concurrent.TimeUnit;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.DateUtil;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.xssf.usermodel.XSSFWorkbook;
The code to read xlsx is as follows (put the code in the main function):
Workbook WB =null; Sheet Sheet = null; row row = null; list<map<string,string>> list = null; String celldata = null; String FilePath = "/users/izayoi/desktop/input.xlsx"; String columns[] = {"Name", "Git"}; WB = Readexcel (FilePath); if (WB! = NULL) {//used to hold the table data list = new arraylist<map<string,string>> (); Gets the first sheet sheet = wb.getsheetat (0); Gets the maximum number of rows int rownum = Sheet.getphysicalnumberofrows (); Gets the first row, row = Sheet.getrow (0); Gets the maximum number of columns int colnum = Row.getphysicalnumberofcells (); for (int i = 1; i<rownum; i++) {map<string,string> Map = new linkedhashmap<string,string> (); row = Sheet.getrow (i); if (row!=null) {for (int j=0;j<colnum;j++) {celldata = Getstringval (Row.getcell (j )); System.out.println (Celldata.trim ()); Map.put (Columns[j], Celldata.trim ()); }}else{break; } list.add (map); }}//traverse the parsed list for (map<string,string> map:list) {String username = ""; String Giturl = ""; String password = ""; int flag = 0; For (entry<string,string> Entry:map.entrySet ()) {if (flag = = 0) {Usern ame = Entry.getvalue (); flag++; } else {giturl = Entry.getvalue (); System.out.println (Giturl); flag--; }} try {assertequals (Giturl, Driver.findelement (By.xpath ("//p")). GetText ()) ; System.out.println (Giturl); } catch (Error e) {verificationerrors.append (e.tostring ()); } systeM.out.println (); }
The functions that read xlsx are as follows:
public static Workbook Readexcel (String filePath) {Workbook WB = null; if (filepath==null) {return null; } String extstring = filepath.substring (Filepath.lastindexof (".")); InputStream is = null; try {is = new FileInputStream (FilePath); if (". xls". Equals (extstring)) {return WB = new Hssfworkbook (IS); }else if (". xlsx". Equals (extstring)) {return WB = new Xssfworkbook (IS); }else{return WB = NULL; }} catch (FileNotFoundException e) {e.printstacktrace (); } catch (IOException e) {e.printstacktrace (); } return WB; public static object Getcellformatvalue (cell cell) {object cellvalue = null; if (cell!=null) {//Determine cell type switch (Cell.getcelltype ()) {case cell.cell_type_numeric:{ Cellvalue = string.valueof (Cell.getnumericcellvalue ()); Break } CASE cell.cell_type_formula:{//Determine if Cell is a date format if (dateutil.iscelldateformatted (Cell)) { Convert to date format yyyy-mm-dd Cellvalue = Cell.getdatecellvalue (); }else{//Digital Cellvalue = string.valueof (Cell.getnumericcellvalue ()); } break; } case cell.cell_type_string:{Cellvalue = Cell.getrichstringcellvalue (). getString (); Break } Default:cellvalue = ""; }}else{cellvalue = ""; } return cellvalue; }
Because some of the table numbers are represented by scientific notation, the following function is required to convert the numbers of scientific notation into strings
private static String getStringVal(Cell cell) { switch (cell.getCellType()) { case Cell.CELL_TYPE_BOOLEAN: return cell.getBooleanCellValue() ? "true" : "false"; case Cell.CELL_TYPE_FORMULA: return cell.getCellFormula(); case Cell.CELL_TYPE_NUMERIC: cell.setCellType(Cell.CELL_TYPE_STRING); return cell.getStringCellValue(); case Cell.CELL_TYPE_STRING: return cell.getStringCellValue(); default: return ""; } }
There are a lot of pits in this form, such as a student without a GitHub link, where I directly judge him to fail, and some students have a space before the GitHub link, I use trim () to eliminate these spaces
The final run results are as follows:
A GitHub URL is attached:
Https://github.com/IzayoiNamida/tjuscs-_software_testing
Software test experiment in the MAC environment Firefox configuration selenium Java read xlsx file