получение ошибки java .lang.NullPointerException в управляемом данными тестировании с использованием @DataProvider в testng - PullRequest
0 голосов
/ 06 апреля 2020

Я создал нижеприведенную программу для тестирования на основе данных в testng и получения ошибки java .lang.NullPointerException.

Это управляемое данными тестирование, которое просто извлекает значения из Excel с использованием @Dataprovider в testNG , но когда я пытаюсь запустить эту программу, она выдает ошибку java .lang.nullpointerexception

package com.datadriven.test;

            import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import com.test.utility.TestNGTestUtil;

public class MercuryToursTest {

    public WebDriver driver;
    @BeforeTest
    public void setUp() {
        System.setProperty("webdriver.chrome.driver","F:\\Drivers\\chromedriver_win32\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();

        driver.get("http://newtours.demoaut.com/");

        driver.findElement(By.linkText("REGISTER")).click();
        driver.manage().window().maximize();
        driver.manage().deleteAllCookies();

        driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

    }

    @DataProvider

    public Iterator<Object[]> getTestData(){

        //in the below line we are calling directly thru class name because the method getDataFromExcel is static in nature
        ArrayList<Object[]> testData = TestNGTestUtil.getDataFromExcel();
        return testData.iterator();
    }

    @Test(dataProvider ="getTestData" )

    public void mercuryToursRegTest(String firstName,String LastName,String Phone,String Email,
            String Address1,String Address2,String City,String State,
            String PostalCode,String Country)  {
        //driver.findElement(By.linkText("REGISTER")).click();  
        driver.findElement(By.xpath("//input[@name = 'firstName']")).sendKeys(firstName);
        driver.findElement(By.xpath("//input[@name = 'lastName']")).sendKeys(LastName);
        driver.findElement(By.xpath("//input[@name = 'phone']")).sendKeys(Phone);
        driver.findElement(By.xpath("//input[@id = 'userName']")).sendKeys(Email);
        driver.findElement(By.xpath("//input[@name = 'address1']")).sendKeys(Address1);
        driver.findElement(By.xpath("//input[@name = 'address2']")).sendKeys(Address2);
        driver.findElement(By.xpath("//input[@name = 'city']")).sendKeys(City);
        driver.findElement(By.xpath("//input[@name = 'state']")).sendKeys(State);
        driver.findElement(By.xpath("//input[@name = 'postalCode']")).sendKeys(PostalCode);

        Select select = new Select(driver.findElement(By.xpath("//*[@name = 'country']")));
        select.selectByVisibleText(Country);
    }

    @AfterTest
    public void tearDown()  {
            driver.quit();

    }
}

и нижеприведенную программу TestNGTestUtil в другом пакете

package com.test.utility;

import java.util.ArrayList;

import com.excel.utility.Xls_Reader;

public class TestNGTestUtil {

    static Xls_Reader reader;

    public static ArrayList<Object[]> getDataFromExcel() {


        ArrayList<Object[]> myData = new ArrayList<Object[]>();
        try {
            reader = new Xls_Reader("F:\\Vishal_Offc Work\\Workspace\\SeleniumBasics\\src\\com\\testdata\\NewToursRegTestData.xlsx");
        } catch(Exception e) {
            e.printStackTrace();
        }
        for(int rowNum = 2 ; rowNum<=reader.getRowCount("RegTestData") ; rowNum++ ) {

            String firstName = reader.getCellData("RegTestData", "FirstName", 2);
            String LastName = reader.getCellData("RegTestData", "LastName", 2);
            String Phone = reader.getCellData("RegTestData", "Phone", 2);
            String Email = reader.getCellData("RegTestData", "Email", 2);
            String Address1 = reader.getCellData("RegTestData", "Address1", 2);
            String Address2 = reader.getCellData("RegTestData", "Address2", 2);
            String City = reader.getCellData("RegTestData", "City", 2);
            String State = reader.getCellData("RegTestData", "State", 2);
            String PostalCode = reader.getCellData("RegTestData", "PostalCode", 2);
            String Country = reader.getCellData("RegTestData", "Country", 2);

            Object ob[] = {firstName,LastName,Phone,Email,Address1,Address2,City,State,
                    PostalCode,Country};
            myData.add(ob);
        }
        return myData;

    }

}

и ниже ошибка в консоли.

[RemoteTestNG] detected TestNG version 7.2.0
Starting ChromeDriver 80.0.3987.106 (f68069574609230cf9b635cd784cfb1bf81bb53a-refs/branch-heads/3987@{#882}) on port 1995
Only local connections are allowed.
Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
[1586164137.810][WARNING]: Timed out connecting to Chrome, retrying...
Apr 06, 2020 2:39:00 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
[1586164143.122][SEVERE]: Timed out receiving message from renderer: 0.100
[1586164143.235][SEVERE]: Timed out receiving message from renderer: 0.100
[1586164144.127][SEVERE]: Timed out receiving message from renderer: 0.100
[1586164144.498][SEVERE]: Timed out receiving message from renderer: 0.100
[1586164146.573][WARNING]: Timed out connecting to Chrome, retrying...
FAILED CONFIGURATION: @AfterTest tearDown
java.lang.NullPointerException
    at com.datadriven.test.MercuryToursTest.tearDown(MercuryToursTest.java:70)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:135)
    at org.testng.internal.MethodInvocationHelper.invokeMethodConsideringTimeout(MethodInvocationHelper.java:64)
    at org.testng.internal.ConfigInvoker.invokeConfigurationMethod(ConfigInvoker.java:364)
    at org.testng.internal.ConfigInvoker.invokeConfigurations(ConfigInvoker.java:318)
    at org.testng.TestRunner.invokeTestConfigurations(TestRunner.java:620)
    at org.testng.TestRunner.afterRun(TestRunner.java:850)
    at org.testng.TestRunner.run(TestRunner.java:591)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:384)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:378)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:337)
    at org.testng.SuiteRunner.run(SuiteRunner.java:286)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1214)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1136)
    at org.testng.TestNG.runSuites(TestNG.java:1066)
    at org.testng.TestNG.run(TestNG.java:1034)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

