question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Null pointer exception while running testng.xml with groups tag

See original GitHub issue
package com.qa.tc;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class GroupTest {
	
		WebDriver driver;

		@BeforeMethod
		public void setUp()
		{
	      System.setProperty("webdriver.chrome.driver", "D:\\Girish\\chromedriver_win32\\chromedriver.exe");
			
			driver = new ChromeDriver();
			driver.get("https://opensource-demo.orangehrmlive.com/");
			
			driver.manage().window().maximize();
			driver.manage().deleteAllCookies();
			driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);	
		}
		
	  @Test(groups= {"Smoke"})
	  public void signIntest() 
	  {
		  driver.findElement(By.xpath("//input[@id='txtUsername']")).sendKeys("Admin");
		  driver.findElement(By.xpath("//input[@id='txtPassword']")).sendKeys("admin123");
		  driver.findElement(By.xpath("//input[@id='btnLogin']")).click();
		  System.out.println(driver.getTitle());
		  
	  }
	  
	  @AfterMethod
		 public void tearDown()
		 {
		    driver.close();
	  }
}

Testng xml as below

<?xml version = "1.0" encoding = "UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name = "Sample test Suite">
   <test name = "Sample test">   
   <groups>
     <run>
       <include name = "Smoke" />
     </run>
   </groups>
      <classes>
         <class name = "com.qa.tc.GroupTest" />
      </classes>
   </test>
</suite>

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
krmahadevancommented, Oct 7, 2019

The second sample you shared doesn’t have anything that can trigger your NPE.

The original sample that you shared, triggers an NPE because of a problem in your code.

Your WebDriver instantiation is being done via the @BeforeMethod annotation. But you are choosing to run tests via groups called Smoke to which you haven’t added your @BeforeMethod method and you haven’t added the alwaysRun=true attribute also. So your @BeforeMethod is not getting executed.

To fix the problem, either add alwaysRun=true or add Smoke group to your @BeforeMethod

0reactions
GirishBaliga123commented, Oct 7, 2019

Thanks @krmahadevan It worked when used alwaysRun=true attribute in @BeforeMethod annotation

Read more comments on GitHub >

github_iconTop Results From Across the Web

NPE error when running TestNg test groups using eclipse
I am trying to run a Test group in TestNG by right clicking on TestNG.xml as a test suite and get below Error....
Read more >
Failed Test due to NullPointerException - Google Groups
I was wondering why group "init" is nonexistent. I defined it in my test class and testng.xml. Can TestNG read groups and methods...
Read more >
Are You Getting NullPointerException For Second TestNG ...
I encounter Null pointer exception when using @BeforeTest, @BeforeMethod and trying to run two classes from testng.xml.
Read more >
Documentation - TestNG
You can also define new groups inside testng.xml and specify additional details in attributes, such as whether to run the tests in parallel,...
Read more >
TestNG.java.lang.NullPointerException running second @Test
It seems like you never apply the PageFactory on this class. Therefore, when trying the line profile.click() the variable is null (because never ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found