Selenium——selenium之利用excel實現參數化


說明:我是通過Workbook方式來讀取excel文件的,這次以登陸界面為例
備注:使用Workbook讀取excel文件,前提是excel需要2003版本,其他版本暫時不支持
具體步驟:
第一步:新建一個excel文件,並且輸入數據內容
第二步:在eclipse中新建一個java class,編寫獲取excel文件的代碼

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
 
import jxl.Sheet;
import jxl.Workbook;
/*
 * 獲取Excel文件的內容,使用Workbook方式來讀取excel
 */
public class ExcelWorkBook {
    //利用list集合來存放數據,其類型為String
    private List<string> list=new ArrayList</string><string>();
    //通過Workbook方式來讀取excel
    Workbook book;
    String username;
     
    /*
     * 獲取excel文件第一列的值,這里取得值為username
     */
    public List</string><string> readUsername(String sourceString) throws IOException,Exception{
        List</string><string> userList = new ArrayList</string><string>();
         
        try {
            Workbook book =Workbook.getWorkbook(new File(sourceFile));
            Sheet sheet=book.getSheet(0);
            //獲取文件的行數
            int rows=sheet.getRows();
            //獲取文件的列數
            int cols=sheet.getColumns();
            //獲取第一行的數據,一般第一行為屬性值,所以這里可以忽略
            String col1=sheet.getCell(0,0).getContents().trim();
            String col2=sheet.getCell(1,0).getContents().trim();
            System.out.println(col1+","+col2);
            //把第一列的值放在userlist中
            for(int z=1;z<rows ;z++){
                String username=sheet.getCell(0,z).getContents();
                userList.add(username);
            }
             
        } catch (Exception e) {
            e.printStackTrace();
        }
        //把獲取的值放回出去,方便調用
        return userList;
}
    /*
     * 獲取excel文件第二列的值,這里取得值為password
     */
    public List<String> readPassword(String sourceString) throws IOException,Exception{
        List<string> passList = new ArrayList</string><string>();
         
        try {
            Workbook book =Workbook.getWorkbook(new File(sourceFile));
            Sheet sheet=book.getSheet(0);
            int rows=sheet.getRows();
            for(int z=1;z<rows ;z++){
                String password=sheet.getCell(1,z).getContents();
                passList.add(password);
            }
             
        } catch (Exception e) {
            e.printStackTrace();
        }
        return passList;
}
     
    public List<String> getList(){
        return list;
    }
}

第三步:新建一個TestNg Class,把excel數據填寫到測試界面,具體代碼如下:

import java.io.File;
import java.util.List;
import java.util.concurrent.TimeUnit;
 
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
 
import File.ExcelWorkBook;
 
public class LoginCenter {
    private WebDriver driver;
    private String url;
    String sourceFile="你文件的路徑和文件名稱";
    @BeforeClass
    public void testBefore(){
                //設置firefox瀏覽器
        FirefoxProfile file=new FirefoxProfile(new File("C:\\Users\\qinfei\\AppData\\Roaming\\Mozilla\\Firefox\\Profiles\\t5ourl6s.selenium"));
        driver=new FirefoxDriver(file);
        url="你的測試地址";
         
    }
  @Test
  public void login() throws Exception{
//初始化ExcelWorkBook Class
      ExcelWorkBook excelbook=new ExcelWorkBook();
      //進入到你的測試界面
      driver.get(url);
      driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
      try{
          //把取出的username放在userlist集合里面
          List<string> userList=excelbook.readUsername(sourceFile);
          //把取出的password放在passlist集合里面
          List</string><string> passList=excelbook.readPassword(sourceFile);
          //把取出來的值,輸入到界面的輸入框中
          int usersize=userList.size();
          for(int i=0;i<usersize ;i++){
              //通過css定位到username輸入框
              WebElement username=driver.findElement(By.cssSelector("input[name=\"j_username\"]"));
              //通過css定位到password輸入框
              WebElement password=driver.findElement(By.cssSelector("input[name=\"j_password\"]"));
              //通過xpath定位登錄按鈕
              WebElement submit=driver.findElement(By.xpath("//button//span[contains(text(),'登錄')]"));
              //清除username輸入框的內容
              username.clear();
              //把list中數據一個一個的取出來
              String name=userList.get(i);
              //然后填寫到username輸入框
              username.sendKeys(name);
              for(int j=0;j<passList.size();j++){
                  password.clear();
                  String pass=passList.get(j);
                  password.sendKeys(pass);
              }
                //點擊登錄按鈕
                submit.click();
                driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
                //通過xpath定位登出按鈕
                WebElement logoutButton=driver.findElement(By.xpath("//button//span[contains(text(),'登出')]"));
                logoutButton.click();
                driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
          }
      }catch(Exception e){
          e.printStackTrace();
      }
       
       
 
  }
}

 

轉自:http://www.cydtest.com/?p=3331

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM