Wednesday, March 26, 2025

My TestNG BeforeMethod and AfterMethod annotations will not be executing earlier than or after the @Check annotated methodology


Folksm I am utilizing TestNG 7.5 and Selenium 4.2.2, with IntelliJ and Java 11.

I’ve acquired all my bits imported and my checks will run individually, however I am making an attempt to get my setup and cleanup strategies to work with the earlier than and after annotations. This is a code pattern:

   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(){
         System.out.println("test_++++++++++++");
         boolean firstLoginPageFound = true;

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

         interactionUtils.clickOnElementByXpath("//span[@id='nav-link-accountList-nav-line-1']");

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

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

         interactionUtils.clickOnElementByXpath("//enter[@id='continue']");

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

         interactionUtils.clickOnElementByXpath("//enter[@id='signInSubmit']");
      }

      @Check
      public void checkLandingPage(){
         System.out.println("REST++++++++++++");
         Assert.assertTrue(stateUtils.verifyElementIsVisible("//div[@id='nav-xshop']//a[contains(@class, 'nav-a') and text()='Buy Again']"), "Purchase once more button is lacking");
      }

      @AfterMethod(alwaysRun = true)
      public void cleanUp(){
         System.out.println("BEST++++++++++++");
         driver.shut();
      }
   }

If I run one thing like:

  RunTest take a look at = new RunTest(new SetUpUtils().getDriver());
  take a look at.checkLandingPage();

the checkLandingPage methodology will run, and fail, however the login and cleanup strategies will not run. I’ve tried just a few variations on the annotations, however no luck to this point.

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.Interplay;
import org.openqa.selenium.help.ui.*;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Check;

import java.time.Period;
import java.util.Base64;

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles

PHP Code Snippets Powered By : XYZScripts.com