Folksm I'm using TestNG 7.5 and Selenium 4.2.2, with IntelliJ and Java 11.

I've got all my bits imported and my tests will run individually, but I'm trying to get my setup and cleanup methods to work with the before and after annotations. Here's a code sample:

   public static class RunTest {
      WebDriver driver;
      WaitUtils waitUtils;
      Actions actions;
      StateUtils stateUtils;

      public RunTest(WebDriver givenDriver){
         this.driver = givenDriver;
         this.actions = new Actions(givenDriver);
         this.waitUtils = new WaitUtils(givenDriver);
         this.stateUtils = new StateUtils(givenDriver);

      @BeforeMethod(alwaysRun = true)
      public void login(){
         boolean firstLoginPageFound = true;

         InteractionUtils interactionUtils = new InteractionUtils(driver);
            interactionUtils.clickOnElementByXpath("//div[@class='nav-bb-right']/a[text()='Your Account']");
         } catch  (NoSuchElementException e){
            firstLoginPageFound = false;


         byte[] decodedBytes = Base64.getDecoder().decode("");

         waitUtils.waitForVisibilityOfLocator("//input[@id='ap_email']").sendKeys(new String(decodedBytes));


         decodedBytes = Base64.getDecoder().decode("");
         waitUtils.waitForVisibilityOfLocator("//input[@id='ap_password']").sendKeys(new String(decodedBytes));


      public void checkLandingPage(){
         Assert.assertTrue(stateUtils.verifyElementIsVisible("//div[@id='nav-xshop']//a[contains(@class, 'nav-a') and text()='Buy Again']"), "Buy again button is missing");

      @AfterMethod(alwaysRun = true)
      public void cleanUp(){

If I run something like:

  RunTest test = new RunTest(new SetUpUtils().getDriver());

the checkLandingPage method will run, and fail, but the login and cleanup methods won't run. I've tried a few variations on the annotations, but no luck so far.

The imports used are :

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.interactions.Interaction;
import org.openqa.selenium.support.ui.*;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.time.Duration;
import java.util.Base64;
  • can you show the annotation being imported , make sure you are importing from correct class
    – PDHide
    Commented Jun 21, 2022 at 9:44
  • I have added it to question , also what is this RunTest? is it a testng method
    – PDHide
    Commented Jun 21, 2022 at 14:42
  • @PDHide It's not a testng method, just a class I created to house several methods that run tests.
    – shaidyn
    Commented Jun 21, 2022 at 15:30

1 Answer 1



  1. Not sure how Java is allowing you to have a top level class be static.

  2. I would try to run your suite using a TestNG suite.xml file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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