Lire des données à partir de fichier Excel en Sélénium Java

Je suis en train de lire des données à partir d'une feuille excel pour automatiser mes tests(avec un certain nombre d'informations de connexion). Je suis à l'aide d'un utilitaire que j'ai trouvé sur le web. Mais il n'est pas en cours d'exécution avec succès.

Ici est l'utilitaire

    package google;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable; 
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class class2 {
static Sheet wrksheet;
static Workbook wrkbook =null;
static Hashtable dict= new Hashtable();
//Create a Constructor
public class2(String ExcelSheetPath) throws BiffException, IOException
{
//Initialize
wrkbook = Workbook.getWorkbook(new File(ExcelSheetPath));
//For Demo purpose the excel sheet path is hardcoded, but not recommended :)
wrksheet = wrkbook.getSheet("Sheet1");
}
//Returns the Number of Rows
public static int RowCount()
{
return wrksheet.getRows();
`enter code here` }
//Returns the Cell value by taking row and Column values as argument
public static String ReadCell(int column,int row)
{
return wrksheet.getCell(column,row).getContents();
}
//Create Column Dictionary to hold all the Column Names
public static void ColumnDictionary()
{`enter code here`
//Iterate through all the columns in the Excel sheet and store the value       
for(int col=0; col <= wrksheet.getColumns();col++)
{
dict.put(ReadCell(col,0), col);
}
}
//Read Column Names
public static int GetCell(String colName)
{
try {
int value;
value = ((Integer) dict.get(colName)).intValue();
return value;
} catch (NullPointerException e) {
return (0);
}
}
}

Et à la suite est la classe qui appelle cet utilitaire.

package google;
import java.io.IOException;
import jxl.read.biff.BiffException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import google.class2;
public class class3 {
//Global initialization of Variables
static class2 xlsUtil;
WebDriver driver = new InternetExplorerDriver();
//Constructor to initialze Excel for Data source
public class3() throws BiffException, IOException
{
//Let's assume we have only one Excel File which holds all Testcases. Demo !!!
xlsUtil = new class2("C:/Users/admin/workspace/login.xls");
//Load the Excel Sheet Col in to Dictionary for Further use in our Test cases.
xlsUtil.ColumnDictionary();
}
@BeforeTest
public void EnvironmentalSetup()
{
System.setProperty("webdriver.chrome.driver",
"C:/Users/admin/Downloads/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://192.168.1.20/dental/userlogin");
}
@Test
public void GmailLoginPage() throws InterruptedException {
//Create a for loop.. for iterate through our Excel sheet for all the test cases.
for(int rowCnt = 1;rowCnt <= xlsUtil.RowCount();rowCnt++)
{
//Enter User Name by reading data from Excel
WebElement userName = driver.findElement(By.name("UserName"));
userName.clear();
userName.sendKeys(xlsUtil.ReadCell(xlsUtil.GetCell("EmailUserName"), rowCnt));
//Enter Password
WebElement password = driver.findElement(By.name("Password"));
password.clear();
password.sendKeys(xlsUtil.ReadCell(xlsUtil.GetCell("Emailpassword"), rowCnt));
//Click on the Sign In Button
//WebElement signin = driver.findElement(By.name("signIn"));
password.submit();
//Sleep for some time,so that we can see things in action @ Screen :)
Thread.sleep(2000);
}
}
}

Mais lorsque je lance le dis cass, il dit " cant instancier google.class3
Je n'ai pas l'erreur ici.
Merci de m'aider à exécuter ce code avec succès.

  • Pour info: je vous recommande d'utiliser Apache POI pour toutes les actions avec les formats de fichiers MS Office. Il est très stable, mature bibliothèque de l'usage est très répandu.
  • grosso modo, mais au lieu d'un lance clause dans votre constructeur, utiliser un bloc try catch dans le constructeur et le vérifier. peut-être la raison
  • Le code peut être trouvé herehttps://docs.google.com/document/d/1p9E_Ob9HevUVdOXOOylLMgqL7J_KTxyghj_f8pjr4lg/modifier
  • balaji krishnan, j'ai essayé de faire ça , mais même résultat. 🙁
  • Envisager popfalushi de l'indice. Nous sommes à l'aide de ApachePOI pour Excel base de tests depuis une couple d'années et son (je cite): "très stable, mature bibliothèque"
InformationsquelleAutor Jaydev | 2013-10-16