Skip to content

Instantly share code, notes, and snippets.

@SarahElson
Last active September 15, 2022 11:28
Complete Tutorial On Appium Parallel Testing [With Examples]
@Builder
public class DriverManager {
private static final ThreadLocal<AppiumDriver<MobileElement>> DRIVER = new ThreadLocal<> ();
private static final String GRID_URL = "@mobile-hub.lambdatest.com/wd/hub";
private static final Logger LOGGER = LogManager.getLogger (
"DriverManager.class");
private static final String LT_ACCESS_TOKEN = System.getenv ("token");
private static final String LT_USERNAME = System.getenv ("username");
private String deviceId;
@SneakyThrows
public DriverManager createRemoteDriver () {
LOGGER.info ("Creating the driver...");
LOGGER.info ("Setting the capabilities..." + capabilities ());
DRIVER.set (new AppiumDriver<> (new URL (
format ("https://{0}:{1}{2}", DriverManager.LT_USERNAME, DriverManager.LT_ACCESS_TOKEN,
DriverManager.GRID_URL)), capabilities ()));
setupDriverTimeouts ();
return this;
}
@SuppressWarnings ("unchecked")
public <D extends AppiumDriver<MobileElement>> D getDriver () {
if (null == DriverManager.DRIVER.get ()) {
createRemoteDriver ();
}
return (D) DriverManager.DRIVER.get ();
}
public void quitDriver () {
if (null != DriverManager.DRIVER.get ()) {
LOGGER.info ("Closing the driver...");
getDriver ().quit ();
DriverManager.DRIVER.remove ();
}
}
private DesiredCapabilities capabilities () {
final DesiredCapabilities capabilities = new DesiredCapabilities ();
try (
final var in = new FileInputStream (requireNonNull (getClass ().getClassLoader ()
.getResource ("parallel.config.json")).getPath ())) {
final var objectMapper = new ObjectMapper ();
final JsonNode jsonNode = objectMapper.readValue (in, JsonNode.class);
final var devices = jsonNode.get ("deviceAndBuildCaps")
.elements ();
devices.forEachRemaining (device -> {
if (device.get ("id")
.asText ()
.equals (this.deviceId)) {
device.fields ()
.forEachRemaining (capability -> {
if (!capability.getKey ()
.equals ("id")) {
capabilities.setCapability (capability.getKey (), capability.getValue ()
.asText ());
}
});
}
});
final JsonNode capabilitiesNode = jsonNode.get ("commonCapabilities");
capabilitiesNode.fields ()
.forEachRemaining (capsEntry -> {
final String commonCapsfieldName = capsEntry.getKey ();
final String commonCapsfieldValue = capsEntry.getValue ()
.asText ();
capabilities.setCapability (commonCapsfieldName, commonCapsfieldValue);
});
} catch (final IOException e) {
LOGGER.error ("Error reading the config json file", e);
}
return capabilities;
}
private void setupDriverTimeouts () {
getDriver ().manage ()
.timeouts ()
.implicitlyWait (30, TimeUnit.SECONDS);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment