0

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

         driver.navigate().to("http://www.amazon.ca");
         InteractionUtils interactionUtils = new InteractionUtils(driver);
         try{
            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("//input[@id='ap_email']").sendKeys(new String(decodedBytes));

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

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

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

      @Test
      public void checkLandingPage(){
         System.out.println("REST++++++++++++");
         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(){
         System.out.println("BEST++++++++++++");
         driver.close();
      }
   }

If I run something like:

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

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;
3
  • can you show the annotation being imported , make sure you are importing from correct class
    – PDHide
    Jun 21, 2022 at 9:44
  • I have added it to question , also what is this RunTest? is it a testng method
    – PDHide
    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
    Jun 21, 2022 at 15:30

1 Answer 1

0

Suggestions:

  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.