【问题标题】:Data Provider using excel sheet:I am providing data from excel sheet by using data provider but my code i code is not working使用 excel 表的数据提供者:我正在使用数据提供者从 excel 表中提供数据,但我的代码我的代码不起作用
【发布时间】:2018-04-04 01:32:41
【问题描述】:

这是我的测试类、excel阅读器类、启动浏览器类和控制台的代码

#Excel阅读器类

        package com.utility;

        import java.io.FileInputStream;

        import java.io.FileNotFoundException;

        import java.io.FileOutputStream;

        import java.io.IOException;

        import org.apache.poi.xssf.usermodel.XSSFCell;

        import org.apache.poi.xssf.usermodel.XSSFRow;

        import org.apache.poi.xssf.usermodel.XSSFSheet;

        import org.apache.poi.xssf.usermodel.XSSFWorkbook;

        public class ExcelUtils {

            private static XSSFSheet ExcelWSheet;

            private static XSSFWorkbook ExcelWBook;

            private static XSSFCell Cell;

            private static XSSFRow Row;


            public static Object[][] getTableArray(String FilePath, String SheetName) throws Exception {   

                String[][] tabArray = null;

                try {

                    FileInputStream ExcelFile = new FileInputStream(FilePath);

                    // Access the required test data sheet

                    ExcelWBook = new XSSFWorkbook(ExcelFile);

                    ExcelWSheet = ExcelWBook.getSheet(SheetName);

                    int startRow = 1;

                    int startCol = 1;

                    int ci,cj;

                    int totalRows = ExcelWSheet.getLastRowNum();

                    int totalCols = 3;


                    tabArray=new String[totalRows][totalCols];

                    ci=0;

                    for (int i=startRow;i<=totalRows;i++, ci++) {                  

                        cj=0;

                        for (int j=startCol;j<=totalCols;j++, cj++){

                            tabArray[ci][cj]=getCellData(i,j);

                            System.out.println(tabArray[ci][cj]);  

                        }

                    }

                }

                catch (FileNotFoundException e){

                    System.out.println("Could not read the Excel sheet");

                    e.printStackTrace();

                }

                catch (IOException e){

                    System.out.println("Could not read the Excel sheet");

                    e.printStackTrace();

                }

                return(tabArray);

            }

            public static String getCellData(int RowNum, int ColNum) throws Exception {

                try{

                    Cell = ExcelWSheet.getRow(RowNum).getCell(ColNum);

                    int dataType = Cell.getCellType();

                    if  (dataType == 3) {

                        return "";

                    }else{

                        String CellData = Cell.getStringCellValue();

                        return CellData;

                    }


                }catch (Exception e){

                    System.out.println(e.getMessage());

                    throw (e);

                }


            }
        }
    --------------------Test class-------------------------

        package com.horra.test.fullflow;

        import org.openqa.selenium.WebDriver;
        import org.testng.annotations.DataProvider;
        import org.testng.annotations.Test;


        import com.horra.pages.Horaa;
        import com.utility.ExcelUtils;

        import HoraaAutomation.HoraaAutomation.LaunchBrowser;

        public class HoraaApp extends LaunchBrowser {
            @DataProvider(name="Authentication")

            public Object[][] Authentication() throws Exception{

                 Object[][] testObjArray = ExcelUtils.getTableArray("D:\\Users\\Richa\\eclipse-workspace\\HoraaAutomation\\src\\test\\java\\com\\testData\\WebTest.xlsx","Login");

                 return (testObjArray);

                }
            @Test(dataProvider="Authentication")
            public void testHoraa(String Email,String Password,String Username ) throws Exception {


                Horaa login= new Horaa(driver); 


                login.enterEmailOrMobileNumber(Email);
                login.clickOnNextButton();
                login.enterPassword(Password);
                login.clickOnSubmitButton();

                login.clickOnUserIcon();
                login.clickOnAddUserButton();
                login.enterUserName(Username);
                login.clickOnDesignation();
                login.clickOnManager(); 

        }
        }
    ---------------------------Launch Browser -----------------------------

    package HoraaAutomation.HoraaAutomation;
    import org.testng.annotations.BeforeClass;
    import org.testng.annotations.BeforeMethod;
    import org.testng.annotations.BeforeSuite;
    import org.testng.annotations.BeforeTest;

    import com.relevantcodes.extentreports.ExtentReports;
    import com.relevantcodes.extentreports.ExtentTest;
    import com.relevantcodes.extentreports.LogStatus;

    import java.io.File;
    import java.lang.reflect.Method;
    import java.util.concurrent.TimeUnit;

    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    import org.testng.Assert;
    import org.testng.ITestResult;
    import org.testng.SkipException;
    import org.testng.annotations.AfterClass;
    import org.testng.annotations.AfterMethod;
    import org.testng.annotations.AfterTest;

    public class LaunchBrowser {
        protected WebDriver driver;
        ExtentReports extent;
        ExtentTest logger;



        @BeforeClass
        public void launchChromerBrowser() throws Exception {

            //  System.setProperty("webdriver.chrome.driver","C:\\Users\\Admin\\Downloads\\chromedriver_win32\\chromedriver.exe");  
            System.setProperty("webdriver.chrome.driver","C:\\Users\\Admin\\Downloads\\chromedriver_win32 (2)\\chromedriver.exe");
            driver = new ChromeDriver();
            driver.manage().window().maximize();
            driver.get("http://testweb.horaa.in/");
            //driver.get("http://devweb.horaa.in");
            //Thread.sleep(5000);
            driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

        }
        @BeforeTest
        public void startReport(){
            extent = new ExtentReports (System.getProperty("user.dir") +"/test-output/richa.html", true);
            extent
            .addSystemInfo("Host Name", "Test")
            .addSystemInfo("Environment", "Automation Testing")
            .addSystemInfo("User Name", "Richa");
            //loading the external xml file (i.e., extent-config.xml) which was placed under the base directory
            //You could find the xml file below. Create xml file in your project and copy past the code mentioned below
             extent.loadConfig(new File(System.getProperty("user.dir")+"\\extent-config.xml"));



        }
        @BeforeMethod
        public void passTest(){ 
            logger = extent.startTest("passTest");
            Assert.assertTrue(true);
            logger.log(LogStatus.PASS, "Test Case Passed is passTest");
        }

        public void failTest(){
            logger = extent.startTest("failTest");
            Assert.assertTrue(false);
            logger.log(LogStatus.PASS, "Test Case (failTest) Status is passed");
        }


        public void skipTest(){
            logger = extent.startTest("skipTest");
            throw new SkipException("Skipping - This is not ready for testing ");
        }

        @AfterMethod
        public void getResult(ITestResult result){
            if(result.getStatus() == ITestResult.FAILURE){
                logger.log(LogStatus.FAIL, "Test Case Failed is "+result.getName());
                logger.log(LogStatus.FAIL, "Test Case Failed is "+result.getThrowable());
            }else if(result.getStatus() == ITestResult.SKIP){
                logger.log(LogStatus.SKIP, "Test Case Skipped is "+result.getName());
            }
            // ending test
            //endTest(logger) : It ends the current test and prepares to create HTML report
            extent.endTest(logger);
        }
        @AfterTest
        public void endReport(){

            extent.flush();
        }
        @AfterClass()
        public void closeBrowser()

        {   

            //driver.quit();
            driver.close();

        }


    }

