In this post, we will discuss how to setup android & ios simulators, write and run appium java tests using Java.
Setup android –
- Download Android studio
- Follow the steps here to download Android SDK
- Update android API levels – Android Studio | Tools | SDK manager | Android SDK | SDK Platforms . ( select the API levels to create android simulators)
- Create android simulator – Android Studio | Tools | AVD Manager
Download Android device manager to manage android simulators – Follow here
or you can use genymotion android simulator, refer here
To get App package and app activity – Use APKInfo.apk
Download reactJS mobile application for android and ios testing, download from here –
https://github.com/saucelabs/my-demo-app-rn/releases
You can use new Maven project with pom.xml as mentioned here
import io.appium.java_client.AppiumBy; import io.appium.java_client.AppiumDriver; import io.appium.java_client.android.AndroidDriver; import io.appium.java_client.android.options.UiAutomator2Options; import io.appium.java_client.ios.IOSDriver; import io.appium.java_client.ios.options.XCUITestOptions; import org.openqa.selenium.By; import java.net.MalformedURLException; import java.net.URL; public class AndroidSampleTest { public static void main(String[] args) throws MalformedURLException, InterruptedException { //https://github.com/saucelabs/my-demo-app-rn/releases UiAutomator2Options options = new UiAutomator2Options(); options.setDeviceName("emulator-5554") .setPlatformVersion("11.0") .setAppPackage("com.saucelabs.mydemoapp.rn") .setAppActivity(".MainActivity"); //.setApp("") //.setNoReset(true) //not install the app if it's already insatlled AppiumDriver driver = new AndroidDriver(new URL("http://0.0.0.0:4723/"), options); driver.findElement(AppiumBy.accessibilityId("open menu")).click(); Thread.sleep(1000); driver.findElement(AppiumBy.accessibilityId("menu item log in")).click(); Thread.sleep(1000); driver.findElement(AppiumBy.accessibilityId("Username input field")).sendKeys("bob@example.com"); driver.findElement(AppiumBy.accessibilityId("Password input field")).sendKeys("10203040"); driver.findElement(AppiumBy.accessibilityId("Login button")).click(); Thread.sleep(3000); driver.quit(); } }
Setup iOS –
- To build ios APP, Download xcode
- Get the simulator as described in the video
- Install the .ipa into real device if you have
or
- install the .app if you use simulator
Get iOS app bundle id –
osascript -e 'id of app "Full path of ios app.app"'
import io.appium.java_client.AppiumBy; import io.appium.java_client.AppiumDriver; import io.appium.java_client.ios.IOSDriver; import io.appium.java_client.ios.options.XCUITestOptions; import java.net.MalformedURLException; import java.net.URL; public class IOSSampleTest { public static void main(String[] args) throws MalformedURLException, InterruptedException { //https://github.com/saucelabs/my-demo-app-rn/releases XCUITestOptions options = new XCUITestOptions(); options.setDeviceName("iPhone SE (3rd generation)") .setPlatformVersion("15.5") .setBundleId("com.saucelabs.mydemoapp.rn"); //.setApp("") //.setNoReset(true) //not install the app if it's already insatlled AppiumDriver driver = new IOSDriver(new URL("http://0.0.0.0:4723/"), options); driver.findElement(AppiumBy.accessibilityId("tab bar option menu")).click(); Thread.sleep(1000); driver.findElement(AppiumBy.accessibilityId("menu item log in")).click(); Thread.sleep(1000); driver.findElement(AppiumBy.accessibilityId("Username input field")).sendKeys("bob@example.com"); driver.findElement(AppiumBy.accessibilityId("Password input field")).sendKeys("10203040"); driver.findElement(AppiumBy.accessibilityId("Login button")).click(); Thread.sleep(3000); driver.quit(); } }
Appium Java code base –
Download code sample from AppiumJava_iOS_Android
Pull the code to you local drive, open the project in Eclipse and run the test.
To run the test – right click on POM.xml | Run As | Maven test
For IOS test demo –
For Android test demo –
Watch demo for details [for earlier version of appium] –
Android – Download apk files for test – APIDemos.apk
IOS –
- Download from here for UICatalog ios app code base.
- Build the code, simulator launches and ios app will be installed.