TestNG : Grouping doesn’t execute before and after method annotations?

Answered

This is my class containing the tests of groups P0 and P1

 

public class prct1 {
WebDriver driver;
@Test(groups = {“P1”})
public void f() {
// driver.findElement(by)
System.out.println(“Just executed”);
}

@Test (groups = {“P0”, “apam”})
public void gogle(){
driver.get(“https://www.sci.gov.in”);

driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
System.out.println(“Executed a P0 case”);
}
@BeforeMethod
public void beforeMethod() {
System.setProperty(“webdriver.chrome.driver”,
“D:\\Disk_E\\Softwares\\Selenium\\Drivers\\chromedriver.exe”);
driver = new ChromeDriver();
driver.get(“http://doordarshan.gov.in”);
}

 

 

And this is my testng xml

 

<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE suite SYSTEM “http://testng.org/testng-1.0.dtd”>
<suite name=”Suite gj”>
<!– <suite name=”Suite” parallel=”classes” thread-count=”2″> –>
<test name=”BOF_Chro me”>
<groups>
<run>
<include name =”P0″></include>
</run>
</groups>
<classes>
<class name=”testNG.practise.prct1″ />
</classes>
</test>
</suite>

 

So,

when i execute test suite, it is executing P0 case directly, without executing the before method annotation

 

so getting a nullpointer exception at P0 test’s first line, as driver is not created

 

Default Asked on April 2, 2019 in Selenium WebDriver.
Add Comment
  • 1 Answer(s)
    Best answer

    By giving the alwaysRun=true for the other annotations it worked without any Error.

    @Test (groups = {“P0”, “apam”})
    public void gogle(){
    driver.get(“https://www.sci.gov.in”);

    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    System.out.println(“Executed a P0 case”);
    }
    @BeforeMethod (alwaysRun = true)
    public void beforeMethod() {
    System.setProperty(“webdriver.chrome.driver”,
    “D:\\Disk_E\\Softwares\\Selenium\\Drivers\\chromedriver.exe”);
    driver = new ChromeDriver();
    driver.get(“http://doordarshan.gov.in”);
    }

    Default Answered on April 2, 2019.
    Add Comment
  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.