在数据提供程序类中遇到返回类型错误

时间:2018-06-21 09:50:53

标签: java selenium testng testng-dataprovider

有人可以在以下代码中找到问题吗?在数据提供程序类中面临返回类型错误。

这是执行此代码时面临的问题,

"Data Provider public java.lang.Object[][] testCases.TestCases.loginData() must return either Object[][] or Iterator<Object>[], not class [[Ljava.lang.Object;"

下面是包含数据提供者类和测试用例的测试用例类,

  package testCases;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import dataProvider.DataInputProvider;
import loginPage.CreateNewProjectPage;
import loginPage.LoginPage;
import testBase.TestBase;


public class TestCases extends TestBase
{
    TestBase browser = new TestBase();
    LoginPage login = new LoginPage();
    DataInputProvider datainput = new DataInputProvider();

    @DataProvider(name="test1")
    public Object[][] loginData() 
    {
        Object[][] arrayObject = DataInputProvider.datasheet();
        return arrayObject;
    } 

    @Test(dataProvider="test1")
    public void createnewprojtc(String cellvalue1,String cellvalue2)
    {
        browser.initialization(cellvalue1);
        browser.environment(cellvalue2);
        login.username();
        login.password();
        login.loginbtn();
        }
}

下面是包含数据提供者方法的数据提供者类

import java.io.FileInputStream;

import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.testng.annotations.DataProvider;

import testBase.TestBase;

public class DataInputProvider extends TestBase
{

    public static Object[][] datasheet() 
{

    try {
        FileInputStream fis = new FileInputStream("C:\\Users\\Raja\\testgit\\ProjectCreation\\DataInputprovider\\Datafetch.xlsx");
        XSSFWorkbook workbook = new XSSFWorkbook(fis);
        XSSFSheet sheet = workbook.getSheetAt(0);
        int rowCount = sheet.getLastRowNum();
        int columnCount = sheet.getRow(0).getLastCellNum();
        Object[][] data = new Object[rowCount-1][columnCount];

        for (int i = 2; i < rowCount; i++) {
            try {
                XSSFRow row = sheet.getRow(i);
                for (int j = 0; j < columnCount; j++) {
                    try {
                        String cellValue = null ;
                        try {
                            cellValue = row.getCell(j).getStringCellValue();
                        } catch (NullPointerException e) {
                            e.printStackTrace();
                        }
                        data[i][j] = cellValue;
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        fis.close();
        workbook.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    Object[][] data = null;
    return data;
    }

}

下面是登录页面类,其中包含用户名,密码和登录按钮方法,

 package loginPage;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;

import testBase.TestBase;

public class LoginPage extends TestBase
{

    public LoginPage()
    {
        PageFactory.initElements(driver, this);
    }

    @FindBy(how=How.XPATH, using = "//input[@id='txtUsername']")
    WebElement username;

    public LoginPage username()
    {
        username.sendKeys("raja.kulandairaju");
        return this;
    }

    @FindBy(how=How.XPATH, using= "//span[@id='spnPassword']")
    WebElement password;
    public LoginPage password()
    {
        password.sendKeys(prop.getProperty("Password"));
        return this;
    }

    @FindBy(how=How.NAME, using = "Button1")
    WebElement loginbtn;
    public CreateNewProjectPage loginbtn()
    {

        loginbtn.click();
        return new CreateNewProjectPage();
    }
}

下面是基类,其中包含浏览器初始化方法和环境方法,

package testBase;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class TestBase
{

    public WebDriver driver;
    public Properties prop;

    public TestBase()
    {
    try 
    {
        FileInputStream ip = new FileInputStream("C:\\Users\\Raja\\testgit"
                + "\\ProjectCreation\\src\\main\\java\\config\\config.properties");
        prop = new Properties();
        prop.load(ip);
    } catch (FileNotFoundException e) 
    {
        System.out.println("File not found exception");
    } catch (IOException e) 
    {
        e.printStackTrace();
    }
}

    public void initialization(String browser)
    {
        if(browser.equalsIgnoreCase("Chrome"))
        {
            System.setProperty("webdriver.chrome.driver", "D:\\Selenium Workspace\\ProjectCreation\\drivers\\chromedriver.exe");
            driver = new ChromeDriver();
        } else if(browser.equalsIgnoreCase("Firefox"))
        {
            System.setProperty("webdriver.gecko.driver", "D:\\Selenium Workspace\\ProjectCreation\\drivers\\geckodriver_64bit.exe");
            driver = new FirefoxDriver();
        } 

        driver.manage().window().maximize();
        driver.manage().deleteAllCookies();
        driver.manage().timeouts().pageLoadTimeout(20,TimeUnit.SECONDS);
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

    }

    public void environment(String env)
    {
        if(env.equalsIgnoreCase("QA"))
        {
        driver.get("environment");
        }
        if(env.equalsIgnoreCase("Dev"))
        {
        driver.get("environment");
        }
     }       
}

我在Testng框架中使用dataprovider。

1 个答案:

答案 0 :(得分:0)

需要从DataInputProvider类的数据表方法中删除

Object[][] data = null;条件。删除条件后,请再次检查