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;