简体   繁体   English

构造函数不接受使用 Java 在 Selenium 中创建对象期间传递的值

[英]Constructor not taking value passed during object creation in Selenium using Java

I am writing a code wherein I am operating with an excel file having three sheets.我正在编写一个代码,其中我正在使用一个包含三张纸的 excel 文件进行操作。 I am using Apache POI and Data provider annotation for data driven approach.我将 Apache POI 和数据提供程序注释用于数据驱动方法。 I wanted to pass Sheet index number or Sheet name at run time but I was not able to do it setting a parameter to data provider method.我想在运行时传递工作表索引号或工作表名称,但我无法将参数设置为数据提供程序方法。 So what I did is I wrote a constructor and a static variable as integer.所以我所做的是我写了一个构造函数和一个静态变量作为整数。 In parameterized constructor, I initialized that static variable and then in my test case, by creating object of the class which had data provider method, I passed an integer that is, sheet index from which I wanted the data to be taken to run the test.在参数化构造函数中,我初始化了该静态变量,然后在我的测试用例中,通过创建具有数据提供程序方法的类的对象,我传递了一个整数,即我希望从中获取数据来运行测试的工作表索引. But every time, the constructor is taking value as 0. I don't know why.但是每次,构造函数都将值设为 0。我不知道为什么。 Below is the data driven code and constructor I have written.下面是我编写的数据驱动代码和构造函数。

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;
        }
    

The test case which I wrote is as follows.我写的测试用例如下。

@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");}
    }
}

Here, I have passed 2 at the time of object creation but every time, the values are taken from the first sheet.在这里,我在创建对象时传递了 2,但每次都从第一张表中获取值。 That is, sheet index 0 is considered every time.即,每次都考虑工作表索引 0。 I don't want to hard code any value in excelutil class code as I wanted to behave it as generic code.我不想在 excelutil 类代码中硬编码任何值,因为我想将其作为通用代码。 So in test case itself I decided to pass sheet number.因此,在测试用例本身中,我决定传递工作表编号。 I am confused what to do and wanted a reason why this is happening and what is the mistake I am doing.我很困惑该怎么做,并想知道为什么会发生这种情况以及我正在做的错误是什么。 Can anyone please guide me with this ?任何人都可以请指导我吗?

I simplified the code to replicate the issue.我简化了代码来复制这个问题。

Existing code :现有代码:

@Test method : @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 : 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;
 }
}

Output :输出

Data provider called...
Sheet number: 0
Test started
Constructor called...
Sheet number: 2
Email: Nandan & Passowrd: 123456

If you see the above output the DataProvider code is being called first so your value is passing as 0 .如果您看到上述输出,则首先调用DataProvider代码,因此您的值将作为0传递。 To fix this call the new Excel_Util(2);要解决此问题,请调用new Excel_Util(2); object into DataProvider对象到DataProvider

Updated code :更新代码:

@Test method : @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 : 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;
 }
}

Output :输出

Constructor called...
Sheet number: 2
Data provider called...
Sheet number: 2
Test started
Email: Nandan & Passowrd: 123456

If you see the above output now the constructor is being called first.如果您现在看到上面的输出,则首先调用构造函数。

As we discussed if you want to pass the value from Test class then you can do something like this.正如我们所讨论的,如果你想从Test类传递值,那么你可以做这样的事情。 Instead of a constructor use static block.而不是constructor使用static块。

Test class : 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 : 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;
 }
}

Output :输出

Static called...
Sheet number: 2
Data provider called...
Sheet number: 2
Test started
Email: Nandan & Passowrd: 123456

fileinitial=filename;文件初始=文件名; It's a wonder you did not get an error message about a static int cannot be assigned from a non static context!奇怪的是,您没有收到有关无法从非静态上下文分配静态 int 的错误消息! Why not simply take static off your global fileinitial.为什么不简单地从全局文件初始值中去除静态。 *NB the class with the int problem were the main class and if you were calling from public static void main(String args[]) that would be different and not require assigning by the constructor, so if it is not the main class remove static keyword from variable "fileinitial". *NB 有 int 问题的类是主类,如果你从 public static void main(String args[]) 调用它会有所不同,不需要由构造函数分配,所以如果它不是主类,请删除 static来自变量“fileinitial”的关键字。

Or build a static class (always nested subclass) to send the "non static filename argument" into (through) a static method and return it as static assignment from that method.或者构建一个静态类(总是嵌套的子类)以将“非静态文件名参数”发送到(通过)静态方法并将其作为该方法的静态分配返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM