4

I'm running Selenium tests using Java (TestNG and IntelliJ) and am currently working on my reporting framework. My test setup looks something like this:

@Test
public void testLogin(){
  myKeyword.login();
}

myKeyword is a class of keywords that I have created, with a method called login. The login keyword will look like:

public void login(){
  myPage.typeInTextBox("Username");
  myPage.typeInTextBox("Password");
  myPage.buttonClick("Login");
}

My validation is built into the methods "buttonClick" and "typeInTextBox". They will look something like:

try{
  driver.findelement(By.id("myButton")).click();
}
catch (Exception e){
  Assert.fail("Could not click on the button.");
}

What I want to know is, is it possible for the "buttonClick" method to know the name of the test that's calling it?

I want to replace the "Assert.fail..." with my own function that will create a text file that I will use to log the information I want, which will need to include the name of the test that failed (in this case "testLogin).

2
  • A stacktrace from your exception?
    – Anonymous
    Commented Nov 16, 2017 at 8:00
  • I don't have an exception. That was just a snippet from my code, I'm still trying to figure out how to pass through the test name to the method "buttonClick".
    – ChrisG29
    Commented Nov 16, 2017 at 8:09

1 Answer 1

3

One option would be to set the name of the current test somewhere, possibly to a static variable. How to do that for TestNG was described here.

public class Test { 
    @BeforeMethod
    public void handleTestMethodName(java.lang.reflect.Method method) {
        GlobalVariables.currentTestName = method.getName(); 
    }
...
}
1
  • Thanks so much, I was just looking at the same question and the answer from Dmitri. Your solution is exactly what I was looking for.
    – ChrisG29
    Commented Nov 16, 2017 at 8:35

Not the answer you're looking for? Browse other questions tagged or ask your own question.