Appium Parallel Testing Issue

Hi,

I’m building a java-appium project for cross-platform (android/iOs) testing and now I’m trying to run parallel testing on 2 android devices simultanously to running “Login tests” for an Android application example in parallel in both devices at same time.

I’m using Testng and this is my runner.xml file:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="MobileAutomation Framework" parallel="tests">



    <test name="Login Test Xiaomi Mi9SE">

        <parameter name="platformName" value="ANDROID"></parameter>
        <parameter name="model" value="XIAOMIMI9SE"></parameter>

        <classes>
            <class name="Test.Login"/>
        </classes>

    </test>

    <test name="Login Test Huawei Y7">

        <parameter name="platformName" value="ANDROID"></parameter>
        <parameter name="model" value="HUAWEIY7"></parameter>

        <classes>
            <class name="Test.Login"/>
        </classes>
    </test>

</suite>

This is my “BaseTestClass” where I have defined the setUp and tearDown:

public class BaseTestClass {

    public AppiumDriver driver;

    @BeforeTest
    public void setUpAppium() throws IOException {
        killExistingAppiumProcess();
        if (AppiumServer.appium == null || !AppiumServer.appium.isRunning()) {
            AppiumServer.start();
        }
    }
    @BeforeMethod
    @Parameters({"platformName", "model"})
    public void setUp(@Optional String platformName, @Optional String model) throws Exception {
        if (platformName.equalsIgnoreCase(PlatformType.ANDROID.toString())) {
            driver = new AndroidDriverBuilder().setupDriver(model);

        } else if (platformName.equalsIgnoreCase(PlatformType.IOS.toString())) {
            driver = new IOSDriverBuilder().setupDriver(model);

        }
    }

    @AfterMethod
    public void tearDown() {
        driver.quit();
    }

    @AfterTest
    private void killExistingAppiumProcess() throws IOException {
        Runtime.getRuntime().exec("killall node");
    }
}

And this is my “AndroidBuilder” for setUp the capabilities and read a “AndroidDeviceModel” that is only a device.json with some information like systemPort, deviceName… etc

public class AndroidDriverBuilder extends DeviceConfig {

    AndroidDriver driver;

    public AndroidDriver setupDriver(String model) throws IOException {

        DesiredCapabilities androidCapabilities = new DesiredCapabilities();

        AndroidDeviceModel device = readAndroidDeviceConfig().getAndroidDeviceByName(model);
        setExecutionPlatform(model);

        File classpathRoot = new File(System.getProperty("user.dir"));
        File appDir = new File(classpathRoot, "/app/Android");
        File app = new File (appDir, "Example.apk");


        androidCapabilities.setCapability(MobileCapabilityType.DEVICE_NAME, device.getDeviceName());
        androidCapabilities.setCapability("deviceId", device.getDeviceId());
        androidCapabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, device.getPlatformName());
        androidCapabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, device.getPlatformVersion());
        androidCapabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, device.getAutomationName());
        androidCapabilities.setCapability(MobileCapabilityType.NO_RESET, device.isReset());
        androidCapabilities.setCapability("app", app.getAbsolutePath());
        androidCapabilities.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, device.getPackageName());
        androidCapabilities.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, device.getActivity());
        androidCapabilities.setCapability("systemPort", device.getSystemPort());

        androidCapabilities.setCapability("sendKeyStrategy","setValue");

        driver = new AndroidDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), androidCapabilities);
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        return driver;
    }
}

The problem that im having is that when I’m running the project, all seems to go fine until sometimes randomly this error appears:

org.openqa.selenium.remote.SessionNotFoundException: A session is either terminated or not started (WARNING: The server did not provide any stacktrace information)

Im suspecting that my “teardown()” method is quitting the other device driver but to be honest I have no idea what could be happening here.

Any help please?

@Javi_Crcls start from:

public AppiumDriver driver;
->
public ThreadLocal<AppiumDriver > driver = new ThreadLocal<>;