[英]Constructor not taking value passed during object creation in Selenium using Java
我正在編寫一個代碼,其中我正在使用一個包含三張紙的 excel 文件進行操作。 我將 Apache POI 和數據提供程序注釋用於數據驅動方法。 我想在運行時傳遞工作表索引號或工作表名稱,但我無法將參數設置為數據提供程序方法。 所以我所做的是我寫了一個構造函數和一個靜態變量作為整數。 在參數化構造函數中,我初始化了該靜態變量,然后在我的測試用例中,通過創建具有數據提供程序方法的類的對象,我傳遞了一個整數,即我希望從中獲取數據來運行測試的工作表索引. 但是每次,構造函數都將值設為 0。我不知道為什么。 下面是我編寫的數據驅動代碼和構造函數。
public class Excel_Util {
static int fileinitial;
public Excel_Util(int filename) {
fileinitial=filename;
}
@DataProvider(name="testdata")
public static Object[][] readexcelretobj() throws Exception {
FileInputStream f= new FileInputStream("F:\\Eclipse Java new Programs\\shdtestpeopleinteractive\\src\\test\\resources\\Test_data.xls");
HSSFWorkbook workbook = new HSSFWorkbook(f);
HSSFSheet s = workbook.getSheetAt(fileinitial);
HSSFRow row = s.getRow(0);
int rownum=s.getPhysicalNumberOfRows();
int colnum=row.getLastCellNum();
//System.out.println(rownum);
//System.out.println(colnum);
Object data[][] = new Object[rownum][colnum];
List<Object> l = new ArrayList<Object>();
for(int i=0;i<rownum-1;i++) {
row = s.getRow(i+1);
for(int j=0;j<colnum;j++) {
HSSFCell c = row.getCell(j);
//l.add(c.getStringCellValue());
data[i][j]=c.getStringCellValue();
}
}
return data;
}
我寫的測試用例如下。
@Test(dataProvider="testdata",dataProviderClass=Excel_Util.class)
public void testsignupdatafields(String email, String password) {
Syncutil.Implicitwait();
try {
Excel_Util ex = new Excel_Util(2);
getdriver().get("https://www."+csvreaderutil.csvread().get(0).toString()+"/");
Marathishd marathishdpage = PageFactory.initElements(getdriver(), Marathishd.class);
Signuppagemarathishd signup = marathishdpage.letsbeginclick();
signup.emailid.sendKeys(email);
signup.password.sendKeys(password);
}catch(Exception e) {System.out.println("Exception");}
}
}
在這里,我在創建對象時傳遞了 2,但每次都從第一張表中獲取值。 即,每次都考慮工作表索引 0。 我不想在 excelutil 類代碼中硬編碼任何值,因為我想將其作為通用代碼。 因此,在測試用例本身中,我決定傳遞工作表編號。 我很困惑該怎么做,並想知道為什么會發生這種情況以及我正在做的錯誤是什么。 任何人都可以請指導我嗎?
我簡化了代碼來復制這個問題。
@Test
方法:
@Test(dataProvider = "testdata", dataProviderClass = Excel_Util.class)
public void testsignupdatafields(String email, String password) {
System.out.println("Test started");
new Excel_Util(2);
System.out.println("Email: " + email + " & Passowrd: " + password);
}
Excel_Util
:
public class Excel_Util {
static int fileinitial;
public Excel_Util(int filename) {
fileinitial = filename;
System.out.println("Constructor called...");
System.out.println("Sheet number: " + fileinitial);
}
@DataProvider(name = "testdata")
public static Object[][] readexcelretobj() throws Exception {
Object[][] object = new Object[1][2];
object[0][0] = "Nandan";
object[0][1] = "123456";
System.out.println("Data provider called...");
System.out.println("Sheet number: " + fileinitial);
return object;
}
}
輸出:
Data provider called...
Sheet number: 0
Test started
Constructor called...
Sheet number: 2
Email: Nandan & Passowrd: 123456
如果您看到上述輸出,則首先調用DataProvider
代碼,因此您的值將作為0
傳遞。 要解決此問題,請調用new Excel_Util(2);
對象到DataProvider
@Test
方法:
@Test(dataProvider = "testdata", dataProviderClass = Excel_Util.class)
public void testsignupdatafields(String email, String password) {
System.out.println("Test started");
System.out.println("Email: " + email + " & Passowrd: " + password);
}
Excel_Util
:
public class Excel_Util {
static int fileinitial;
public Excel_Util(int filename) {
fileinitial = filename;
System.out.println("Constructor called...");
System.out.println("Sheet number: " + fileinitial);
}
@DataProvider(name = "testdata")
public static Object[][] readexcelretobj() throws Exception {
new Excel_Util(2);
Object[][] object = new Object[1][2];
object[0][0] = "Nandan";
object[0][1] = "123456";
System.out.println("Data provider called...");
System.out.println("Sheet number: " + fileinitial);
return object;
}
}
輸出:
Constructor called...
Sheet number: 2
Data provider called...
Sheet number: 2
Test started
Email: Nandan & Passowrd: 123456
如果您現在看到上面的輸出,則首先調用構造函數。
正如我們所討論的,如果你想從Test
類傳遞值,那么你可以做這樣的事情。 而不是constructor
使用static
塊。
Test
類:
public class ClassTestNG {
static int sheetNumber = 2;
@Test(dataProvider = "testdata", dataProviderClass = Excel_Util.class)
public void testsignupdatafields(String email, String password) {
System.out.println("Test started");
System.out.println("Email: " + email + " & Passowrd: " + password);
}
Excel_Util
:
public class Excel_Util {
static int fileinitial;
static {
fileinitial = ClassTestNG.sheetNumber;
System.out.println("Static called...");
System.out.println("Sheet number: " + fileinitial);
}
@DataProvider(name = "testdata")
public static Object[][] readexcelretobj() throws Exception {
Object[][] object = new Object[1][2];
object[0][0] = "Nandan";
object[0][1] = "123456";
System.out.println("Data provider called...");
System.out.println("Sheet number: " + fileinitial);
return object;
}
}
輸出:
Static called...
Sheet number: 2
Data provider called...
Sheet number: 2
Test started
Email: Nandan & Passowrd: 123456
文件初始=文件名; 奇怪的是,您沒有收到有關無法從非靜態上下文分配靜態 int 的錯誤消息! 為什么不簡單地從全局文件初始值中去除靜態。 *NB 有 int 問題的類是主類,如果你從 public static void main(String args[]) 調用它會有所不同,不需要由構造函數分配,所以如果它不是主類,請刪除 static來自變量“fileinitial”的關鍵字。
或者構建一個靜態類(總是嵌套的子類)以將“非靜態文件名參數”發送到(通過)靜態方法並將其作為該方法的靜態分配返回。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.