Afsal
Afsal

Reputation: 494

How to pass parameter in TestNG Listener method - Selenium Java

I have the below class which fails or passes based on the boolean value I provide.

public class testLogin extends Base
{
  public static String requestBody;

  @Test
  public void executeTest()
  {
    System.out.println("Log in to Salesforce.");
    salesforceLogin(false);
  }

public void salesforceLogin(boolean status)
{
  if(status)
  {
    System.out.println("Test passed.");
  }
  else
  {
    System.out.println("Test failed.");
    Assert.fail();
  }
 }
}

I am using TestNG Listener to run below method:

@Override
public void onTestSuccess(ITestResult result) 
{
   VoneRest vo = PageFactory.initElements(driver, VoneRest.class);
   System.out.println("Test Success. Updating Vone status...");
   vo.updateVonePass();  //method to update test status in Vone.
}

What I need is something like this:

@Override
public void onTestSuccess(ITestResult result, **String testID**) 
{
 VoneRest vo = PageFactory.initElements(driver, VoneRest.class);
 System.out.println("Test Success. Updating Vone status...");
 vo.updateVonePass(**testID**); 
 //method to update test status in Vone based on test id passed.
}

This program executes a TestNG test and updates corresponding Test's status in VersionOne as Passed or Failed.

Issue is that Listener method onTestSuccess() does not accept a second parameter. How can I achieve this? Or, is there a better way of doing this?

Upvotes: 1

Views: 1280

Answers (2)

Martin Ma
Martin Ma

Reputation: 150

This is what you need to do :

1 - in class with test

@Test
public void executeTest(ITestContext context){
context.setAttribute("your attribute","123");
}

2- in listener class ( on any method with interface ISuite,ITestResult etc..) e.g. this

@Override
public void onTestFailure(ITestResult result) 
{  
String yourAttribute = result.getTestContext().getAttribute("your attribute");
}

Upvotes: 0

Afsal
Afsal

Reputation: 494

Found the answer myself after connecting different ideas found on the internet. Posting for someone who might need it in the future.

You can pass parameters to Listener using ITestContext like this:

@Test
public void executeTest(ITestContext Story,ITestContext Test)

Then, in your listener class, you can reference both variables liked this:

@Override
public void onTestFailure(ITestResult result) 
{  
vo.updateVoneFail(result.getTestContext().getAttribute("testAttribute"),result.getTestContext().getAttribute("storyAttribute"));
}

Upvotes: 2

Related Questions