Selenium--selenium using Excel for Parameterization

Source: Internet
Author: User
Tags testng

Description: I read the Excel file by workbook way, this time with the login interface as an example
Note: Use workbook to read Excel files, as long as Excel requires 2003 version, other versions temporarily do not support
Specific steps:
First step: Create a new Excel file and enter the data content
Step Two: Create a new Java class in eclipse and write the code to get the Excel file

ImportJava.io.File;Importjava.io.IOException;Importjava.util.ArrayList;Importjava.util.List;ImportJXL. Sheet;ImportJXL. Workbook;/** Get the contents of the Excel file and use the workbook method to read Excel*/ Public classExcelWorkbook {//Use the list collection to hold the data, which is of type string    PrivateList<string> list=NewArraylist</string><string>(); //to read Excel by workbook wayWorkbook Book;         String username; /** Get the value of the first column of Excel file, get the value of username*/     PublicList</string><string> readusername (String sourcestring)throwsioexception,exception{List</string><string> userlist =NewArraylist</string><string>(); Try{Workbook Book=workbook.getworkbook (NewFile (sourcefile)); Sheet Sheet=book.getsheet (0); //gets the number of rows in the file            intRows=sheet.getrows (); //get the number of columns in a file            intcols=Sheet.getcolumns (); //gets the first row of data, typically the first behavior property value, so this can be ignoredString Col1=sheet.getcell (0,0). getcontents (). Trim (); String col2=sheet.getcell (1,0). getcontents (). Trim (); System.out.println (col1+","+col2); //Place the value of the first column in the UserList             for(intZ=1;z<rows; z++) {String username=sheet.getcell (0, z). getcontents ();            Userlist.add (username); }                     } Catch(Exception e) {e.printstacktrace (); }        //Put the obtained value back out, convenient to call        returnuserlist;} /** Get the value of the second column of Excel file, get the value of password*/     PublicList<string> Readpassword (String sourcestring)throwsioexception,exception{List<string> passlist =NewArraylist</string><string>(); Try{Workbook Book=workbook.getworkbook (NewFile (sourcefile)); Sheet Sheet=book.getsheet (0); intRows=sheet.getrows ();  for(intZ=1;z<rows; z++) {String password=sheet.getcell (1, z). getcontents ();            Passlist.add (password); }                     } Catch(Exception e) {e.printstacktrace (); }        returnPasslist;}  PublicList<string>getList () {returnlist; }}

Step three: Create a new testng Class, fill in the Excel data into the test interface, the code is as follows:

ImportJava.io.File;Importjava.util.List;ImportJava.util.concurrent.TimeUnit;Importorg.openqa.selenium.By;ImportOrg.openqa.selenium.WebDriver;Importorg.openqa.selenium.WebElement;ImportOrg.openqa.selenium.firefox.FirefoxDriver;ImportOrg.openqa.selenium.firefox.FirefoxProfile;ImportOrg.testng.annotations.BeforeClass;Importorg.testng.annotations.Test;ImportFile.excelworkbook; Public classLogincenter {PrivateWebdriver Driver; PrivateString URL; String sourcefile= "path and file name of your file"; @BeforeClass Public voidTestbefore () {//set up Firefox browserFirefoxprofile file=NewFirefoxprofile (NewFile ("C:\\users\\qinfei\\appdata\\roaming\\mozilla\\firefox\\profiles\\t5ourl6s.selenium")); Driver=Newfirefoxdriver (file); URL= "Your test address"; } @Test Public voidLogin ()throwsexception{//Initialize ExcelWorkbook ClassExcelWorkbook excelbook=NewExcelWorkbook (); //go to your test screendriver.get (URL); Driver.manage (). Timeouts (). Implicitlywait (30, Timeunit.seconds); Try{          //Put the username out in the UserList collection.List<string> userlist=Excelbook.readusername (sourcefile); //Put the password out in the Passlist collection.List</string><string> passlist=Excelbook.readpassword (sourcefile); //Enter the value you have taken out into the input box of the interface.          intUsersize=userlist.size ();  for(intI=0;i<usersize; i++){              //Navigate to the username input box via CSSWebelement username=driver.findelement (By.cssselector ("input[name=\" j_username\ "]")); //Navigate to the Password input box via CSSWebelement password=driver.findelement (By.cssselector ("input[name=\" j_password\ "]")); //Locate the login button via XPathWebelement submit=driver.findelement (By.xpath ("//button//span[contains (Text (), ' login ')]")); //clear the contents of the Username input boxusername.clear (); //take out the data from the list one by one.String name=Userlist.get (i); //then fill in the Username input boxUsername.sendkeys (name);  for(intJ=0;j<passlist.size (); j + +) {password.clear (); String Pass=Passlist.get (j);              Password.sendkeys (pass); }                //Click the login buttonSubmit.click (); Driver.manage (). Timeouts (). Implicitlywait (30, Timeunit.seconds); //Locate the Logout button via XPathWebelement logoutbutton=driver.findelement (By.xpath ("//button//span[contains (Text (), ' logout ')]"));                Logoutbutton.click (); Driver.manage (). Timeouts (). Implicitlywait (30, Timeunit.seconds); }      }Catch(Exception e) {e.printstacktrace (); }                 }}

Transferred from: http://www.cydtest.com/?p=3331

Selenium--selenium using Excel for Parameterization

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.