为 TestNG 中的方法调用获取 NullPointerException

Getting NullPointerException for method Invocation in TestNG

我是 Selenium 的新手,在下面编写了代码以从 ITestContext 组获取参数。

代码:

 import java.util.concurrent.TimeUnit;
 import org.openqa.selenium.By;
 import org.openqa.selenium.WebDriver;
 import org.openqa.selenium.WebElement;
 import org.openqa.selenium.chrome.ChromeDriver;
 import org.openqa.selenium.interactions.Actions;
 import org.testng.ITestContext;
 import org.testng.annotations.BeforeTest;
 import org.testng.annotations.DataProvider;
 import org.testng.annotations.Test;

public class ITestGroupism {
WebDriver driver;

  @BeforeTest(groups={"A","B"})
  public void setup()
  {
      System.setProperty("webdriver.chrome.driver", "E:\Automation Jars\chromedriver_win32\chromedriver.exe");
      driver = new ChromeDriver();  
      driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
      driver.get("https://www.google.co.in");
  }     

  @Test(dataProvider="SearchProvider", groups="A")
  public void testMethodA(String author, String searchKey) throws InterruptedException 
  { 
      WebElement searchText = driver.findElement(By.id("sb_ifc0"));
      Actions actions = new Actions(driver);
      actions.moveToElement(searchText);
      actions.click();
      actions.sendKeys(searchKey);
      actions.build().perform();
      System.out.println("Welcome ->"+author+" Your search key is "+searchKey);
      driver.navigate().back();
      driver.navigate().forward();
      System.out.println("thread will sleep now");
      Thread.sleep(2000);     
      }

     @Test(dataProvider="SearchProvider", groups="B")
     public void testMethodB(String searchKey) throws InterruptedException
     {
         WebElement searchText = driver.findElement(By.id("sb_ifc0"));
          Actions actions = new Actions(driver);
          actions.moveToElement(searchText);
          actions.click();
          actions.sendKeys(searchKey);
          actions.build().perform();
          //searchText.sendKeys(searchKey);
          System.out.println("Welcome Professor"+"Your search key is "+searchKey);
          driver.navigate().back();
          driver.navigate().forward();
          System.out.println("thread will sleep now");
          Thread.sleep(2000);              
     }

     @DataProvider(name="SearchProvider")
     public Object[][] ff(ITestContext c)
     {
     Object[][] groupArray =null;
     for(String group : c.getIncludedGroups())
     {
        if(group.equalsIgnoreCase("A"))
        {
            groupArray = new Object[][]
            {
            {"Aakash", "India"},
             {"Aayush", "US"},
             {"Raveena", "UK"}               
            };
            break;
         }else if(group.equalsIgnoreCase("B"))
         {
            groupArray= new Object[][]
               {
                 {"Canada"},
                 {"New Zealand"},
                 {"Russia"}              
               };
             }break;
     }
     return groupArray;
    }
}

但我遇到以下异常:

SKIPPED: testMethodA java.lang.NullPointerException at org.testng.internal.MethodInvocationHelper.invokeDataProvider(MethodInvocationHelper.java:151) at org.testng.internal.Parameters.handleParameters(Parameters.java:430) at org.testng.internal.Invoker.handleParameters(Invoker.java:1243) at org.testng.internal.Invoker.createParameters(Invoker.java:992) at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1082) at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129) at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112) at org.testng.TestRunner.privateRun(TestRunner.java:778) at org.testng.TestRunner.run(TestRunner.java:632) at org.testng.SuiteRunner.runTest(SuiteRunner.java:366) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319) at org.testng.SuiteRunner.run(SuiteRunner.java:268) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1225) at org.testng.TestNG.runSuitesLocally(TestNG.java:1150) at org.testng.TestNG.runSuites(TestNG.java:1075) at org.testng.TestNG.run(TestNG.java:1047) at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:126) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:137) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:58)

SKIPPED: testMethodB java.lang.NullPointerException at ....

当您使用组时,您需要在 XML 文件中定义这些组。

<suite name="Group Suite" verbose="1">
<test name="Test">
<groups>
  <run>
    <include name="A" />
    <include name="B" />
  </run>
</groups>
<classes>
  <class name="fully.qualied.package.ITestGroupism" />
 </classes>
 </test>
</suite>

更新代码:

    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.interactions.Actions;
    import org.testng.annotations.BeforeTest;
    import org.testng.annotations.DataProvider;
    import org.testng.annotations.Test;

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

    public class ITestGroupism {
        WebDriver driver;

        @BeforeTest(groups = {"A", "B"})
        public void setup() {
            System.setProperty("webdriver.chrome.driver", new File("src/test/resources/driver/chromedriver")
                    .getAbsolutePath());
            driver = new ChromeDriver();
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            driver.get("https://www.google.co.in");
        }

        @Test(dataProvider = "SearchProvider", alwaysRun = true, groups = "A")
        public void testMethodA(String author, String searchKey) throws InterruptedException {
            WebElement searchText = driver.findElement(By.id("sb_ifc0"));
            Actions actions = new Actions(driver);
            actions.moveToElement(searchText);
            actions.click();
            actions.sendKeys(searchKey);
            actions.build().perform();
            System.out.println("Welcome ->" + author + " Your search key is " + searchKey);
            driver.navigate().back();
            driver.navigate().forward();
            System.out.println("thread will sleep now");
            Thread.sleep(2000);
        }

        @Test(dataProvider = "SearchProvider", alwaysRun = true, groups = "A")
        public void testMethodB(String searchKey) throws InterruptedException {
            WebElement searchText = driver.findElement(By.id("sb_ifc0"));
            Actions actions = new Actions(driver);
            actions.moveToElement(searchText);
            actions.click();
            actions.sendKeys(searchKey);
            actions.build().perform();
            //searchText.sendKeys(searchKey);
            System.out.println("Welcome Professor" + "Your search key is " + searchKey);
            driver.navigate().back();
            driver.navigate().forward();
            System.out.println("thread will sleep now");
            Thread.sleep(2000);
        }

        @DataProvider(name = "SearchProvider")
        public Object[][] ff(Method method) {
            Object[][] groupArray = null;
            if (method.getName().equalsIgnoreCase("testMethodA")) {
                groupArray = new Object[][]
                        {
                                {"Aakash", "India"},
                                {"Aayush", "US"},
                                {"Raveena", "UK"}
                        };
            } else if (method.getName().equalsIgnoreCase("testMethodB")) {
                groupArray = new Object[][]
                        {
                                {"Canada"},
                                {"New Zealand"},
                                {"Russia"}
                        };
            }
            return groupArray;
        }
    }