Best Carina code snippet using com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.CmdLine.insertCommandsAfter
Source:Device.java
...157 if (this.isNull())158 return -1;159 160 dropFile(pathToFile);161 String[] cmd = CmdLine.insertCommandsAfter(executor.getDefaultCmd(), "-s", getUdid(), "shell", "screenrecord", "--bit-rate", "1000000", "--verbose", pathToFile);162 try {163 ProcessBuilderExecutor pb = new ProcessBuilderExecutor(cmd);164 pb.start();165 return pb.getPID();166 } catch (ExecutorException e) {167 e.printStackTrace();168 return -1;169 }170 }171 172 public void stopRecording(Integer pid) {173 if (isNull())174 return;175 176 if (pid != null && pid != -1) {177 Platform.killProcesses(Arrays.asList(pid));178 }179 }180 181 public void dropFile(String pathToFile) {182 if (this.isNull())183 return;184 String[] cmd = CmdLine.insertCommandsAfter(executor.getDefaultCmd(), "-s", getUdid(), "shell", "rm", pathToFile);185 executor.execute(cmd);186 }187 188 public String getFullPackageByName(final String name) {189 String deviceUdid = getUdid();190 LOGGER.info("Device udid: ".concat(deviceUdid));191 String[] cmd = CmdLine.createPlatformDependentCommandLine("adb", "-s", deviceUdid, "shell", "pm", "list",192 "packages");193 LOGGER.info("Following cmd will be executed: " + Arrays.toString(cmd));194 List<String> packagesList = executor.execute(cmd);195 LOGGER.info("Found packages: ".concat(packagesList.toString()));196 String resultPackage = null;197 for (String packageStr : packagesList) {198 if (packageStr.matches(String.format(".*%s.*", name))) {199 LOGGER.info("Package was found: ".concat(packageStr));200 resultPackage = packageStr;201 break;202 }203 }204 if (null == resultPackage) {205 LOGGER.info("Package wasn't found using following name: "206 .concat(name));207 resultPackage = "not found";208 }209 return resultPackage;210 }211 212 public void pullFile(String pathFrom, String pathTo) {213 if (isNull())214 return;215 String[] cmd = CmdLine.insertCommandsAfter(executor.getDefaultCmd(), "-s", getUdid(), "pull", pathFrom, pathTo);216 executor.execute(cmd);217 }218 219 220 221 private Boolean getScreenState() {222 // determine current screen status223 // adb -s <udid> shell dumpsys input_method | find "mScreenOn"224 String[] cmd = CmdLine.insertCommandsAfter(executor.getDefaultCmd(), "-s", getUdid(), "shell", "dumpsys",225 "input_method");226 List<String> output = executor.execute(cmd);227 Boolean screenState = null;228 String line;229 Iterator<String> itr = output.iterator();230 while (itr.hasNext()) {231 // mScreenOn - default value for the most of Android devices232 // mInteractive - for Nexuses233 line = itr.next();234 if (line.contains("mScreenOn=true") || line.contains("mInteractive=true")) {235 screenState = true;236 break;237 }238 if (line.contains("mScreenOn=false") || line.contains("mInteractive=false")) {239 screenState = false;240 break;241 }242 }243 if (screenState == null) {244 LOGGER.error(udid245 + ": Unable to determine existing device screen state!");246 return screenState; //no actions required if state is not recognized.247 }248 if (screenState) {249 LOGGER.info(udid + ": screen is ON");250 }251 if (!screenState) {252 LOGGER.info(udid + ": screen is OFF");253 }254 return screenState;255 }256 public void screenOff() {257 if (!Configuration.get(Parameter.MOBILE_PLATFORM_NAME).equalsIgnoreCase(SpecialKeywords.ANDROID)) {258 return;259 }260 if (!Configuration.getBoolean(Parameter.MOBILE_SCREEN_SWITCHER)) {261 return;262 }263 264 if (isNull())265 return;266 Boolean screenState = getScreenState();267 if (screenState == null) {268 return;269 }270 if (screenState) {271 String[] cmd = CmdLine.insertCommandsAfter(executor.getDefaultCmd(), "-s", getUdid(), "shell", "input",272 "keyevent", "26");273 executor.execute(cmd);274 pause(5);275 screenState = getScreenState();276 if (screenState) {277 LOGGER.error(udid + ": screen is still ON!");278 }279 if (!screenState) {280 LOGGER.info(udid + ": screen turned off.");281 }282 }283 }284 public void screenOn() {285 if (!Configuration.get(Parameter.MOBILE_PLATFORM_NAME).equalsIgnoreCase(SpecialKeywords.ANDROID)) {286 return;287 }288 if (!Configuration.getBoolean(Parameter.MOBILE_SCREEN_SWITCHER)) {289 return;290 }291 if (isNull())292 return;293 294 Boolean screenState = getScreenState();295 if (screenState == null) {296 return;297 }298 if (!screenState) {299 String[] cmd = CmdLine.insertCommandsAfter(executor.getDefaultCmd(), "-s", getUdid(), "shell",300 "input", "keyevent", "26");301 executor.execute(cmd);302 pause(5);303 // verify that screen is Off now304 screenState = getScreenState();305 if (!screenState) {306 LOGGER.error(udid + ": screen is still OFF!");307 }308 if (screenState) {309 LOGGER.info(udid + ": screen turned on.");310 }311 }312 }313 314 public void pressKey(int key) {315 if (isNull())316 return;317 String[] cmd = CmdLine.insertCommandsAfter(executor.getDefaultCmd(), "-s", getUdid(), "shell", "input",318 "keyevent", String.valueOf(key));319 executor.execute(cmd);320 }321 322 public void pause(long timeout) {323 try {324 Thread.sleep(timeout * 1000);325 } catch (InterruptedException e) {326 e.printStackTrace();327 }328 }329 330 public void clearAppData() {331 clearAppData(Configuration.get(Parameter.MOBILE_APP));332 }333 334 public void clearAppData(String app) {335 if (!Configuration.get(Parameter.MOBILE_PLATFORM_NAME).equalsIgnoreCase(SpecialKeywords.ANDROID)) {336 return;337 }338 339 if (!Configuration.getBoolean(Parameter.MOBILE_APP_CLEAR_CACHE))340 return;341 if (isNull())342 return;343 //adb -s UDID shell pm clear com.myfitnesspal.android344 String packageName = getApkPackageName(app);345 String[] cmd = CmdLine.insertCommandsAfter(executor.getDefaultCmd(), "-s", getUdid(), "shell", "pm", "clear", packageName);346 executor.execute(cmd);347 }348 349 public String getApkPackageName(String apkFile) {350 // aapt dump badging <apk_file> | grep versionCode351 // aapt dump badging <apk_file> | grep versionName352 // output:353 // package: name='com.myfitnesspal.android' versionCode='9025' versionName='develop-QA' platformBuildVersionName='6.0-2704002'354 String[] cmd = CmdLine.insertCommandsAfter("aapt dump badging".split(" "), apkFile);355 List<String> output = executor.execute(cmd);356 // parse output command and get appropriate data357 String packageName = "";358 for (String line : output) {359 if (line.contains("versionCode") && line.contains("versionName")) {360 LOGGER.debug(line);361 String[] outputs = line.split("'");362 packageName = outputs[1]; //package363 }364 }365 return packageName;366 }367 368 public void uninstallApp(String packageName) {369 if (isNull())370 return;371 //adb -s UDID uninstall com.myfitnesspal.android372 String[] cmd = CmdLine.insertCommandsAfter(executor.getDefaultCmd(), "-s", getUdid(), "uninstall", packageName);373 executor.execute(cmd);374 }375 public void installApp(String packageName) {376 if (isNull())377 return;378 //adb -s UDID install com.myfitnesspal.android379 String[] cmd = CmdLine.insertCommandsAfter(executor.getDefaultCmd(), "-s", getUdid(), "install", "-r", packageName);380 executor.execute(cmd);381 }382 public synchronized void installAppSync(String packageName) {383 if (isNull())384 return;385 //adb -s UDID install com.myfitnesspal.android386 String[] cmd = CmdLine.insertCommandsAfter(executor.getDefaultCmd(), "-s", getUdid(), "install", "-r", packageName);387 executor.execute(cmd);388 }389 390 public void reinstallApp() {391 if (!Configuration.get(Parameter.MOBILE_PLATFORM_NAME).equalsIgnoreCase(SpecialKeywords.ANDROID)) {392 return;393 }394 if (isNull())395 return;396 397 String mobileApp = Configuration.get(Parameter.MOBILE_APP);398 String oldMobileApp = Configuration.get(Parameter.MOBILE_APP_PREUPGRADE);399 400 if (!oldMobileApp.isEmpty()) {401 //redefine strategy to do upgrade scenario402 R.CONFIG.put(Parameter.MOBILE_APP_UNINSTALL.getKey(), "true");403 R.CONFIG.put(Parameter.MOBILE_APP_INSTALL.getKey(), "true");404 }405 if (Configuration.getBoolean(Parameter.MOBILE_APP_UNINSTALL)) {406 // explicit reinstall the apk407 String[] apkVersions = getApkVersion(mobileApp); // Configuration.get(Parameter.MOBILE_APP)408 if (apkVersions != null) {409 String appPackage = apkVersions[0];410 String[] apkInstalledVersions = getInstalledApkVersion(appPackage);411 LOGGER.info("installed app: " + apkInstalledVersions[2] + "-" + apkInstalledVersions[1]);412 LOGGER.info("new app: " + apkVersions[2] + "-" + apkVersions[1]);413 if (apkVersions[1].equals(apkInstalledVersions[1]) && apkVersions[2].equals(apkInstalledVersions[2]) && oldMobileApp.isEmpty()) {414 LOGGER.info(415 "Skip application uninstall and cache cleanup as exactly the same version is already installed.");416 } else {417 uninstallApp(appPackage);418 clearAppData(appPackage);419 420 if (!oldMobileApp.isEmpty()) {421 LOGGER.info("Starting sync install operation for preupgrade app: " + oldMobileApp);422 installAppSync(oldMobileApp);423 }424 425 if (Configuration.getBoolean(Parameter.MOBILE_APP_INSTALL)) {426 // install application in single thread to fix issue with gray Google maps427 LOGGER.info("Starting sync install operation for app: " + mobileApp);428 installAppSync(mobileApp);429 }430 }431 }432 }433 }434 435 public String[] getInstalledApkVersion(String packageName) {436 //adb -s UDID shell dumpsys package PACKAGE | grep versionCode437 if (isNull())438 return null;439 String[] res = new String[3];440 res[0] = packageName;441 String[] cmd = CmdLine.insertCommandsAfter(executor.getDefaultCmd(), "-s", getUdid(), "shell", "dumpsys", "package", packageName);442 List<String> output = executor.execute(cmd);443 for (String line : output) {444 LOGGER.debug(line);445 if (line.contains("versionCode")) {446 // versionCode=17040000 targetSdk=25447 LOGGER.info("Line for parsing installed app: " + line);448 String[] outputs = line.split("=");449 String tmp = outputs[1]; //everything after '=' sign450 res[1] = tmp.split(" ")[0];451 }452 if (line.contains("versionName")) {453 // versionName=8.5.0454 LOGGER.info("Line for parsing installed app: " + line);455 String[] outputs = line.split("=");456 res[2] = outputs[1];457 }458 }459 if (res[0] == null && res[1] == null && res[2] == null) {460 return null;461 }462 return res;463 }464 465 public String[] getApkVersion(String apkFile) {466 // aapt dump badging <apk_file> | grep versionCode467 // aapt dump badging <apk_file> | grep versionName468 // output:469 // package: name='com.myfitnesspal.android' versionCode='9025' versionName='develop-QA' platformBuildVersionName='6.0-2704002'470 String[] cmd = CmdLine.insertCommandsAfter("aapt dump badging".split(" "), apkFile);471 List<String> output = executor.execute(cmd);472 // parse output command and get appropriate data473 String[] res = new String[3];474 for (String line : output) {475 if (line.contains("versionCode") && line.contains("versionName")) {476 LOGGER.debug(line);477 String[] outputs = line.split("'");478 res[0] = outputs[1]; //package479 res[1] = outputs[3]; //versionCode480 res[2] = outputs[5]; //versionName481 }482 }483 return res;484 }485 486 public void restartAppium() {487 if (!Configuration.getBoolean(Parameter.MOBILE_APPIUM_RESTART))488 return;489 490 if (isNull())491 return;492 stopAppium();493 startAppium();494 }495 // TODO: think about moving shutdown/startup scripts into external property and make it cross platform 496 public void stopAppium() {497 if (!Configuration.getBoolean(Parameter.MOBILE_APPIUM_RESTART))498 return;499 500 if (isNull())501 return;502 LOGGER.info("Stopping appium...");503 String cmdLine = Configuration.get(Parameter.MOBILE_TOOLS_HOME) + "/stopNodeAppium.sh";504 String[] cmd = CmdLine.insertCommandsAfter(cmdLine.split(" "), getUdid());505 List<String> output = executor.execute(cmd);506 for (String line : output) {507 LOGGER.debug(line);508 }509 }510 public void startAppium() {511 if (!Configuration.getBoolean(Parameter.MOBILE_APPIUM_RESTART))512 return;513 514 if (isNull())515 return;516 LOGGER.info("Starting appium...");517 String cmdLine = Configuration.get(Parameter.MOBILE_TOOLS_HOME) + "/startNodeAppium.sh";518 String[] cmd = CmdLine.insertCommandsAfter(cmdLine.split(" "), getUdid(), "&");519 List<String> output = executor.execute(cmd);520 for (String line : output) {521 LOGGER.debug(line);522 }523 AppiumStatus.waitStartup(getSeleniumServer(), 30);524 }525 526 public List<String> execute(String[] cmd) {527 return executor.execute(cmd);528 }529}...
insertCommandsAfter
Using AI Code Generation
1package com.qaprosoft.carina.core.foundation.utils.android.recorder.utils;2import java.io.File;3import java.io.IOException;4import java.util.ArrayList;5import java.util.Arrays;6import java.util.List;7import org.apache.commons.io.FileUtils;8import com.qaprosoft.carina.core.foundation.utils.android.recorder.exception.AndroidRecorderException;9public class CmdLine {10 public static final String CMD = "cmd.exe";11 public static final String CMD_PARAM = "/C";12 public static final String CMD_PARAM_SPLITTER = " ";13 public static void insertCommandsAfter(String[] commands, String commandToInsert, String commandToSearch)14 throws AndroidRecorderException {15 List<String> commandsList = new ArrayList<String>(Arrays.asList(commands));16 int index = commandsList.indexOf(commandToSearch);17 if (index == -1) {18 throw new AndroidRecorderException(19 "Command to search: " + commandToSearch + " was not found in commands list: " + commandsList);20 }21 commandsList.add(index + 1, commandToInsert);22 commands = commandsList.toArray(new String[commandsList.size()]);23 }24 public static void insertCommandsBefore(String[] commands, String commandToInsert, String commandToSearch)25 throws AndroidRecorderException {26 List<String> commandsList = new ArrayList<String>(Arrays.asList(commands));27 int index = commandsList.indexOf(commandToSearch);28 if (index == -1) {29 throw new AndroidRecorderException(30 "Command to search: " + commandToSearch + " was not found in commands list: " + commandsList);31 }32 commandsList.add(index, commandToInsert);33 commands = commandsList.toArray(new String[commandsList.size()]);34 }35 public static void insertCommandsBefore(String[] commands, String[] commandsToInsert, String commandToSearch)36 throws AndroidRecorderException {37 List<String> commandsList = new ArrayList<String>(Arrays.asList(commands));38 int index = commandsList.indexOf(commandToSearch);39 if (index == -1) {40 throw new AndroidRecorderException(41 "Command to search: " + commandToSearch + " was not found in commands list: " + commandsList);42 }43 commandsList.addAll(index, Arrays.asList(commandsToInsert));44 commands = commandsList.toArray(new String[commandsList.size()]);45 }46 public static void insertCommandsAfter(String[] commands, String[] commandsToInsert, String commandToSearch)
insertCommandsAfter
Using AI Code Generation
1import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.CmdLine;2public class CmdLineExample {3 public static void main(String[] args) {4 CmdLine cmdLine = new CmdLine();5 cmdLine.insertCommandsAfter("adb shell input tap 100 100", "adb shell input tap 200 200");6 cmdLine.printCommands();7 }8}9[INFO] --- exec-maven-plugin:1.6.0:java (default-cli) @ carina-recorder ---10import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.CmdLine;11public class CmdLineExample {12 public static void main(String[] args) {13 CmdLine cmdLine = new CmdLine();14 cmdLine.insertCommandsAfter("adb shell input tap 100 100", "adb shell input tap 200 200");15 cmdLine.printCommands();16 }17}18[INFO] --- exec-maven-plugin:1.6.0:java (default-cli) @ carina-recorder ---
insertCommandsAfter
Using AI Code Generation
1import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.CmdLine2import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.Command3import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.CommandType4import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.CommandUtils5import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.CommandUtils.getCommand6import java.util.ArrayList7import java.util.List8import java.util.regex.Pattern9import java.util.regex.Matcher10import java.util.stream.Collectors11import java.util.stream.Stream12def insertCommandsAfter(List<Command> commands, String commandName, List<Command> commandsToInsert) {13 List<Command> newCommands = new ArrayList<Command>()14 int index = 0;15 for (Command command : commands) {16 newCommands.add(command)17 if (command.getName().equals(commandName)) {18 newCommands.addAll(commandsToInsert)19 }20 }21}22def insertCommandAfter(List<Command> commands, String commandName, Command commandToInsert) {23 List<Command> newCommands = new ArrayList<Command>()24 int index = 0;25 for (Command command : commands) {26 newCommands.add(command)27 if (command.getName().equals(commandName)) {28 newCommands.add(commandToInsert)29 }30 }31}32def insertCommandBefore(List<Command> commands, String commandName, Command commandToInsert) {33 List<Command> newCommands = new ArrayList<Command>()34 int index = 0;35 for (Command command : commands) {36 if (command.getName().equals(commandName)) {37 newCommands.add(commandToInsert)38 }39 newCommands.add(command)40 }41}42def insertCommandsBefore(List<Command> commands, String commandName, List<Command> commandsToInsert) {43 List<Command> newCommands = new ArrayList<Command>()44 int index = 0;45 for (Command command : commands) {46 if (command.getName().equals(commandName)) {47 newCommands.addAll(commandsToInsert)48 }
insertCommandsAfter
Using AI Code Generation
1import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.CmdLine2def cmdLine = new CmdLine()3cmdLine.insertCommandsAfter("adb shell", "adb shell", "echo 'test'", "echo 'test'")4cmdLine.execute()5import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.CmdLine6def cmdLine = new CmdLine()7cmdLine.insertCommandsBefore("adb shell", "adb shell", "echo 'test'", "echo 'test'")8cmdLine.execute()9import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.CmdLine10def cmdLine = new CmdLine()11cmdLine.insertCommandsAfter("adb shell", "adb shell", "echo 'test'", "echo 'test'")12cmdLine.execute()13import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.CmdLine14def cmdLine = new CmdLine()15cmdLine.insertCommandsBefore("adb shell", "adb shell", "echo 'test'", "echo 'test'")16cmdLine.execute()17import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.CmdLine18def cmdLine = new CmdLine()19cmdLine.insertCommandsAfter("adb shell", "adb shell", "echo 'test'", "echo 'test'")20cmdLine.execute()21import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.CmdLine22def cmdLine = new CmdLine()23cmdLine.insertCommandsBefore("adb shell", "adb shell", "echo 'test'", "echo 'test'")24cmdLine.execute()25import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.CmdLine26def cmdLine = new CmdLine()
insertCommandsAfter
Using AI Code Generation
1CmdLine.insertCommandsAfter("adb shell uiautomator dump", "adb shell uiautomator dump /sdcard/window_dump.xml");2import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.CmdLine;3import org.apache.log4j.Logger;4public class Test {5 private static final Logger LOGGER = Logger.getLogger(Test.class);6 public static void main(String[] args) {7 CmdLine.insertCommandsAfter("adb shell uiautomator dump", "adb shell uiautomator dump /sdcard/window_dump.xml");8 }9}10[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ carina-utils ---11[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ carina-utils ---12[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ carina-utils ---13[INFO] --- maven-surefire-plugin:2.19.1:test (default-test) @ carina-utils ---
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!!