2017-05-29 19 views
-1

此腳本用於讀取Excel中的數據並在硒腳本中使用它。這使用Apache POI來讀取數據,將其存儲在變量中並使用它。讀取Excel數據並在硒中使用

+1

什麼問題? –

+0

@MatheswaranKanagarajan你能考慮使用您的工作,研究,相關HTML DOM和錯誤堆棧跟蹤更新問題區域,以便SO志願者進一步分析?謝謝 – DebanjanB

回答

0

你可以試試下面的代碼。我已採取的Facebook作爲示例應用程序。

package testPackage; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.util.concurrent.TimeUnit; 

import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.chrome.ChromeDriver; 

import org.testng.annotations.*; 
import org.testng.annotations.DataProvider; 

import jxl.Sheet; 
import jxl.Workbook; 
import jxl.read.biff.BiffException; 

public class ExcelReadingwithDP { 

WebDriver driver; 
@BeforeTest 
public void OpenApp() 
{ 
    System.setProperty("webdriver.chrome.driver", "E:/Selenium/Webdriver/Softwares/chromedriver.exe"); 
    driver = new ChromeDriver(); 
    driver.navigate().to("http://facebook.com"); 
    driver.manage().window().maximize(); 
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
} 


@Test(dataProvider="empLogin") 
public void login(String username, String password) 
{ 
    WebElement login1 = driver.findElement(By.id("email")); 
    login1.clear(); 
    login1.sendKeys(username); 
    WebElement passwd=driver.findElement(By.id("pass")); 
    passwd.clear(); 
    passwd.sendKeys(password); 
    driver.findElement(By.xpath("//*[@id='u_0_q']")).click(); 
    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS); 
    WebElement back = driver.findElement(By.xpath("//*[@id='blueBarDOMInspector']/div/div[1]/div/div/h1/a/i")); 
    back.click(); 
} 


@DataProvider(name="empLogin") 
    public Object[][] logindata() 
{ 
    Object[][] arrayobject = getexceldata("E://Deepak/IntranetLogin.xls","Sheet1"); 
    return arrayobject; 
} 

public String[][] getexceldata(String filename, String sheetname) 
{ 

    String[][] arrayexceldata = null; 
    try 
    { 
    FileInputStream fis = new FileInputStream(filename); 
    Workbook wb = Workbook.getWorkbook(fis); 
    Sheet sh = wb.getSheet(sheetname); 
    int row = sh.getRows(); 
    int col = sh.getColumns(); 
    arrayexceldata = new String[row-1][col]; 
    for (int i=1;i< row;i++) 
    { 
     for(int j=0;j<col;j++) 
     { 
      arrayexceldata[i-1][j]=sh.getCell(j,i).getContents(); 
     } 
    } 

    } 
    catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     e.printStackTrace(); 
    } catch (BiffException e) { 
     e.printStackTrace(); 
    } 
    return arrayexceldata; 

} 

}

1
/* 
* Download Apache POI from https://www.apache.org/dyn/closer.lua/poi/release/bin/poi-bin-3.16-20170419.zip 
* 
*/ 

import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.util.Date; 
import java.util.List; 

import org.apache.poi.hssf.usermodel.HSSFCell; 
import org.apache.poi.hssf.usermodel.HSSFRow; 
import org.apache.poi.hssf.usermodel.HSSFSheet; 
import org.apache.poi.hssf.usermodel.HSSFWorkbook; 
import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.WebElement; 
import org.openqa.selenium.chrome.ChromeDriver; 

public class FormFill { 
    public static void main(String[] args) throws Exception { 



     try { 
      FileInputStream fileInputStream = new FileInputStream("C:\\data.xls"); 
      HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream); 
      HSSFSheet worksheet = workbook.getSheet("sheet1"); 
      HSSFRow row1 = worksheet.getRow(0); 
      HSSFCell cellA1 = row1.getCell((short) 0); 
      String a1Val = cellA1.getStringCellValue(); 
      HSSFCell cellB1 = row1.getCell((short) 1); 
      String b1Val = cellB1.getStringCellValue(); 

      System.out.println("A1: " + a1Val); 
      System.out.println("B1: " + b1Val); 

     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 


     String url = "http://thedemosite.co.uk/addauser.php"; 
     System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe"); 
     WebDriver driver = new ChromeDriver(); 
     driver.get(url); 
     //Thread.sleep(30000); 
     driver.findElement(By.name("username")).sendKeys(a1Val); 
     driver.findElement(By.name("password")).sendKeys(b1Val); 


    } 

} 
-1

我覺得csv比Excel好,因爲Excel有更多的時間來讀取數據,但csv是快速的。

私有靜態最後字符串CSV_SEPARATOR =」,(=(?:?[^ \ 「] \」[^ \ 「] \」)[^ \ 「] $)」;

public List<String[]> parseFile(String fileName) { 
    try { 
     BufferedReader br = new BufferedReader(new FileReader(fileName)); 
     br.readLine(); // skip header; 
     String line = br.readLine(); 
     List<String[]> lines = new ArrayList(); 
     while (line != null) { 
      //System.out.println(line.toString()); 
      lines.add(line.split(CSV_SEPARATOR)); 
      line = br.readLine(); 
     } 
     return lines; 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    return null; 
}