getting error java.lang.NullPointerException in data driven testing using @DataProvider in testng

huangapple go评论85阅读模式
英文:

getting error java.lang.NullPointerException in data driven testing using @DataProvider in testng

问题

以下是您提供的程序的翻译部分:

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() {
        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.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();
    }
}
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;
    }
}

在您提供的代码中,您的主要问题是 java.lang.NullPointerException 错误。这个错误发生在 tearDown 方法和 mercuryToursRegTest 方法中。您需要确保 WebDriver 对象 driver 被正确地初始化和使用。同时,确保 TestNGTestUtil 类中的 getDataFromExcel 方法能够正常地获取数据。

英文:

I have created the below program for data driven test in testng and getting the error java.lang.NullPointerException.

This is data driven testing which is just fetching values from excel using @Dataprovider in testNG. but when i try to run this program it gives me the error 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(&quot;webdriver.chrome.driver&quot;,&quot;F:\\Drivers\\chromedriver_win32\\chromedriver.exe&quot;);
WebDriver driver = new ChromeDriver();
driver.get(&quot;http://newtours.demoaut.com/&quot;);
driver.findElement(By.linkText(&quot;REGISTER&quot;)).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&lt;Object[]&gt; getTestData(){
//in the below line we are calling directly thru class name because the method getDataFromExcel is static in nature
ArrayList&lt;Object[]&gt; testData = TestNGTestUtil.getDataFromExcel();
return testData.iterator();
}
@Test(dataProvider =&quot;getTestData&quot; )
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(&quot;REGISTER&quot;)).click();	
driver.findElement(By.xpath(&quot;//input[@name = &#39;firstName&#39;]&quot;)).sendKeys(firstName);
driver.findElement(By.xpath(&quot;//input[@name = &#39;lastName&#39;]&quot;)).sendKeys(LastName);
driver.findElement(By.xpath(&quot;//input[@name = &#39;phone&#39;]&quot;)).sendKeys(Phone);
driver.findElement(By.xpath(&quot;//input[@id = &#39;userName&#39;]&quot;)).sendKeys(Email);
driver.findElement(By.xpath(&quot;//input[@name = &#39;address1&#39;]&quot;)).sendKeys(Address1);
driver.findElement(By.xpath(&quot;//input[@name = &#39;address2&#39;]&quot;)).sendKeys(Address2);
driver.findElement(By.xpath(&quot;//input[@name = &#39;city&#39;]&quot;)).sendKeys(City);
driver.findElement(By.xpath(&quot;//input[@name = &#39;state&#39;]&quot;)).sendKeys(State);
driver.findElement(By.xpath(&quot;//input[@name = &#39;postalCode&#39;]&quot;)).sendKeys(PostalCode);
Select select = new Select(driver.findElement(By.xpath(&quot;//*[@name = &#39;country&#39;]&quot;)));
select.selectByVisibleText(Country);
}
@AfterTest
public void tearDown()  {
driver.quit();
}
}

and the TestNGTestUtil program below in another package

package com.test.utility;
import java.util.ArrayList;
import com.excel.utility.Xls_Reader;
public class TestNGTestUtil {
static Xls_Reader reader;
public static ArrayList&lt;Object[]&gt; getDataFromExcel() {
ArrayList&lt;Object[]&gt; myData = new ArrayList&lt;Object[]&gt;();
try {
reader = new Xls_Reader(&quot;F:\\Vishal_Offc Work\\Workspace\\SeleniumBasics\\src\\com\\testdata\\NewToursRegTestData.xlsx&quot;);
} catch(Exception e) {
e.printStackTrace();
}
for(int rowNum = 2 ; rowNum&lt;=reader.getRowCount(&quot;RegTestData&quot;) ; rowNum++ ) {
String firstName = reader.getCellData(&quot;RegTestData&quot;, &quot;FirstName&quot;, 2);
String LastName = reader.getCellData(&quot;RegTestData&quot;, &quot;LastName&quot;, 2);
String Phone = reader.getCellData(&quot;RegTestData&quot;, &quot;Phone&quot;, 2);
String Email = reader.getCellData(&quot;RegTestData&quot;, &quot;Email&quot;, 2);
String Address1 = reader.getCellData(&quot;RegTestData&quot;, &quot;Address1&quot;, 2);
String Address2 = reader.getCellData(&quot;RegTestData&quot;, &quot;Address2&quot;, 2);
String City = reader.getCellData(&quot;RegTestData&quot;, &quot;City&quot;, 2);
String State = reader.getCellData(&quot;RegTestData&quot;, &quot;State&quot;, 2);
String PostalCode = reader.getCellData(&quot;RegTestData&quot;, &quot;PostalCode&quot;, 2);
String Country = reader.getCellData(&quot;RegTestData&quot;, &quot;Country&quot;, 2);
Object ob[] = {firstName,LastName,Phone,Email,Address1,Address2,City,State,
PostalCode,Country};
myData.add(ob);
}
return myData;
}
}

and below is the error in console.

[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(&quot;Tom&quot;, &quot;Peter&quot;, &quot;6.54564889E8&quot;, &quot;tom@yahoo.com&quot;, &quot;12,new street&quot;, &quot;downtown&quot;, &quot;Mumbai&quot;, &quot;Maharashtra&quot;, &quot;400016.0&quot;, &quot;INDIA&quot;)
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(&quot;Tom&quot;, &quot;Peter&quot;, &quot;6.54564889E8&quot;, &quot;tom@yahoo.com&quot;, &quot;12,new street&quot;, &quot;downtown&quot;, &quot;Mumbai&quot;, &quot;Maharashtra&quot;, &quot;400016.0&quot;, &quot;INDIA&quot;)
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(&quot;Tom&quot;, &quot;Peter&quot;, &quot;6.54564889E8&quot;, &quot;tom@yahoo.com&quot;, &quot;12,new street&quot;, &quot;downtown&quot;, &quot;Mumbai&quot;, &quot;Maharashtra&quot;, &quot;400016.0&quot;, &quot;INDIA&quot;)
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(&quot;Tom&quot;, &quot;Peter&quot;, &quot;6.54564889E8&quot;, &quot;tom@yahoo.com&quot;, &quot;12,new street&quot;, &quot;downtown&quot;, &quot;Mumbai&quot;, &quot;Maharashtra&quot;, &quot;400016.0&quot;, &quot;INDIA&quot;)
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(&quot;Tom&quot;, &quot;Peter&quot;, &quot;6.54564889E8&quot;, &quot;tom@yahoo.com&quot;, &quot;12,new street&quot;, &quot;downtown&quot;, &quot;Mumbai&quot;, &quot;Maharashtra&quot;, &quot;400016.0&quot;, &quot;INDIA&quot;)
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
===============================================

Can you please help me in resolving this.

答案1

得分: 1

在你的 MercuryToursTest 类中,你在全局层级上定义了 public WebDriver driver;,然后在 setUp() 方法中再次定义和实例化了另一个 WebDriver driver,因此全局的 driver 保持为 null,导致在 tearDown() 方法中出现了 NPE(空指针异常)。

你只需要在 setUp() 方法中进行一行改动,它就会起作用。

你的 setUp() 方法应该像这样:

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

    // 在这里实例化全局 driver
    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);
}
英文:

In your MercuryToursTest class, you have defined public WebDriver driver; at the global level and then you are again defining and instantiating another WebDriver driver in the setUp() method because of which the global driver remains null and because of which you are getting NPE in your tearDown() method.
You need to make a single line change in the setUp() method and it would work.

Your setUp() method should be like:

@BeforeTest
public void setUp() {
System.setProperty(&quot;webdriver.chrome.driver&quot;,&quot;F:\\Drivers\\chromedriver_win32\\chromedriver.exe&quot;);
//Instantiate the global driver here
driver = new ChromeDriver();
driver.get(&quot;http://newtours.demoaut.com/&quot;);
driver.findElement(By.linkText(&quot;REGISTER&quot;)).click();
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().pageLoadTimeout(40, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}

huangapple
  • 本文由 发表于 2020年4月6日 17:19:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/61056536.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定