2016-09-17 66 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; 
    } 
} 

但我得到下面的异常:

跳过:testMethodA显示java.lang.NullPointerException在 org.testng.internal.MethodInvocationHelper.invokeDataProvider(MethodInvocationHelper.java:在org.testng.internal.Invoker.handleParameters(Invoker.java:1243)org.testng.internal.Parameters.handleParameters(Parameters.java:430) 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) 在 org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112) 在org.testng.TestRunner.privateRun(TestRunner.java:778)在 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) org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)at org.testng.TestNG.runSuitesSequentially(TestNG.java:1225)at org.testng。 (TestNG.java:1150) org.testng.TestNG.runSuites(TestNG.java:1075)at org.testng.TestNG.run(TestNG.java:1047)at org.testng.remote。 AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:126) 在org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:137) 在org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:58)

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

+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([参数{index = 0,type = java.lang.String,declaredAnnotations = []}] ) 参数:[(java.lang.String)Aakash,(java.lang.String)India] ...删除了20个堆栈帧 –

+0

我已更新了不使用组的代码的答案。我认为它失败的原因是因为它试图运行带有“Aakash”,“India”的 {“Aayush”,“US”}, {“Raveena”,“UK” ' }'。您需要稍微调整xml文件以使其适用于组。 –

相关问题