----------控制台---------- ------------------

 [RemoteTestNG] detected TestNG version 6.12.0
        Starting ChromeDriver 2.36.540470 (e522d04694c7ebea4ba8821272dbef4f9b818c91) on port 40437
        Only local connections are allowed.
        Apr 04, 2018 6:25:37 AM org.openqa.selenium.remote.ProtocolHandshake createSession
        INFO: Detected dialect: OSS
        dhoni@csk.com
        welcome
        TestUser
        null
        [Utils] [ERROR] [Error] java.lang.NullPointerException
            at com.utility.ExcelUtils.getCellData(ExcelUtils.java:101)
            at com.utility.ExcelUtils.getTableArray(ExcelUtils.java:65)
            at com.horra.test.fullflow.HoraaApp.Authentication(HoraaApp.java:18)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            at java.lang.reflect.Method.invoke(Method.java:498)
            at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)
            at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:55)
            at org.testng.internal.MethodInvocationHelper.invokeMethodNoCheckedException(MethodInvocationHelper.java:45)
            at org.testng.internal.MethodInvocationHelper.invokeDataProvider(MethodInvocationHelper.java:115)
            at org.testng.internal.Parameters.handleParameters(Parameters.java:612)
            at org.testng.internal.Invoker.handleParameters(Invoker.java:1316)
            at org.testng.internal.Invoker.createParameters(Invoker.java:1044)
            at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1134)
            at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
            at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
            at org.testng.TestRunner.privateRun(TestRunner.java:776)
            at org.testng.TestRunner.run(TestRunner.java:634)
            at org.testng.SuiteRunner.runTest(SuiteRunner.java:425)
            at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:420)
            at org.testng.SuiteRunner.privateRun(SuiteRunner.java:385)
            at org.testng.SuiteRunner.run(SuiteRunner.java:334)
            at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
            at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
            at org.testng.TestNG.runSuitesSequentially(TestNG.java:1318)
            at org.testng.TestNG.runSuitesLocally(TestNG.java:1243)
            at org.testng.TestNG.runSuites(TestNG.java:1161)
            at org.testng.TestNG.run(TestNG.java:1129)
            at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
            at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
            at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

        SKIPPED: testHoraa
        java.lang.RuntimeException: java.lang.NullPointerException
            at org.testng.internal.MethodInvocationHelper.invokeMethodNoCheckedException(MethodInvocationHelper.java:49)
            at org.testng.internal.MethodInvocationHelper.invokeDataProvider(MethodInvocationHelper.java:115)
            at org.testng.internal.Parameters.handleParameters(Parameters.java:612)
            at org.testng.internal.Invoker.handleParameters(Invoker.java:1316)
            at org.testng.internal.Invoker.createParameters(Invoker.java:1044)
            at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1134)
            at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
            at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
            at org.testng.TestRunner.privateRun(TestRunner.java:776)
            at org.testng.TestRunner.run(TestRunner.java:634)
            at org.testng.SuiteRunner.runTest(SuiteRunner.java:425)
            at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:420)
            at org.testng.SuiteRunner.privateRun(SuiteRunner.java:385)
            at org.testng.SuiteRunner.run(SuiteRunner.java:334)
            at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
            at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
            at org.testng.TestNG.runSuitesSequentially(TestNG.java:1318)
            at org.testng.TestNG.runSuitesLocally(TestNG.java:1243)
            at org.testng.TestNG.runSuites(TestNG.java:1161)
            at org.testng.TestNG.run(TestNG.java:1129)
            at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
            at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
            at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
        Caused by: java.lang.NullPointerException
            at com.utility.ExcelUtils.getCellData(ExcelUtils.java:101)
            at com.utility.ExcelUtils.getTableArray(ExcelUtils.java:65)
            at com.horra.test.fullflow.HoraaApp.Authentication(HoraaApp.java:18)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
            at java.lang.reflect.Method.invoke(Method.java:498)
            at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)
            at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:55)
            at org.testng.internal.MethodInvocationHelper.invokeMethodNoCheckedException(MethodInvocationHelper.java:45)
            ... 22 more


    ===============================================
        Default test
        Tests run: 1, Failures: 0, Skips: 1
    ===============================================


    ===============================================
    Default suite
    Total tests run: 1, Failures: 0, Skips: 1
    ===============================================

