Created
October 15, 2022 05:15
-
-
Save mfaisalkhatri/1160e03458c573fc601135651c9a2290 to your computer and use it in GitHub Desktop.
Driver Manager class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DriverManager { | |
private static final String APP_PATH = System.getProperty ("user.dir") + "\\src\\test\\resources\\app\\webdriverio-app.apk"; | |
private static final ThreadLocal<AppiumDriver> DRIVER = new ThreadLocal<> (); | |
private static final Logger LOG = LogManager.getLogger ("DriverManager.class"); | |
private static AppiumDriverLocalService service; | |
public static void createAndroidDriver () { | |
startServer (); | |
DRIVER.set (new AndroidDriver (service.getUrl (), setCapabilities ())); | |
setupDriverTimeouts (); | |
} | |
public static void quitSession () { | |
if (null != DRIVER.get ()) { | |
LOG.info ("Closing the driver..."); | |
getDriver ().quit (); | |
DRIVER.remove (); | |
stopServer (); | |
} | |
} | |
public static <D extends AppiumDriver> D getDriver () { | |
return (D) DriverManager.DRIVER.get (); | |
} | |
private static <D extends AppiumDriver> void setDriver (final D driver) { | |
DriverManager.DRIVER.set (driver); | |
} | |
private DriverManager () { | |
} | |
private static void setupDriverTimeouts () { | |
getDriver ().manage () | |
.timeouts () | |
.implicitlyWait (Duration.ofSeconds (30)); | |
} | |
private static DesiredCapabilities setCapabilities () { | |
DesiredCapabilities capabilities = new DesiredCapabilities (); | |
capabilities.setCapability (MobileCapabilityType.PLATFORM_NAME, Platform.ANDROID); | |
capabilities.setCapability (MobileCapabilityType.DEVICE_NAME, "Pixel_5_API_30"); | |
capabilities.setCapability (MobileCapabilityType.APP, APP_PATH); | |
capabilities.setCapability ("appPackage", "com.wdiodemoapp"); | |
capabilities.setCapability ("appActivity", "com.wdiodemoapp.MainActivity"); | |
capabilities.setCapability ("noReset", false); | |
capabilities.setCapability (MobileCapabilityType.AUTOMATION_NAME, AutomationName.ANDROID_UIAUTOMATOR2); | |
return capabilities; | |
} | |
public static void startServer () { | |
AppiumServiceBuilder builder = new AppiumServiceBuilder (); | |
builder.withIPAddress ("127.0.0.1") | |
.usingPort (4723) | |
.usingAnyFreePort () | |
.withAppiumJS ( | |
new File ("C:\\Users\\Faisal Khatri\\AppData\\Roaming\\npm\\node_modules\\appium\\build\\lib\\main.js")) | |
.usingDriverExecutable (new File ("E:\\Program Files\\nodejs\\node.exe")) | |
.withArgument (BASEPATH, "/wd/hub") | |
.withArgument (GeneralServerFlag.SESSION_OVERRIDE) | |
.withArgument (GeneralServerFlag.LOG_LEVEL, "debug"); | |
service = AppiumDriverLocalService.buildService (builder); | |
service.start (); | |
} | |
public static void stopServer () { | |
service.stop (); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment