Driver specific classes - Notes By ShariqSP
Understanding the UiAutomator2Options
Class
The UiAutomator2Options
class is part of the Appium Java client, providing a streamlined way to configure capabilities for Android automation using the UiAutomator2 driver. This class eliminates the need to manually define desired capabilities by offering predefined methods.
Key Methods of UiAutomator2Options
Class
-
setApp(String app)
: Specifies the path to the application under test. -
setDeviceName(String deviceName)
: Sets the target device name. -
setPlatformVersion(String platformVersion)
: Defines the OS version of the device. -
setAutomationName(String automationName)
: Sets the automation engine (e.g., UiAutomator2). -
setNoReset(boolean noReset)
: Controls whether the app state is retained between sessions.
Example Usage in a Test Script
import io.appium.java_client.android.options.UiAutomator2Options; import io.appium.java_client.android.AndroidDriver; public class AndroidTest { public static void main(String[] args) { // Create UiAutomator2Options object UiAutomator2Options options = new UiAutomator2Options() .setDeviceName("Pixel_4_Emulator") .setPlatformVersion("11.0") .setApp("/path/to/app.apk") .setAutomationName("UiAutomator2") .setNoReset(true); // Initialize Appium driver AndroidDriver driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), options); // Your test script logic here // Quit the driver driver.quit(); } }
Understanding the XCUITestOptions
Class
The XCUITestOptions
class is designed for configuring iOS automation using the XCUITest driver. This class simplifies capability management by providing tailored methods for iOS-specific settings.
Key Methods of XCUITestOptions
Class
-
setApp(String app)
: Specifies the path to the iOS app (.ipa or .app file). -
setDeviceName(String deviceName)
: Sets the name of the target device. -
setPlatformVersion(String platformVersion)
: Configures the iOS version of the device. -
setAutomationName(String automationName)
: Sets the automation engine (e.g., XCUITest). -
setNoReset(boolean noReset)
: Retains or resets the app state between test sessions.
Example Usage in a Test Script
import io.appium.java_client.ios.options.XCUITestOptions; import io.appium.java_client.ios.IOSDriver; public class iOSTest { public static void main(String[] args) { // Create XCUITestOptions object XCUITestOptions options = new XCUITestOptions() .setDeviceName("iPhone 12") .setPlatformVersion("14.0") .setApp("/path/to/app.ipa") .setAutomationName("XCUITest") .setNoReset(true); // Initialize Appium driver IOSDriver driver = new IOSDriver(new URL("http://127.0.0.1:4723/wd/hub"), options); // Your test script logic here // Quit the driver driver.quit(); } }
Understanding the EspressoOptions
Class
The EspressoOptions
class provides a dedicated way to configure capabilities for Android automation using the Espresso driver. This is especially useful for testing native Android apps with advanced interaction requirements.
Key Methods of EspressoOptions
Class
-
setApp(String app)
: Specifies the path to the application under test. -
setDeviceName(String deviceName)
: Configures the target device name. -
setPlatformVersion(String platformVersion)
: Defines the OS version of the device. -
setAutomationName(String automationName)
: Sets the automation engine to Espresso. -
setNoReset(boolean noReset)
: Determines whether to retain app state between sessions.
Example Usage in a Test Script
import io.appium.java_client.android.options.EspressoOptions; import io.appium.java_client.android.AndroidDriver; public class EspressoTest { public static void main(String[] args) { // Create EspressoOptions object EspressoOptions options = new EspressoOptions() .setDeviceName("Pixel_4_Emulator") .setPlatformVersion("11.0") .setApp("/path/to/app.apk") .setAutomationName("Espresso") .setNoReset(true); // Initialize Appium driver AndroidDriver driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), options); // Your test script logic here // Quit the driver driver.quit(); } }