Writing Your First Script
Now that everything is set up, you can write your first Appium test script! You can use any programming language that you’re comfortable with. The test script will send commands to the Appium server, which will interact with the mobile device.
Here is an example test script to launch an app and login on Android:
Java
C#
Python
Javascript
Kotlin
Copy
import io.appium.java_client.AppiumBy; import io.appium.java_client.android.AndroidDriver; import io.appium.java_client.android.options.UiAutomator2Options; import org.openqa.selenium.WebElement; import java.net.MalformedURLException; import java.net.URL; import org.openqa.selenium.remote.RemoteWebDriver; public class AppiumTest { public static void main(String[] args) throws MalformedURLException { // Define the Desired Capabilities UiAutomator2Options options = new UiAutomator2Options(); options.setPlatformName("Android"); options.setDeviceName("Pixel_4_Emulator"); // Replace with the name of your device/emulator options.setAutomationName("UiAutomator2"); options.setApp("/path/to/your/app.apk"); // Path to your APK file // Initialize the AndroidDriver URL appiumServerUrl = new URL("http://localhost:4723"); RemoteWebDriver driver = new AndroidDriver<>(appiumServerUrl, options); // Example Test: Interacting with an Element // Wait for the app to launch and find the login button (replace with the correct ID) WebElement loginButton = driver.findElement(AppiumBy.accessibilityId("loginButton")); // Click the login button loginButton.click(); // Find the username field (replace with the correct element locator) WebElement usernameField = driver.findElement(AppiumBy.id("com.example:id/username")); // Input text into the username field usernameField.sendKeys("testuser"); // Find the password field (replace with the correct element locator) WebElement passwordField = driver.findElement(AppiumBy.id("com.example:id/password")); // Input text into the password field passwordField.sendKeys("password"); // Click the login button again loginButton.click(); // Close the driver after test execution driver.quit(); } }
C# code coming soon
Python code coming soon
Javascript code coming soon
Kotlin code coming soon