此腳本用於讀取Excel中的數據並在硒腳本中使用它。這使用Apache POI來讀取數據,將其存儲在變量中並使用它。讀取Excel數據並在硒中使用
-1
A
回答
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;
}
相關問題
- 1. 用testNG讀取Excel中的excel數據
- 2. 硒代碼從excel中獲取數據
- 3. 用javascript讀取excel數據?
- 4. 用水豚或硒讀取excel文件
- 5. 使用c讀取excel的大數據#
- 6. 使用OLEDB從Excel讀取數據
- 7. 使用VB.NET按列讀取excel數據
- 8. 想使用硒從excel中讀取用戶憑證
- 9. 讀取硒中的xml數據
- 10. 使用PHPExcel與Codeigniter讀取excel文件並寫入數據庫
- 11. 使用Powershell讀取Excel數據並寫入變量
- 12. poi讀取現有的excel並使用大數據編輯
- 13. 使用C#讀取Excel文件並清理數據
- 14. 使用POI讀取Java數據庫(CLOB列)中的Excel數據
- 15. 閱讀在vb.net中使用ssis合併excel數據
- 16. 使用硒讀取表格
- 17. 如何在Yii 2中讀取excel文件並顯示數據?
- 18. 使用java讀取excel後在列表中存儲數據
- 19. 從excel表格中讀取數據並在同一張excel表格中寫入
- 20. 如何使用硒python讀表數據?
- 21. outOfMemoryException當讀取excel數據
- 22. 循環讀取excel數據
- 23. MATLAB讀取Excel數據環
- 24. Excel數據讀取和PHP
- 25. 從C#讀取Excel數據#
- 26. 從Excel讀取數據
- 27. 從python讀取excel數據
- 28. 從Excel中讀取列中的數據
- 29. 讀取標籤中使用硒的webdriver
- 30. 從excel中讀取數據並忽略python中的空行
什麼問題? –
@MatheswaranKanagarajan你能考慮使用您的工作,研究,相關HTML DOM和錯誤堆棧跟蹤更新問題區域,以便SO志願者進一步分析?謝謝 – DebanjanB