In TestNG, context.getAllTestMethods() returns an array of testcases from that test class n not just one test. | Selenium Forum
U
Urvashi Mehta Posted on 20/01/2023
Problem: In the last video out of the four videos in the TestNG live project link in Selenium training videos,"Testng Raw Project, Running of tests, Report generation”,

"Passing parameters into test, Dependencies between tests and groups,Mailing Reports"

In the above video, you answer three questions that a student underwent in the interview. One of the question was as to how to divide the test cases into 50 percent running on Chrome and 50% on IE/firefox…
The way you suggested was to declare two parameters in the testng.xml with the same name as the @Test group name.  Then when you retrieve context.getAllTestMethods(), it will retrieve one method only which sounds correct as BeforeMethod is called solely before each testcase. But when there are more than one testcase in a test class, it does not have just one method in the array received by calling con.getAllTestMethod(). So when I try  con.getAllTestMethod()[0] it always reads just the first testcase and its group names. I am not being able to do it for other testcases. 
Is there something that my code is doing incorrectly?Is there any other way to run half the tests on different browser each?
 

package testbase;

import java.util.Arrays;

import org.testng.ITestContext;
import org.testng.ITestNGMethod;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;


import com.aventstack.extentreports.ExtentReports;
import com.aventstack.extentreports.ExtentTest;
//cmd shift o to bring all imports
//ctrl spacebar is for command completion
public class TestBase {
public ExtentTest test;
public ExtentReports report;//put its dependency in pom.xml
public String browser="";
//WebDriver driver;
public String[] groupnms;

@BeforeMethod(alwaysRun = true)
public void init(ITestContext con) {
System.out.println("In @BeforeMethod");
groupnms = con.getAllTestMethods()[0].getGroups() ;//this always gives only first test method from the test class
System.out.println("Test at [0]is ---> "+con.getAllTestMethods()[0]);
/*
* for( ITestNGMethod a:con.getAllTestMethods()) {
* System.out.println(a.toString());//this lists all testcases in that test
* class //a.getGroups() }
*/

//System.out.println(Arrays.asList(groupnms));//this list groupnames of the testcase retrieved by context variable

for(int i=0;i<groupnms.length;i++) {
if(groupnms[i].startsWith("browsergroup")) {
browser = (String)groupnms[i];

break; }//if

}
browser = con.getCurrentXmlTest().getParameter(browser);
//System.out.println("Browser in @Before : " +browser);
// if(browser.equals("chrome"))
// driver = new ChromeDriver();
// else
// driver = new FirefoxDriver();
//driver.get("http://www.google.com");
//driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
//driver.manage().window().maximize();
}
@AfterMethod
public void tearDown() {
System.out.println("In @AfterMethod");
//browser = "";
//driver.quit();
}
}

 

//Test1

package suitea;

 

import java.util.Arrays;

 

import org.testng.ITestResult;

import org.testng.annotations.Parameters;

import org.testng.annotations.Test;

 

import testbase.TestBase;

 

import org.testng.annotations.Test;

//In this test class I am not using @Parameters to read the right value of parameters

//since I am extracting them in @Beforemethod with the con.getCurrentXMLTest().

public class Test1 extends TestBase{

 

//@Parameters("browsergroupA")

@Test(groups= {"sanity","browsergroupA"})

public void testA1() { //public void testA1(String browser) {

System.out.print("In testA1 : "+ Arrays.asList(groupnms));

System.out.println("Browser : "+browser);

}

//@Parameters("browsergroupA")

@Test(groups= {"smoke","browsergroupA"})

public void testA2() { //public void testA2(String browser) {

System.out.print("In testA2 : "+ Arrays.asList(groupnms));

System.out.println("Browser : "+browser);

 

}

//@Parameters("browsergroupB")

@Test(groups= {"smoke","browsergroupB"})

public void testA3() { //public void testA3(String browser) {

System.out.print("In testA3 : "+ Arrays.asList(groupnms));

System.out.println("Browser : "+browser);

}

}

//Test2

package suitea;

 

import org.testng.ITestResult;

import org.testng.annotations.Parameters;

import org.testng.annotations.Test;

 

import testbase.TestBase;

 

import org.testng.annotations.Test;

/*

Here since I am getting the parameter value by passing each testcase with the right parameter in @Parmeters annotation

and passing String browser argument in the testcase,

I am getting the right value for that parameter although the con.getAllTestMethods() does not get the right test method each time

and hence the group names retrieved are also not the right one n hence the browser name retrieved is also incorrect.

* */

 

public class Test2 extends TestBase{

@Parameters("browsergroupB")

@Test(groups= {"sanity", "browsergroupB"})

public void testB(String browser,ITestResult res) {

System.out.print("testB ");

System.out.println(" Browser : "+browser);

}

@Parameters("browsergroupB")

@Test(groups= {"sanity","browsergroupB"})

public void testB1(String browser) {

System.out.print("testB1 ");

System.out.println(" Browser : "+browser);

}

@Parameters("browsergroupB")

@Test(groups= {"smoke","browsergroupB"})

public void testB2(String browser) {

System.out.print("testB2 ");

System.out.println(" Browser : "+browser);

}

@Parameters("browsergroupA")

@Test(groups= {"smoke","browsergroupA"})

public void testB3(String browser) {

System.out.print("testB3 ");

System.out.println(" Browser : "+browser);

}

}

//testng.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">

<suite name="Suite">

<parameter name= "browsergroupA" value = "chrome"/>

<parameter name= "browsergroupB" value = "firefox"/>

<test name="Test1">

 

<classes>

<class name="suitea.Test1"/>

</classes>

</test>

<test name="Test2">

 

<classes>

<class name="suitea.Test2"/>

</classes>

</test>

 

</suite> <!-- Suite -->