Best io.appium code snippet using io.appium.java_client.AppiumDriver
MobileBase.java
Source:MobileBase.java
...22import com.relevantcodes.extentreports.ExtentTest;23import com.relevantcodes.extentreports.LogStatus;24import com.restassured.services.ReportPaths;2526import io.appium.java_client.AppiumDriver;27import io.appium.java_client.MobileBy;28import io.appium.java_client.MobileElement;29import io.appium.java_client.TouchAction;30import io.appium.java_client.android.AndroidDriver;31import io.appium.java_client.ios.IOSDriver;32import io.appium.java_client.ios.IOSElement;33import io.appium.java_client.remote.AndroidMobileCapabilityType;34import io.appium.java_client.remote.AutomationName;35import io.appium.java_client.remote.MobileCapabilityType;36import io.appium.java_client.service.local.AppiumDriverLocalService;37import io.appium.java_client.service.local.AppiumServiceBuilder;38import io.appium.java_client.touch.offset.PointOption;3940public class MobileBase {41 42 public static String projectPath = System.getProperty("user.dir");43 public static String mobileReportsPath = projectPath + File.separator + "MobileReports" + File.separator + ReportPaths.reportPathName;44 private String nodeJSPath = System.getenv("NODE_PATH");45 private String appiumServerJSPath = System.getenv("APPIUM_JS_PATH");46 private AppiumDriverLocalService appiumDriverService;47 public AppiumDriver<MobileElement> appiumDriver;48 private ConfigFilesUtility configFileObj;49 50 /**************************************************** Desired Capabilities (Android/iOS) ************************************************/51 /*52 * Set up desired capabilities and pass the Android app-activity and app-package to Appium53 * Create AppiumDriver instance and connect to the Appium server54 * It will launch the Application in Android Device using the configurations specified in Desired Capabilities55 */5657 public AppiumDriver<MobileElement> launchMobileDriver(ExtentReports reports) throws Exception {58 configFileObj = new ConfigFilesUtility();59 configFileObj.loadPropertyFile("DeviceCapabilities.properties");60 appiumDriverService = setUpAppiumDriver();61 startAppiumServer();62 63 DesiredCapabilities capabilities = new DesiredCapabilities();64 65 66 capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, configFileObj.getProperty("deviceName").trim()); 67 capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, configFileObj.getProperty("platformName").trim()); 68 capabilities.setCapability(MobileCapabilityType.FULL_RESET, configFileObj.getProperty("fullreset").trim()); 69 capabilities.setCapability(MobileCapabilityType.UDID, configFileObj.getProperty("udid").trim());70 capabilities.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 60*10); //ending the session 10 minutes71 capabilities.setCapability(MobileCapabilityType.APP, projectPath + File.separator + "mobile" + File.separator + (configFileObj.getProperty("app").trim().contains(".ipa") ? "ios" : "android") + File.separator + configFileObj.getProperty("app").trim());72 73 if(configFileObj.getProperty("app").trim().contains(".ipa")) {74 capabilities.setCapability("bundleid", configFileObj.getProperty("appPackage").trim());75 capabilities.setCapability("wdaLaunchTimeout",60000); // launching webdriver agent time76 capabilities.setCapability("wdaStartupRetries", 2); //webdriveragent retries 4 times77 capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "XCUITest");78 79 appiumDriver = new IOSDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);80 } else {81 capabilities.setCapability(AndroidMobileCapabilityType.AUTO_GRANT_PERMISSIONS,true);82 capabilities.setCapability("autoAcceptAlerts", true);83 capabilities.setCapability("appPackage", configFileObj.getProperty("appPackage").trim());84 capabilities.setCapability("appActivity", configFileObj.getProperty("appActivity").trim());85 capabilities.setCapability("appWaitActivity", configFileObj.getProperty("appActivity").trim());86 //capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, AutomationName.ANDROID_UIAUTOMATOR2);87 //capabilities.setCapability("unicodeKeyboard", "true");88 //capabilities.setCapability("resetKeyboard", "true");89 String platformVersion = configFileObj.getProperty("platformVersion"); 90 if ((platformVersion != null) && !(platformVersion.trim().isEmpty())) {91 capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, configFileObj.getProperty("platformVersion").trim());92 } else {93 capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, configFileObj.getProperty("VERSION").trim());94 }95 if ((platformVersion != null) && !platformVersion.trim().isEmpty() && !(Integer.valueOf(platformVersion.trim().split("\\.")[0]) >= 7) ) {96 capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, AutomationName.ANDROID_UIAUTOMATOR2);97 } else if (Double.valueOf(configFileObj.getProperty("VERSION").trim()) >= 7 ) {98 capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, AutomationName.ANDROID_UIAUTOMATOR2);99 }100 appiumDriver = new AndroidDriver<MobileElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);101 }102 //appiumDriver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);103 ExtentTest test = reports.startTest("Device Information");104 System.out.println("sessionDetails" + new JSONObject(appiumDriver.getSessionDetails()));105 JSONObject sessionObj = new JSONObject(appiumDriver.getSessionDetails());106 String deviceScreenSize = sessionObj.optString("deviceScreenSize");107 String pltformVersion = configFileObj.getProperty("platformVersion");108 String deviceModel = sessionObj.optString("deviceModel");109 String deviceManufacturer = sessionObj.optString("deviceManufacturer");110 String platformName = sessionObj.optString("platformName");111 112 if(configFileObj.getProperty("app").trim().contains(".ipa")) {113 JSONObject rectObj = sessionObj.optJSONObject("viewportRect");114 deviceScreenSize = "Width: " + rectObj.optString("width") + ", Height: " + rectObj.optString("height") + ", Top: " + rectObj.optString("top");115 deviceModel = sessionObj.optString("deviceName");116 } else {117 test.log(LogStatus.INFO, "Device Manufacture : " + deviceManufacturer);118 }119 test.log(LogStatus.INFO, "Device Model : " + deviceModel);120 //test.log(LogStatus.INFO, "Device Screen Size : " + deviceScreenSize);121 test.log(LogStatus.INFO, "Platform Name : " + platformName);122 test.log(LogStatus.INFO, "Platform Version : " + pltformVersion);123 124 return appiumDriver;125 }126 127 128 /********************************** Hide Keyboard (Android/iOS) ****************************/129 public void hideKeyboard() {130 if(appiumDriver != null) {131 if(configFileObj.getProperty("app").contains(".ipa")) {132 hideKeyBoard();133 } else {134 appiumDriver.hideKeyboard();135 }136 }137 }138 139 @SuppressWarnings("rawtypes")140 public void hideKeyBoard() {141 IOSElement element = (IOSElement) appiumDriver.findElementByClassName("XCUIElementTypeKeyboard");142 Point keyboardPoint = element.getLocation();143 TouchAction touchAction = new TouchAction(appiumDriver);144 touchAction.tap(PointOption.point(keyboardPoint.getX() + 2, keyboardPoint.getY() - 2)).perform();145 try {146 Thread.sleep(500);147 } catch (InterruptedException e) {148 e.printStackTrace();149 }150 }151 152 /************************************** APPIUM SETUP ***************************************/153 // Appium server setup service 154 private AppiumDriverLocalService setUpAppiumDriver() {155 // Only for iOS/Mac156 if(configFileObj.getProperty("app").contains(".ipa") || isMac()) {157 nodeJSPath = configFileObj.getProperty("nodePath").trim();158 appiumServerJSPath = configFileObj.getProperty("appiumJSPATH").trim();159 }160 161 appiumDriverService = AppiumDriverLocalService.buildService(new AppiumServiceBuilder()162 .usingDriverExecutable(new File(nodeJSPath))163 .withAppiumJS(new File(appiumServerJSPath))164 .withIPAddress("127.0.0.1")165 .usingPort(4723));166 return appiumDriverService;167 }168 169 public boolean checkIfServerIsRunnning(int port) {170 171 boolean isServerRunning = false;172 ServerSocket serverSocket;173 try {174 serverSocket = new ServerSocket(port);175 serverSocket.close();176 } catch (IOException e) {177 //If control comes here, then it means the port is in use178 isServerRunning = true;179 } finally {180 serverSocket = null;181 }182 return isServerRunning;183 } 184 185 186 // used to start appium server187 private boolean startAppiumServer() {188 if (appiumDriverService != null) {189 appiumDriverService.start();190 return appiumDriverService.isRunning();191 }192 return false;193 }194 195 // used to stop appium server196 protected void stopAppiumServer() {197 if (appiumDriverService != null) {198 appiumDriverService.stop();199 System.out.println(" Stopped Appium Server");200 } 201 }202 203 // add screen count for Report204 public void addScreensExecutionCount(){205 Constants.TOTAL_TC = Constants.TOTAL_TC + 1;206 Constants.IS_TESTCASE = true; Constants.iS_WEB = true;207 }208 209 210 /**************************************************** For iOS **************************************************/211 212 protected MobileElement findElement(String xpath) {213 MobileElement element = null;214 try {215 try {216 element = appiumDriver.findElementByXPath(xpath);217 } catch (Exception e) {218 waitForExpectedElement(appiumDriver, xpath, 60);219 element = appiumDriver.findElementByXPath(xpath);220 }221 if (element.isDisplayed()) {222 return element;223 } else {224 for (int i = 0; i < 5; i++) {225 scrollDown();226 }227 }228 } catch (Exception e) {229 e.printStackTrace();230 }231232 return element;233 }234 235 // It's not working latest appium versions236 public void scrolliniOS(String xpath) {237 MobileElement elements = appiumDriver.findElementByXPath(xpath);238 for (int second = 0;; second++) {239 if (second >= 25){240 break;241 }242 JavascriptExecutor js = (JavascriptExecutor) appiumDriver;243244 HashMap<String, Object> scrollObjects = new HashMap<String,Object>();245 scrollObjects.put("direction", "down");246 scrollObjects.put("element", elements);247 js.executeScript("mobile: scroll", scrollObjects);248 try {249 Thread.sleep(1000);250 } catch (InterruptedException e) {251 e.printStackTrace();252 System.out.println("scroll wont work for you");253254 }255 }256 }257 258 /**************************************************** For ANDROID **********************************************/259 260 /************************************ Find element by Text **********************************************/261 262 /**263 * used to find element by text 264 * Less than android version 7 - Used UiScrollable for scrolling265 * Greater than or equalTo android version 7 - Used TouchAction for scrolling to find the element266 * @param xpath267 * @return MobileElement268 */269 // used to finding the element in screen automatically scrolls full page for finding the element by Text270 protected MobileElement findElementByText(String xpath) {271 272 MobileElement element = null;273 int maxSwipes = 10;274 String text = xpath.split("'")[1]; //Extracting the text from xpath275 try { 276 if (Double.valueOf(getVersion()) >= 7) {277 for (int i = 0; i < maxSwipes; i++) {278 if(isElementFound(xpath)) {279 element = appiumDriver.findElement(MobileBy.AndroidUIAutomator("new UiScrollable(new UiSelector()."280 + "scrollable(true).instance(0))"281 + ".scrollIntoView(new UiSelector().text(\"" + text + "\").instance(0));"));282 break;283 } else {284 scrollDown();285 }286 }287 } else {288 try {289 if (new WebDriverWait(appiumDriver, 15).until(ExpectedConditions.visibilityOfElementLocated(By.xpath(xpath))).isDisplayed()) {290 element = appiumDriver.findElementByXPath(xpath);291 }292 } catch (Exception e) {293 element = scrollByText(text, xpath);294 }295 }296 } catch (Exception e) {297 e.printStackTrace();298 }299 return element;300 }301 302303 private String getVersion() {304 JSONObject sessionObj = null;305 if(sessionObj == null) {306 sessionObj = new JSONObject(appiumDriver.getSessionDetails());307 }308 String version = sessionObj.optString("platformVersion").trim().split("\\.")[0];309 return version;310 }311 312 public boolean isElementFound(String text) {313 try {314 appiumDriver.findElement(By.xpath(text));315 } catch(Exception e){316 return false;317 }318 return true;319 }320 321 322 public MobileElement scrollByText(String text, String xpath) {323 try {324 return appiumDriver.findElement(MobileBy.AndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text(\"" + text + "\").instance(0));"));325 } catch (Exception e) {326 WebElement isElementPresence = waitForExpectedElement(appiumDriver, xpath, 60);327 if (isElementPresence.isDisplayed()) {328 return appiumDriver.findElementByXPath(xpath);329 }330 }331 return null;332 }333 334 /*************************************** Find element by Id *************************************/335 // used to finding the element in screen 336 // scrolls full page for finding the element by ID 337 protected MobileElement findElementById(String id) {338 MobileElement element = null;339 String xpath = id.split("'")[1];340 try {341 //element = (MobileElement) appiumDriver.findElementByAccessibilityId(xpath);342 element = appiumDriver.findElement(MobileBy.AndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0))" +343 ".scrollIntoView(new UiSelector().resourceId(\"" + xpath + "\").instance(0))"));344 } catch (Exception e) {345 WebElement isElementPresence = waitForExpectedElement(appiumDriver, id, 60);346 if(isElementPresence.isDisplayed()) {347 element = appiumDriver.findElement(MobileBy.AndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0))" +348 ".scrollIntoView(new UiSelector().resourceId(\"" + xpath + "\").instance(0))"));349 }350 }351 System.out.println("Search Box Name - " + element.getText());352 return element;353 }354 355 //Only For android password field356 protected WebElement findElementByPwd(String Pwd, String value) {357 WebElement PasswordTextInputText = waitForExpectedElement(appiumDriver, Pwd, 60);358 PasswordTextInputText.sendKeys(value);359 return PasswordTextInputText;360 }361 362 // wait 60 seconds for finding the element363 public static WebElement waitForExpectedElement(AppiumDriver<MobileElement> appiumDriver, String locator, long timeOutInSeconds) {364 WebDriverWait wait = new WebDriverWait(appiumDriver, timeOutInSeconds);365 appiumDriver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);366 return wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(locator)));367 }368 369 370 371 /*********************************** ScrollDown (Android/iOS) ***********************************/372 373 private void scrollDown() {374 //if pressX was zero it didn't work for me375 int pressX = appiumDriver.manage().window().getSize().width / 2;376 // 4/5 of the screen as the bottom finger-press point377 int bottomY = appiumDriver.manage().window().getSize().height * 4/5;378 // just non zero point, as it didn't scroll to zero normally379 int topY = appiumDriver.manage().window().getSize().height / 8;380 //scroll with TouchAction by itself381 scroll(pressX, bottomY, pressX, topY);382 }383 384 385 /*386 * Don't forget that it's "natural scroll" where 387 * fromY is the point where you press and toY where you release it388 */389 @SuppressWarnings("rawtypes")390 private void scroll(int fromX, int fromY, int toX, int toY) {391 TouchAction touchAction = new TouchAction(appiumDriver);392 touchAction.longPress(PointOption.point(fromX, fromY)).moveTo(PointOption.point(toX, toY)).release().perform();393 }394395 /************************************** Reports ******************************************/396 397 // These methods used to write the extent reports and logger398 public void testPass(String message, ExtentTest test, Logger logger) {399 test.log(LogStatus.PASS, message);400 logger.info(message);401 }402403 public void testInfo(String message, ExtentTest test, Logger logger) {404 test.log(LogStatus.INFO, message);405 logger.info(message);406 }407408 public void testFail(String message, ExtentTest test, Logger logger, AppiumDriver<MobileElement> mobileDriver) {409 test.log(LogStatus.FAIL, message);410 logger.error(message);411 Utilities.setMobilePlatform();412 test.log(LogStatus.INFO, "Screenshot Taken : " + Utilities.captureScreenshot(mobileDriver, message + " is Failed"));413 }414415 // ScreenName Header for Reports416 public static void testLogHeader(ExtentTest test ,String data) {417 test.log(LogStatus.INFO, "<b style = 'background-color: #ffffff; color : #ff8f00 ; font-size : 18px' >"+ data + "</b>");418 }419 420 421 /*********************************** Find the Client OS ****************************************/422
...
CommonMethodsFunctions.java
Source:CommonMethodsFunctions.java
...4import java.util.Set;5import org.openqa.selenium.Dimension;6import org.openqa.selenium.WebElement;7import org.openqa.selenium.interactions.Actions;8import io.appium.java_client.AppiumDriver;9import io.appium.java_client.FindsByAndroidUIAutomator;10import io.appium.java_client.MobileElement;11import io.appium.java_client.TouchAction;12import io.appium.java_client.touch.WaitOptions;13import io.appium.java_client.touch.offset.PointOption;14import src.test.java.Driver.DesiredCapabilities;15public class CommonMethodsFunctions extends DesiredCapabilities{16 17 /**18 * 19 * @param text20 * @param driver21 * @throws MalformedURLException22 * @throws InterruptedException23 */24 public void scrollToText(String text, AppiumDriver<MobileElement> driver)25 throws MalformedURLException, InterruptedException {26 MobileElement el = (MobileElement) ((FindsByAndroidUIAutomator) driver)27 .findElementByAndroidUIAutomator("new UiScrollable("28 + "new UiSelector().scrollable(true)).scrollIntoView(" 29 + "new UiSelector().textContains(\""+text+"\"));");30 el.click();31 }32 33 /**34 * 35 * @param driver36 * @info : Swipe the Page in Horizontal Direction37 */38 public static void swipeHorizontal(AppiumDriver<MobileElement> driver) {39 40 41 Log.info("Call swipe left...");42 43 Dimension size = driver.manage().window().getSize();44 int anchor = (int) (size.height * .15);45 int startPoint = (int) (size.width * .10);46 int endPoint = (int) (size.width * .50);47 48 Log.info("Swipe: "+anchor+", "+startPoint+", "+endPoint+" and Size: "+size);49 new TouchAction(driver).press(PointOption.point(950, 700)).waitAction(WaitOptions.waitOptions(Duration.ofMillis(100))).moveTo(PointOption.point(100, 700)).release().perform();50 Log.info("Element Swiped...");51 }52 53 /**54 * 55 * @param element56 * @param driver57 * @throws InterruptedException58 */59 public void clickAction(WebElement element, AppiumDriver<MobileElement> driver) throws InterruptedException {60 Thread.sleep(2000);61 Actions builder = new Actions(driver);62 builder.moveToElement(element).click();63 builder.perform();64 }65 66 /**67 * Switch to WebView context68 * 69 * @param driver70 */71 public void switchToWebView(AppiumDriver<MobileElement> driver) {72 Set<String> availableContexts = driver.getContextHandles();73 Log.info("Total No of Context Found = " + availableContexts.size());74 Log.info("Available Contexts: " + availableContexts);75 for (String context : availableContexts) {76 Log.info("context: " + context);77 78 if (context.contains("WEBVIEW")) {79 Log.info("Switching to Context: " + context);80 driver.context(context);81 Log.info("Switching to Context: " + context);82 83 break;84 } else {85 86 Log.info("Expected context not found");87 }88 }89 }90 /** 91 * Switch to Native context92 * 93 * @param driver94 */95 public void switchToNativeApp(AppiumDriver<MobileElement> driver) {96 driver.context("NATIVE_APP");97 98 Log.info("Context switched to " + "NATIVE_APP");99 }100}...
BaseTest.java
Source:BaseTest.java
1import com.thoughtworks.gauge.AfterScenario;2import com.thoughtworks.gauge.BeforeScenario;3import io.appium.java_client.AppiumDriver;4import io.appium.java_client.MobileElement;5import io.appium.java_client.android.AndroidDriver;6import io.appium.java_client.ios.IOSDriver;7import io.appium.java_client.remote.AndroidMobileCapabilityType;8import io.appium.java_client.remote.IOSMobileCapabilityType;9import io.appium.java_client.remote.MobileCapabilityType;10import io.appium.java_client.remote.MobilePlatform;11import org.openqa.selenium.remote.DesiredCapabilities;12import java.net.MalformedURLException;13import java.net.URL;14public class BaseTest {15 protected static AppiumDriver<MobileElement> appiumDriver;16 protected boolean localAndroid = true;17 @BeforeScenario18 public void Education() throws MalformedURLException {19 if(localAndroid){20 System.out.println("Android testi baÅlıyor");21 DesiredCapabilities desiredCapabilities = new DesiredCapabilities();22 desiredCapabilities23 .setCapability(MobileCapabilityType.PLATFORM_NAME, MobilePlatform.ANDROID);24 desiredCapabilities.setCapability(MobileCapabilityType.DEVICE_NAME,"emulator-5554");25 desiredCapabilities.setCapability(AndroidMobileCapabilityType.APP_PACKAGE,"com.ozdilek.ozdilekteyim");26 desiredCapabilities.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY,"com.ozdilek.ozdilekteyim.MainActivity");27 desiredCapabilities.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 3000);28 URL url = new URL("http://127.0.0.1:4723/wd/hub");29 appiumDriver = new AndroidDriver(url, desiredCapabilities);...
AppEntrance.java
Source:AppEntrance.java
1package com.ushio.appium;2import com.ushio.appium.page.LaunchPage;3import com.ushio.util.ThreadUtil;4import io.appium.java_client.AppiumDriver;5import io.appium.java_client.MobileElement;6import io.appium.java_client.remote.AndroidMobileCapabilityType;7import io.appium.java_client.remote.MobileCapabilityType;8import io.appium.java_client.remote.MobilePlatform;9import org.openqa.selenium.remote.DesiredCapabilities;10import org.openqa.selenium.support.ui.WebDriverWait;11import java.net.URL;12import java.util.concurrent.TimeUnit;13/**14 * @author: ushio15 * @description:appå
¥å£ç±»ï¼åä¾ç±»16 **/17public class AppEntrance {18 private volatile static AppEntrance appEntrance;19 private static AppiumDriver<MobileElement> appiumDriver;20 private static WebDriverWait webDriverWait;21 public static AppEntrance getInstance() {22 if (appEntrance == null) {23 synchronized (AppEntrance.class) {24 if (appEntrance == null) {25 appEntrance = new AppEntrance();26 }27 }28 }29 return appEntrance;30 }31 private AppEntrance(){32 initWebDriver();33 }34 private void initWebDriver() {35 if (appiumDriver == null) {36 DesiredCapabilities capabilities = new DesiredCapabilities();37 capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, AppiumConstant.DEVICE_NAME);38 capabilities.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, AppiumConstant.APP_PACKAGE);39 capabilities.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, AppiumConstant.APP_ACTIVITY);40 capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, AppiumConstant.PLATFORM_NAME);41 capabilities.setCapability(MobileCapabilityType.NO_RESET, AppiumConstant.NO_RESET);42// capabilities.setCapability(AndroidMobileCapabilityType.DONT_STOP_APP_ON_RESET, true);43 try {44 appiumDriver = new AppiumDriver<>(new URL(AppiumConstant.URL),45 capabilities);46 appiumDriver.manage().timeouts().implicitlyWait(AppiumConstant.IMPLICITLY_WAIT, TimeUnit.SECONDS);47 webDriverWait = new WebDriverWait(appiumDriver, 60,1000);48 } catch (Exception e) {49 e.printStackTrace();50 }51 }52 }53 public void quit() {54 if (appiumDriver != null) {55 ThreadUtil.sleep(15000);56 appiumDriver.quit();57 }58 }...
BaseMethods.java
Source:BaseMethods.java
1package Base;2import com.thoughtworks.gauge.Logger;3import io.appium.java_client.AppiumDriver;4import io.appium.java_client.MobileElement;5import io.appium.java_client.PerformsTouchActions;6import io.appium.java_client.TouchAction;7import io.appium.java_client.touch.WaitOptions;8import io.appium.java_client.touch.offset.PointOption;9import org.openqa.selenium.By;10import org.openqa.selenium.Dimension;11import org.junit.Assert;12import java.time.Duration;13public class BaseMethods extends BaseSettings {14 protected static AppiumDriver<MobileElement> appiumDriver;15 public BaseMethods(AppiumDriver<MobileElement> appiumDriver) {16 this.appiumDriver = appiumDriver;17 }18 public void waitForSecond(int x) throws InterruptedException {19 Thread.sleep(1000 * x);20 Logger.info(x + " saniye bekleniyor...");21 }22 public void clickId(String id) {23 appiumDriver.findElement(By.id(id)).click();24 }25 public void clickXpath(String xpath) {26 appiumDriver.findElement(By.xpath(xpath)).click();27 }28 public void checkPage(String id) {29 Assert.assertNotNull("acilmadii", appiumDriver.findElement(By.id(id)));...
DriverFactory.java
Source:DriverFactory.java
1package driverFactory;2import io.appium.java_client.AppiumDriver;3import io.appium.java_client.MobileElement;4import io.appium.java_client.android.AndroidDriver;5import io.appium.java_client.android.GsmCallActions;6import io.appium.java_client.remote.AndroidMobileCapabilityType;7import io.appium.java_client.remote.MobileCapabilityType;8import org.openqa.selenium.remote.DesiredCapabilities;9import java.net.MalformedURLException;10import java.net.URL;11public class DriverFactory {12 AppiumDriver<MobileElement> driver;13 public DesiredCapabilities configuracaoEmulador() throws MalformedURLException {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}...
Session.java
Source:Session.java
1package sessions;2import controlsAppium.Label;3import factoryDevices.FactoryDevice;4import io.appium.java_client.AppiumDriver;5import io.appium.java_client.TouchAction;6import io.appium.java_client.touch.WaitOptions;7import io.appium.java_client.touch.offset.PointOption;8import org.openqa.selenium.By;9import java.net.MalformedURLException;10import java.time.Duration;11public class Session {12 private static Session instance = null;13 private AppiumDriver driver;14 private Session() throws MalformedURLException {15 driver = FactoryDevice.make(FactoryDevice.DeviceType.ANDROID).create();16 }17 public static Session getInstance() throws MalformedURLException {18 if (instance == null)19 instance = new Session();20 return instance;21 }22 public void close() {23 driver.quit();24 instance = null;25 }26 public AppiumDriver getDriver() {27 return driver;28 }29}...
ThreadDriver.java
Source:ThreadDriver.java
1package suporte.driver;2import java.io.Console;3import java.util.List;4import io.appium.java_client.AppiumDriver;5import io.appium.java_client.MobileElement;6public class ThreadDriver {7 private static ThreadLocal<AppiumDriver<MobileElement>> _driver = new ThreadLocal<AppiumDriver<MobileElement>>();8 public synchronized static void setTDriver(AppiumDriver<MobileElement> driver) {9 _driver.set(driver);10 }11 public synchronized static AppiumDriver<MobileElement> getTDriver() {12 return _driver.get();13 }14 15}...
AppiumDriver
Using AI Code Generation
1import io.appium.java_client.AppiumDriver;2import io.appium.java_client.android.AndroidDriver;3import org.openqa.selenium.remote.DesiredCapabilities;4import java.net.URL;5import java.net.MalformedURLException;6import java.lang.InterruptedException;7public class AppiumTest {8 public static void main(String[] args) throws MalformedURLException, InterruptedException {9 DesiredCapabilities caps = new DesiredCapabilities();10 caps.setCapability("deviceName", "Pixel 2 API 29");11 caps.setCapability("platformName", "Android");12 caps.setCapability("platformVersion", "10.0");13 caps.setCapability("appPackage", "com.android.calculator2");14 caps.setCapability("appActivity", "com.android.calculator2.Calculator");15 caps.setCapability("noReset", "true");
AppiumDriver
Using AI Code Generation
1import io.appium.java_client.AppiumDriver;2import io.appium.java_client.MobileElement;3import io.appium.java_client.android.AndroidDriver;4import io.appium.java_client.remote.MobileCapabilityType;5import java.net.MalformedURLException;6import java.net.URL;7import java.util.concurrent.TimeUnit;8import org.openqa.selenium.remote.DesiredCapabilities;9public class Appium {10 public static AppiumDriver<MobileElement> driver;11 public static void main(String[] args) {12 try {13 openCalculator();14 } catch (MalformedURLException e) {15 System.out.println(e.getCause());16 System.out.println(e.getMessage());17 e.printStackTrace();18 }19 }20 public static void openCalculator() throws MalformedURLException {21 DesiredCapabilities cap = new DesiredCapabilities();22 cap.setCapability("deviceName", "My Phone");23 cap.setCapability("udid", "emulator-5554");24 cap.setCapability("platformName", "Android");25 cap.setCapability("platformVersion", "8.1.0");26 cap.setCapability(MobileCapabilityType.NO_RESET, "true");27 cap.setCapability("appPackage", "com.android.calculator2");28 cap.setCapability("appActivity", "com.android.calculator2.Calculator");
AppiumDriver
Using AI Code Generation
1package appium;2import java.net.URL;3import java.util.concurrent.TimeUnit;4import org.openqa.selenium.By;5import org.openqa.selenium.WebDriver;6import org.openqa.selenium.WebElement;7import org.openqa.selenium.remote.DesiredCapabilities;8import io.appium.java_client.AppiumDriver;9import io.appium.java_client.MobileBy;10import io.appium.java_client.MobileElement;11import io.appium.java_client.android.AndroidDriver;12import io.appium.java_client.android.AndroidKeyCode;13import io.appium.java_client.android.AndroidTouchAction;14public class Appium {15public static void main(String args[]) throws Exception {16DesiredCapabilities caps = new DesiredCapabilities();17caps.setCapability("deviceName", "Redmi Note 4");18caps.setCapability("udid", "
AppiumDriver
Using AI Code Generation
1package appium.java;2import io.appium.java_client.AppiumDriver;3import io.appium.java_client.android.AndroidDriver;4import io.appium.java_client.remote.MobileCapabilityType;5import java.net.MalformedURLException;6import java.net.URL;7import org.openqa.selenium.remote.DesiredCapabilities;8public class AppiumJava {9public static void main(String[] args) throws MalformedURLException {10DesiredCapabilities cap = new DesiredCapabilities();11cap.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Device");12cap.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");13cap.setCapability(MobileCapabilityType.PLATFORM_VERSION, "6.0");14cap.setCapability(MobileCapabilityType.UDID, "emulator-5554");15cap.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, "25");16cap.setCapability("appPackage", "com.android.calculator2");17cap.setCapability("appActivity", "com.android.calculator2.Calculator");
AppiumDriver
Using AI Code Generation
1import java.net.MalformedURLException;2import java.net.URL;3import java.util.concurrent.TimeUnit;4import org.openqa.selenium.remote.DesiredCapabilities;5import io.appium.java_client.AppiumDriver;6import io.appium.java_client.MobileElement;7public class Appium {8 public static void main(String[] args) {9 try {10 openCalculator();11 } catch (Exception e) {12 System.out.println(e.getCause());13 System.out.println(e.getMessage());14 e.printStackTrace();15 }16 }17 public static void openCalculator() throws MalformedURLException {18 DesiredCapabilities cap = new DesiredCapabilities();19 cap.setCapability("deviceName", "Pixel 2");20 cap.setCapability("udid", "emulator-5554");21 cap.setCapability("platformName", "Android");22 cap.setCapability("platformVersion", "9.0");23 cap.setCapability("appPackage", "com.android.calculator2");24 cap.setCapability("appActivity", "com.android.calculator2.Calculator");25 cap.setCapability("noReset", "true");
AppiumDriver
Using AI Code Generation
1import java.net.URL;2import org.openqa.selenium.remote.DesiredCapabilities;3import io.appium.java_client.AppiumDriver;4import io.appium.java_client.MobileElement;5public class Appium {6 public static void main(String[] args) throws Exception {7 DesiredCapabilities capabilities = new DesiredCapabilities();8 capabilities.setCapability("deviceName", "My Phone");9 capabilities.setCapability("BROWSER_NAME", "Android");10 capabilities.setCapability("VERSION", "4.4.2");11 capabilities.setCapability("platformName", "Android");12 capabilities.setCapability("appPackage", "com.android.calculator2");13 capabilities.setCapability("appActivity", "com.android.calculator2.Calculator");
AppiumDriver
Using AI Code Generation
1package appium.java;2import io.appium.java_client.android.AndroidDriver;3import java.net.URL;4import java.net.MalformedURLException;5import org.openqa.selenium.remote.DesiredCapabilities;6import org.openqa.selenium.By;7import org.openqa.selenium.WebElement;8import org.openqa.selenium.support.ui.ExpectedConditions;9import org.openqa.selenium.support.ui.WebDriverWait;10public class AppiumJava {11 public static void main(String[] args) throws MalformedURLException, InterruptedException {12 DesiredCapabilities capabilities = new DesiredCapabilities();13 capabilities.setCapability("deviceName", "Android Emulator");14 capabilities.setCapability("browserName", "Android");15 capabilities.setCapability("platformVersion", "4.4.2");16 capabilities.setCapability("platformName", "Android");17 capabilities.setCapability("appPackage", "com.android.calculator2");18 capabilities.setCapability("appActivity", "com.android.calculator2.Calculator");
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!!