Best io.appium code snippet using io.appium.java_client.android.Activity
KhathaBook.java
Source:KhathaBook.java
1import POM.LoginPage;2import Utilities.DriverUtilities;3import io.appium.java_client.*;4import io.appium.java_client.android.Activity;5import io.appium.java_client.android.AndroidBatteryInfo;6import io.appium.java_client.android.AndroidDriver;7import io.appium.java_client.android.NetworkSpeed;8import io.appium.java_client.android.nativekey.AndroidKey;9import io.appium.java_client.android.nativekey.KeyEvent;10import io.appium.java_client.android.nativekey.KeyEventFlag;11import io.appium.java_client.touch.TapOptions;12import io.appium.java_client.touch.offset.ElementOption;13import io.appium.java_client.touch.offset.PointOption;14import org.openqa.selenium.By;15import org.openqa.selenium.Point;16import org.openqa.selenium.WebDriver;17import org.openqa.selenium.WebElement;18import org.openqa.selenium.interactions.touch.TouchActions;19import org.testng.Assert;20import org.testng.Reporter;21import org.testng.annotations.*;22import org.openqa.selenium.interactions.Actions;23import java.net.MalformedURLException;24import java.time.Duration;25import java.util.List;26import static io.appium.java_client.touch.LongPressOptions.longPressOptions;27import static io.appium.java_client.touch.WaitOptions.waitOptions;28import static io.appium.java_client.touch.offset.ElementOption.element;29import static io.appium.java_client.touch.offset.PointOption.point;30import static java.time.Duration.ofSeconds;31import static org.testng.Assert.assertEquals;32@Listeners(Utilities.ListenerTest.class)33/**34 * The Main test file thats mapped in testng.xml35 */36public class KhathaBook extends LoginPage {37 public AppiumDriver driver;38 public LoginPage loginPage;39 public KhathaBook() throws MalformedURLException {40 driver = DriverUtilities.driver;41 }42 @BeforeClass43 public void setUp() throws MalformedURLException {44 loginPage = new LoginPage();45 }46 @BeforeMethod47 public void startApp(){48 Activity activity = new Activity("com.vaibhavkalpe.android.khatabook","in.khatabook.android.app.base.presentation.ui.view.MainActivity");49 ((AndroidDriver) driver).startActivity(activity);50}51 @Test(priority = 0, description = "create a customer - add transactions - verify the balance")52 public void verifyDebitCreditDetails() throws InterruptedException {53 loginPage.enterCustomerDetails("vin","");54 Assert.assertEquals(driver.findElementById("com.vaibhavkalpe.android.khatabook:id/newEntryHint").getText(),"Add first transaction of vin");55 Reporter.log("Customer: vin added");56 loginPage.enterAmountGiven("100");57 Reporter.log("First transaction - amount lent: â¹ 100");58 Assert.assertTrue(driver.findElementByXPath("//android.widget.TextView[@text='Send Free SMS to vin']").isDisplayed(),"Send Message option is displayed!");59 loginPage.clickNotNowOption();60 Assert.assertTrue(loginPage.getData(1,"â¹ 100",""));61 Reporter.log("Record Shows â¹ 100 lent");62 loginPage.enterAmountReceived("70");63 Reporter.log("Second transaction - amount received: â¹ 70");...
Capability.java
Source:Capability.java
...16import io.appium.java_client.service.local.AppiumDriverLocalService;17import io.appium.java_client.service.local.AppiumServiceBuilder;18public class Capability {19 protected static String appPackage;20 protected static String appActivity;21 protected static String deviceName;22 protected static String chromedriverExecutable;23 public AppiumDriverLocalService service;24 // creating this method to start my appium through nodejs // this is given by appium server25 public AppiumDriverLocalService startServer()26 {27 boolean flag = checkifserverisRunning(4723);28 if(!flag)29 { 30 //service = AppiumDriverLocalService.buildDefaultService();31 service = AppiumDriverLocalService.buildService(new AppiumServiceBuilder()32 .usingDriverExecutable(new File("C:\\Program Files\\nodejs\\node.exe"))33 .withAppiumJS(new File("C:\\Users\\ShashankCN\\AppData\\Roaming\\npm\\node_modules\\appium\\build\\lib\\main.js"))34 .withIPAddress("127.0.0.1").usingPort(4723)); 35 service.start();36 }37 return service; 38 }39 40 public static boolean checkifserverisRunning(int port)41 {42 boolean isserverRunning = false;43 ServerSocket serverSocket;44 try45 {46 serverSocket = new ServerSocket(port);47 serverSocket.close();48 }49 catch (Exception e) {50 isserverRunning= true;51 }52 finally {53 serverSocket=null;54 }55 return isserverRunning;56 }57 58 public static void startEmulator() throws IOException, InterruptedException59 {60 Runtime.getRuntime().exec(System.getProperty("user.dir")+"\\src\\main\\java\\Emulator.bat");61 Thread.sleep(8000);62 }63 64 public static AndroidDriver<AndroidElement> Capability(String appPackage, String appActivity,String deviceName,String chromedriverExecutable) throws IOException, InterruptedException {65 FileReader fis = new FileReader(System.getProperty("user.dir")+"//src//main//java//global.properties");66 Properties Pro = new Properties();67 Pro.load(fis);68 appPackage = Pro.getProperty("appPakcage");69 appActivity = Pro.getProperty("appActivity");70 deviceName = Pro.getProperty("deviceName");71 if(deviceName.contains("Shashank"))72 {73 startEmulator();74 }75 76 chromedriverExecutable = Pro.getProperty("chromedriverExecutable");77 78 //DesiredCapabilities cap = new DesiredCapabilities();79 //cap.setCapability(MobileCapabilityType.DEVICE_NAME, "Shashank"); 80 //cap.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");81 //cap.setCapability(AndroidMobileCapabilityType.CHROMEDRIVER_EXECUTABLE, "Training\\SDET\\Appium\\chromedriver_win32\\chromedriver.exe");82 //cap.setCapability(MobileCapabilityType.AUTOMATION_NAME, AutomationName.ANDROID_UIAUTOMATOR2);83 //cap.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, "com.androidsample.generalstore"); 84 //cap.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, "com.androidsample.generalstore.SplashActivity");85 //AndroidDriver<AndroidElement> driver = new AndroidDriver<AndroidElement>(new URL("http://127.0.0.1:4723/wd/hub"),cap);86 //return driver;87 DesiredCapabilities cap = new DesiredCapabilities();88 cap.setCapability(MobileCapabilityType.DEVICE_NAME, "Shashank"); 89 cap.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");90 cap.setCapability(AndroidMobileCapabilityType.CHROMEDRIVER_EXECUTABLE, "C:\\SVN\\mobile\\WebApp\\driver.chromedriver.exe");91 cap.setCapability(MobileCapabilityType.AUTOMATION_NAME, AutomationName.ANDROID_UIAUTOMATOR2);92 cap.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, "org.khanacademy.android"); 93 cap.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, "org.khanacademy.android.ui.library.MainActivity");94 AndroidDriver<AndroidElement> driver = new AndroidDriver<AndroidElement>(new URL("http://127.0.0.1:4723/wd/hub"),cap);95 return driver;96 }97}...
DriverManager.java
Source:DriverManager.java
...77 }78 cap.setCapability(AndroidMobileCapabilityType.AUTO_GRANT_PERMISSIONS, true);79 cap.setCapability("systemPort", device.getSystemPort());80 cap.setCapability("appPackage", "com.todoist");81 cap.setCapability("appWaitActivity",82 "com.todoist.activity.HomeActivity,com.todoist.activity.WelcomeActivity");83 cap.setCapability("appWaitDuration", 60000);84 cap.setCapability(MobileCapabilityType.NO_RESET, "false");85 driver = new AndroidDriver<MobileElement>(appiumService, cap);86 setDriver(driver);87 }88}...
CommonUtilss.java
Source:CommonUtilss.java
...42 PLATFORM_VERSION = prop.getProperty("platform.version");43 PLATFORM_NAME = prop.getProperty("platform.name");44 BROWSER_NAME = prop.getProperty("browser.name");45 APP_ACTIVITY = prop.getProperty("application.activity");46 APP_WAIT_ACTIVITY = prop.getProperty("application.WaitActivity");47 BASE_PKG = prop.getProperty("base.pkg");48 }49 // SetAndroidCapabilties50 public void setAndroidCapabilities() {51 capabilties.setCapability(MobileCapabilityType.DEVICE_NAME, DEVICE_NAME);52 capabilties.setCapability(MobileCapabilityType.PLATFORM_NAME, PLATFORM_NAME);53 capabilties.setCapability(MobileCapabilityType.PLATFORM_VERSION, PLATFORM_VERSION);54 capabilties.setCapability(MobileCapabilityType.BROWSER_NAME, BROWSER_NAME);55 capabilties.setCapability(MobileCapabilityType.APP, System.getProperty("user.dir") + APP_PATH);56 capabilties.setCapability("unicodeKeyboard", true);57 capabilties.setCapability("resetKeyboard", true);58 capabilties.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, BASE_PKG);59 capabilties.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, APP_ACTIVITY);60 capabilties.setCapability("autoGrantPermissions", true);...
AppiumDriverType.java
Source:AppiumDriverType.java
1package com.masteringselenium.config;2import io.appium.java_client.AppiumDriver;3import io.appium.java_client.android.Activity;4import io.appium.java_client.android.AndroidDriver;5import io.appium.java_client.remote.AndroidMobileCapabilityType;6import io.appium.java_client.remote.AutomationName;7import io.appium.java_client.remote.MobileCapabilityType;8import org.openqa.selenium.Platform;9import org.openqa.selenium.remote.DesiredCapabilities;10import java.net.URL;11public enum AppiumDriverType implements DriverSetup {12 ANDROID {13 public AppiumDriverType createAppiumObject(URL appiumServerLocation, DesiredCapabilities desiredCapabilities) {14 capabilities = desiredCapabilities;15 serverLocation = appiumServerLocation;16 capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, Platform.ANDROID);17 capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, AutomationName.APPIUM);18 capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Device");19 if (ENABLE_DEBUG_MODE) {20 capabilities.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, "3600");21 }22 return this;23 }24 public AppiumDriver getAppiumDriver() {25 return new AndroidDriver(serverLocation, capabilities);26 }27 };28 private static final boolean ENABLE_DEBUG_MODE = Boolean.getBoolean("enableDebugMode");29 DesiredCapabilities capabilities;30 URL serverLocation;31 public AppiumDriverType setActivity(Activity activity) {32 capabilities.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, activity.getAppPackage());33 capabilities.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, activity.getAppActivity());34 return this;35 }36}...
BaseTest.java
Source:BaseTest.java
...19 desiredCapabilities20 .setCapability(MobileCapabilityType.PLATFORM_NAME, MobilePlatform.ANDROID);21 desiredCapabilities.setCapability(MobileCapabilityType.DEVICE_NAME,"emulator-5554");22 desiredCapabilities.setCapability(AndroidMobileCapabilityType.APP_PACKAGE,"com.ozdilek.ozdilekteyim");23 desiredCapabilities.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY,"com.ozdilek.ozdilekteyim.MainActivity");24 desiredCapabilities.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 3000);25 URL url = new URL("http://127.0.0.1:4723/wd/hub");26 appiumDriver = new AndroidDriver(url, desiredCapabilities);27 }28 @AfterScenario29 public void afterScenario() {30 if(appiumDriver != null)31 appiumDriver.quit();32 }33}...
DriverFactory.java
Source:DriverFactory.java
...14 DesiredCapabilities capabilities = new DesiredCapabilities();15 capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME,"android");16 capabilities.setCapability(MobileCapabilityType.DEVICE_NAME,"emulator-5554");17 capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME,"uiautomator2");18 capabilities.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY,"com.leavjenn.hews.ui.MainActivity");19 return capabilities;20 }21 public void iniciarDriver(){22 try {23 driver = new AppiumDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), configuracaoEmulador());24 }25 catch (MalformedURLException exception){26 System.out.println("url invalida"+ exception.getMessage());27 }28 }29 public AppiumDriver<MobileElement> pegaDriver(){30 return driver;31 }32}...
DriverAndroid.java
Source:DriverAndroid.java
...15 DesiredCapabilities capabilities = new DesiredCapabilities();16 capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME,"android");17 capabilities.setCapability(MobileCapabilityType.DEVICE_NAME,"emulator-5554");18 capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME,"uiautomator2");19 capabilities.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY,"com.leavjenn.hews.ui.MainActivity");20 driver = new AndroidDriver(new URL("http://localhost:4723/wd/hub"), capabilities);21 return capabilities;22 }23 public static AndroidDriver getDriver(){24 return driver;25 }26}...
Activity
Using AI Code Generation
1import io.appium.java_client.android.AndroidDriver;2import io.appium.java_client.android.AndroidElement;3import io.appium.java_client.android.AndroidKeyCode;4import java.net.MalformedURLException;5import java.net.URL;6import java.util.concurrent.TimeUnit;7import org.openqa.selenium.By;8import org.openqa.selenium.By.ByXPath;9import org.openqa.selenium.WebDriver;10import org.openqa.selenium.WebElement;11import org.openqa.selenium.remote.DesiredCapabilities;12import org.openqa.selenium.support.ui.ExpectedConditions;13import org.openqa.selenium.support.ui.WebDriverWait;14public class Appium {15 public static void main(String[] args) throws MalformedURLException, InterruptedException {16 DesiredCapabilities cap = new DesiredCapabilities();17 cap.setCapability("deviceName", "Nexus 5");18 cap.setCapability("platformName", "Android");19 cap.setCapability("platformVersion", "5.0.1");20 cap.setCapability("appPackage", "com.android.calculator2");21 cap.setCapability("appActivity", "com.android.calculator2.Calculator");
Activity
Using AI Code Generation
1import io.appium.java_client.android.Activity;2import io.appium.java_client.android.AndroidDriver;3import io.appium.java_client.android.AndroidElement;4import org.openqa.selenium.By;5import org.openqa.selenium.remote.DesiredCapabilities;6import java.net.URL;7import io.appium.java_client.MobileBy;8import io.appium.java_client.MobileElement;9import io.appium.java_client.MobileDriver;10import org.openqa.selenium.remote.RemoteWebDriver;11import org.openqa.selenium.support.ui.WebDriverWait;12import org.openqa.selenium.WebElement;13import org.openqa.selenium.support.ui.ExpectedConditions;14public class Appium {15 public static void main(String[] args) throws Exception {16 DesiredCapabilities capabilities = new DesiredCapabilities();17 capabilities.setCapability("deviceName", "Android Emulator");18 capabilities.setCapability("platformName", "Android");19 capabilities.setCapability("platformVersion", "9");20 capabilities.setCapability("appPackage", "com.android.calculator2");21 capabilities.setCapability("appActivity", "com.android.calculator2.Calculator");
Activity
Using AI Code Generation
1import io.appium.java_client.android.Activity;2import io.appium.java_client.android.AndroidDriver;3import java.net.URL;4import org.openqa.selenium.remote.DesiredCapabilities;5public class Appium {6public static void main(String[] args) throws Exception {7DesiredCapabilities capabilities = new DesiredCapabilities();8capabilities.setCapability("deviceName", "Android Device");9capabilities.setCapability(CapabilityType.BROWSER_NAME, "Android");10capabilities.setCapability(CapabilityType.VERSION, "4.4");11capabilities.setCapability("platformName", "Android");12capabilities.setCapability("appPackage", "com.android.calculator2");13capabilities.setCapability("appActivity", "com.android.calculator2.Calculator");
Activity
Using AI Code Generation
1package appium;2import java.net.URL;3import org.openqa.selenium.remote.DesiredCapabilities;4import org.testng.annotations.Test;5import io.appium.java_client.android.AndroidDriver;6import io.appium.java_client.android.Activity;7public class Appium {8public void testappium() throws Exception {9DesiredCapabilities capabilities = new DesiredCapabilities();10capabilities.setCapability("deviceName", "Android Emulator");11capabilities.setCapability("browserName", "Android");12capabilities.setCapability("platformVersion", "4.2");13capabilities.setCapability("platformName", "Android");14capabilities.setCapability("appPackage", "com.android.calculator2");15capabilities.setCapability("appActivity", "com.android.calculator2.Calculator");
Activity
Using AI Code Generation
1package appium.java;2import java.net.MalformedURLException;3import java.net.URL;4import java.util.concurrent.TimeUnit;5import org.openqa.selenium.By;6import org.openqa.selenium.WebDriver;7import org.openqa.selenium.WebElement;8import org.openqa.selenium.remote.DesiredCapabilities;9import org.testng.annotations.Test;10import io.appium.java_client.android.AndroidDriver;11public class AppiumTest {12 AndroidDriver driver;13 public void testApp() throws MalformedURLException, InterruptedException{14 DesiredCapabilities cap = new DesiredCapabilities();15 cap.setCapability("deviceName", "Android Emulator");16 cap.setCapability("platformName", "Android");17 cap.setCapability("platformVersion", "6.0");18 cap.setCapability("appPackage", "com.android.calculator2");19 cap.setCapability("appActivity", "com.android.calculator2.Calculator");
Activity
Using AI Code Generation
1driver.startActivity("com.android.calculator2", ".Calculator");2driver.startActivity("com.android.calculator2", ".Calculator", "some extras");3driver.startActivity("com.android.calculator2", ".Calculator", "some extras", 0);4driver.startActivity("com.android.calculator2", ".Calculator", "some extras", 0);5driver.startActivity("com.android.calculator2", ".Calculator", "some extras", 0, "some category");6driver.startActivity("com.android.calculator2", ".Calculator", "some extras", 0, "some category", "some data");7driver.startActivity("com.android.calculator2", ".Calculator", "some extras", 0, "some category", "some data", "some mime type");8driver.startActivity("com.android.calculator2", ".Calculator", "some extras", 0, "some category", "some data", "some mime type", "some package");9driver.startActivity("com.android.calculator2", ".Calculator", "some extras", 0, "some category", "some data", "some mime type", "some package", "some component");10driver.startActivity("com.android.calculator2", ".Calculator", "some extras", 0, "some category", "some data", "some mime type", "some package", "some component", "some URI");11driver.startActivity("com.android.calculator2", ".Calculator", "some extras", 0, "some category", "some data", "some mime type", "some package", "some component", "some URI", "some selector");12driver.startActivity("com.android.calculator2", ".Calculator", "some extras", 0, "some category", "some data", "some mime type", "some package
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!