FAILED: mercuryToursRegTest("Tom", "Peter", "6.54564889E8", "tom@yahoo.com", "12,new street", "downtown", "Mumbai", "Maharashtra", "400016.0", "INDIA")
java.lang.NullPointerException
    at com.datadriven.test.MercuryToursTest.mercuryToursRegTest(MercuryToursTest.java:54)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:135)
    at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:598)
    at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:174)
    at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
    at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:821)
    at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:147)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1510)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:588)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:384)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:378)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:337)
    at org.testng.SuiteRunner.run(SuiteRunner.java:286)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1214)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1136)
    at org.testng.TestNG.runSuites(TestNG.java:1066)
    at org.testng.TestNG.run(TestNG.java:1034)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

FAILED: mercuryToursRegTest("Tom", "Peter", "6.54564889E8", "tom@yahoo.com", "12,new street", "downtown", "Mumbai", "Maharashtra", "400016.0", "INDIA")
java.lang.NullPointerException
    at com.datadriven.test.MercuryToursTest.mercuryToursRegTest(MercuryToursTest.java:54)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:135)
    at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:598)
    at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:174)
    at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
    at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:821)
    at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:147)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1510)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:588)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:384)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:378)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:337)
    at org.testng.SuiteRunner.run(SuiteRunner.java:286)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1214)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1136)
    at org.testng.TestNG.runSuites(TestNG.java:1066)
    at org.testng.TestNG.run(TestNG.java:1034)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

FAILED: mercuryToursRegTest("Tom", "Peter", "6.54564889E8", "tom@yahoo.com", "12,new street", "downtown", "Mumbai", "Maharashtra", "400016.0", "INDIA")
java.lang.NullPointerException
    at com.datadriven.test.MercuryToursTest.mercuryToursRegTest(MercuryToursTest.java:54)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:135)
    at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:598)
    at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:174)
    at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
    at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:821)
    at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:147)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1510)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:588)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:384)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:378)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:337)
    at org.testng.SuiteRunner.run(SuiteRunner.java:286)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1214)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1136)
    at org.testng.TestNG.runSuites(TestNG.java:1066)
    at org.testng.TestNG.run(TestNG.java:1034)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

FAILED: mercuryToursRegTest("Tom", "Peter", "6.54564889E8", "tom@yahoo.com", "12,new street", "downtown", "Mumbai", "Maharashtra", "400016.0", "INDIA")
java.lang.NullPointerException
    at com.datadriven.test.MercuryToursTest.mercuryToursRegTest(MercuryToursTest.java:54)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:135)
    at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:598)
    at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:174)
    at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
    at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:821)
    at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:147)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1510)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:588)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:384)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:378)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:337)
    at org.testng.SuiteRunner.run(SuiteRunner.java:286)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1214)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1136)
    at org.testng.TestNG.runSuites(TestNG.java:1066)
    at org.testng.TestNG.run(TestNG.java:1034)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)

FAILED: mercuryToursRegTest("Tom", "Peter", "6.54564889E8", "tom@yahoo.com", "12,new street", "downtown", "Mumbai", "Maharashtra", "400016.0", "INDIA")
java.lang.NullPointerException
    at com.datadriven.test.MercuryToursTest.mercuryToursRegTest(MercuryToursTest.java:54)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:135)
    at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:598)
    at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:174)
    at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
    at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:821)
    at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:147)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1510)
    at org.testng.TestRunner.privateRun(TestRunner.java:767)
    at org.testng.TestRunner.run(TestRunner.java:588)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:384)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:378)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:337)
    at org.testng.SuiteRunner.run(SuiteRunner.java:286)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1214)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1136)
    at org.testng.TestNG.runSuites(TestNG.java:1066)
    at org.testng.TestNG.run(TestNG.java:1034)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)


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


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

Не могли бы вы помочь мне решить эту проблему.

1 Ответ

1 голос
/ 06 апреля 2020

В своем классе MercuryToursTest вы определили public WebDriver driver; на глобальном уровне, а затем вы снова определяете и создаете экземпляр другого WebDriver driver в методе setUp(), из-за которого глобальный driver остается null и из-за этого вы получаете NPE в своем методе tearDown().
Вам необходимо внести одну строчку в метод setUp(), и это сработает.

Ваш метод setUp() должен выглядеть следующим образом:

@BeforeTest
public void setUp() {
    System.setProperty("webdriver.chrome.driver","F:\\Drivers\\chromedriver_win32\\chromedriver.exe");

    //Instantiate the global driver here
    driver = new ChromeDriver();
    driver.get("http://newtours.demoaut.com/");

    driver.findElement(By.linkText("REGISTER")).click();
    driver.manage().window().maximize();
    driver.manage().deleteAllCookies();

    driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...