Appium: Running entire test suite, first test passes, the rest of the feature files fails

Hi All,

I hope you are well.

I have a maven project currently running on Appium, Cucumber and I use Junit.

When I run the tests individually, they work as they should. For example, If I run feature 1 alone, it works as normal. I noticed that when I run the entire test suite, Only the first one passes and the rest of the tests fails Giving an error such as: cannot locate element.

Note: The tests are independent, so they don’t depend on another test or state

I have checked a couple of resources online, but haven’t seen any direct solution.

I have added some code snippets below.

This is my DriverManagerTest Class

public class DriverManagerTest {

   private AppiumDriver driver;


    public AppiumDriver getDriver() {
        if (driver == null) {
            throw new IllegalStateException("Driver not initialized. Call androidLaunch() or iosLaunch() first.");
        }
        return driver;
    }

public void iosLaunch () throws MalformedURLException, URISyntaxException {
        XCUITestOptions options = new XCUITestOptions();
        options.setDeviceName("iPhone 15");
        options.setPlatformVersion("17.2");
        options.setApp(System.getProperty("user.dir")+"/src/test/resources/app/App.zip");
        options.setCapability("automationName", "XCUITest");
        options.setCapability("autoAcceptAlerts", true);  // Automatically accept system alerts
        driver = new IOSDriver(new URI("http://127.0.0.1:4723").toURL(), options);

    }

public void quitDriver() {
        if (driver != null) {
            driver.quit();
        }
    }

This is my Step Definition for the Wishlist below:

public class Wishlist {

    AppiumDriver driver;

    DriverManagerTest driverManagerTest;

    WishlistPage wishlistPage;


    @Before
        public void setupDriver() throws MalformedURLException, URISyntaxException {

        driverManagerTest = new DriverManagerTest();

       // Set up the iOS driver
        driverManagerTest.iosLaunch();
        driver = driverManagerTest.getDriver();
        wishlistPage = new WishlistPage(driver);
    }

    @After
    public void tearDownDriver() {
        driverManagerTest.quitDriver();
    }
// i have some steps below with @When etc...

This is my step definition for Checkout

public class Checkout {
    AppiumDriver driver;

    DriverManagerTest driverManagerTest;

    CheckoutPage checkoutPage;

    @Before
    public void setupDriver() throws MalformedURLException, URISyntaxException {

        driverManagerTest = new DriverManagerTest();

        // Set up the iOS driver
        driverManagerTest.iosLaunch();
        driver = driverManagerTest.getDriver();
        checkoutPage = new CheckoutPage(driver);
    }

    @After
    public void tearDownDriver() {
        driverManagerTest.quitDriver();
    }

Note: I use page object model. Simulator Device: iOS 15 v17.2

Any idea how to make the entire test suite run? When I run them individually, they work normal, but running the entire test suite, only the first test / feature file passes, the rest fails.

Thank you.

start from

  • add BaseTest class. The only place where you start and quit driver
  • add logs into your functions
public class BaseTest {
    protected AppiumDriver driver;
    DriverManagerTest driverManagerTest;
    
    @Before
    public void setupDriver() {
        System.out.println("setupDriver");
        driverManagerTest = new DriverManagerTest();

        // Set up the iOS driver
        driverManagerTest.iosLaunch();
        driver = driverManagerTest.getDriver();
    }

    @After
    public void tearDownDriver() {
        System.out.println("tearDownDriver");
        driverManagerTest.quitDriver();
    }
}

// now lets write some TestOne that extends BaseTest class
public class TestOne extends BaseTest {
    WishlistPage wishlistPage = new WishlistPage(driver);

    wishlistPage
            .setInput("my wish")
            .tapExecuteButton();
}

// and TestTwo
public class TestTwo extends BaseTest {
    CheckoutPage checkoutPage = new CheckoutPage(driver);

    checkoutPage
            .tapCheckButton()
            .tapContinueButton();
}

now you can create Junit XML file to run TestOne and TestTwo tests

Hi @Aleksei,

I have created the BaseTest class as you mentioned:

public class BaseTest {
    protected AppiumDriver driver;
    DriverManagerTest driverManagerTest;

    @Before
    public void setupDriver() throws MalformedURLException, URISyntaxException {
        System.out.println("setupDriver");
        driverManagerTest = new DriverManagerTest();

        // Set up the iOS driver
        driverManagerTest.iosLaunch();
        driver = driverManagerTest.getDriver();
    }

    @After
    public void tearDownDriver() {
        System.out.println("tearDownDriver");
        driverManagerTest.quitDriver();
    }
}

This is my Wishlist Step Definition

public class Wishlist extends BaseTest {
        WishlistPage wishlistPage = new WishlistPage(driver);
        
@Then("the product is added to wishlist")
    public void productAddedToWishlist(){
        wishlistPage.wishListNavIcon();
    }
}

This is my Checkout Step definition

public class Checkout extends BaseTest {
     CheckoutPage checkoutPage = new CheckoutPage(driver);
@When("the user selects a product on the homepage")
    public void userSelectProduct(){
        checkoutPage.clickaProductOnHomePage();
    }
}

But i get this error

org.openqa.selenium.WebDriverException: java.lang.NullPointerException
Driver info: driver.version: unknown

I run the test using a cucumber runner class

@RunWith(Cucumber.class)
@CucumberOptions(features = "src/test/resources/features", glue = "stepDefinitions", tags = "@runThisFeature" )
public class Runner {

}

I think the ‘checkoutPage.clickaProductOnHomePage’ might be returning null

there are nice examples how you need to write with :


You should create something like Steps class that extends our BaseTest.

PS I never used Cucumber

Thank you for sharing these articles.