【问题讨论】:

  • 您能解释一下您的代码要做什么吗?此外,您能否尝试删除与您的问题无关的代码
  • 哇,那个帖子是一个一团糟,而且非常不清楚你在问什么以及任何标签是如何相关的。有关@Legman 建议的提示,请参阅此处:“minimal reproducible example”和更多信息:“How to Ask”以及优秀指南(来自Stack Overflow 的顶级用户)here

标签: excel eclipse selenium dataprovider testng-dataprovider


【解决方案1】:

见异常SKIPPED: testHoraa java.lang.RuntimeException: java.lang.NullPointerException 这是空指针异常,它即将到来是因为您可能从getCellData 数据方法返回空值。检查您的 getCellData 方法实现。

在excel表格中检查第4行,它必须包含null,请参阅测试日志包含null...

[RemoteTestNG] detected TestNG version 6.12.0
        Starting ChromeDriver 2.36.540470 (e522d04694c7ebea4ba8821272dbef4f9b818c91) on port 40437
        Only local connections are allowed.
        Apr 04, 2018 6:25:37 AM org.openqa.selenium.remote.ProtocolHandshake createSession
        INFO: Detected dialect: OSS
        dhoni@csk.com
        welcome
        TestUser
        null
        [Utils] [ERROR] [Error] java.lang.NullPointerException
            at com.utility.ExcelUtils.getCellData(ExcelUtils.java:101)
        ......

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-07
    • 2021-12-24
    • 2016-10-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多