2016-09-17 7 views
1

私はSeleniumを初めて使用しており、ITestContextグループからパラメータを取得するコードの下に書いてあります。TestNGでのメソッド呼び出しのNullPointerExceptionの取得

コード:

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

しかし、私は、例外下に取得しています: org.testng.internal.MethodInvocationHelper.invokeDataProvider(MethodInvocationHelper.javaでtestMethodAのjava.lang.NullPointerException:

がスキップ: 151) at org.testng.internal.Parameters.handleParameters(Parameters.java:430) at org.testng.internal.Invoker.handleParameters(Invoker.java:1243)(Invoker.java:992)at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1082)at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker。 Javaの:129) org.testng.TestRunner.run(テストランナーでorg.testng.TestRunner.privateRunで org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112) (TestRunner.java:778)で.java:632) org.testng.SuiteRunner.privateRun(SuiteRunner.javaで org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)で org.testng.SuiteRunner.runTest(SuiteRunner.java:366)で:319) org.testng.SuiteRunner.run(SuiteRunner.java:268)at org。 test35.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) org.testng.TestNG.runSuitesSequentially(TestNG.java:1225) org.testngにあります。 TestNG.runSuitesLocally(TestNG.java:1150) org.testng.TestNG.runSuites(TestNG.java:1075) org.testng.TestNG.run(TestNG.java:1047) org.testng.remote。 org.testng.remote.RemoteTestNG.mainでorg.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:137) でAbstractRemoteTestNG.run(AbstractRemoteTestNG.java:126) (RemoteTestNG.java:58)

スキップ:testMethodBでjava.lang.NullPointerException ....

+0

その私のために、ファイルを実行している..あなたは私たちに伝えることができた時にあなたが取得ラインNPE? –

+0

テストはスキップされ、値はGoogle検索ボックスに送信されません。任意の行番号/特定の例外を取得していません –

答えて

2

グループを使用している場合は、これらのグループを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; 
     } 
    } 
+0

ありがとうございました。しかし、今私は別の例外を取得しています:testMethodB org.testng.internal.reflect.MethodMatcherException: データプロバイダの不一致 メソッド:testMethodB([Parameter {index = 0、type = java.lang.String、declaredAnnotations = []}]] ) 引数:[(java.lang.String)Aakash、(java.lang.String)India] ... 20個のスタックフレームを削除しました –

+0

グループを使用しないコードで回答を更新しました。私は、{{Aacush "、" India "}、 {" Aayush "、" US "}、 {" Raveena "、" UK " } } '。あなたは、それがグループで動作するように少しXMLファイルを微調整する必要があります。 –

関連する問題