1
votes

I am new to Selenium and have written below Code to take parameters from ITestContext group.

Code :

 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;
    }
}

But I am getting Below exceptions:

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 ....

1
Its Running file for me.. Can you tell us at which line you getting NPE?Deendayal Garg
Tests are getting skipped and values are not being sent to Google search box. Not getting any line number/specific exceptionsAakash Goyal

1 Answers

2
votes

When you are using groups, you need to define those groups in XML file.

<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>

Updated Code:

    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;
        }
    }