Best Carina code snippet using com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.CmdLine
Source:Device.java
...8import com.qaprosoft.carina.core.foundation.utils.Configuration.Parameter;9import com.qaprosoft.carina.core.foundation.utils.SpecialKeywords;10import com.qaprosoft.carina.core.foundation.utils.android.recorder.exception.ExecutorException;11import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.AdbExecutor;12import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.CmdLine;13import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.Platform;14import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.ProcessBuilderExecutor;15import com.qaprosoft.carina.core.foundation.utils.factory.DeviceType.Type;16import com.qaprosoft.carina.core.foundation.webdriver.appium.status.AppiumStatus;17//Motorola|ANDROID|4.4|T01130FJAD|http://localhost:4725/wd/hub;Samsung_S4|ANDROID|4.4.2|5ece160b|http://localhost:4729/wd/hub;18public class Device19{20 private static final Logger LOGGER = Logger.getLogger(Device.class);21 private String name;22 private String type;23 private String os;24 private String osVersion;25 private String udid;26 private String seleniumServer;27 private String testId;28 29 AdbExecutor executor = new AdbExecutor();30 public Device(String args)31 {32 // Samsung_S4|ANDROID|4.4.2|5ece160b|4729|4730|http://localhost:4725/wd/hub33 LOGGER.debug("mobile_device_args: " + args);34 args = args.replaceAll("|", "|");35 LOGGER.debug("mobile_device_args: " + args);36 String[] params = args.split("\\|");37 // TODO: organize verification onto the params count38 this.name = params[0];39 LOGGER.debug("mobile_device_name: " + name);40 this.type = params[1];41 LOGGER.debug("mobile_device_type: " + params[1]);42 this.os = params[2];43 this.osVersion = params[3];44 this.udid = params[4];45 this.seleniumServer = params[5];46 }47 48 public Device()49 {50 this(null, null, null, null, null, null);51 }52 public Device(String name, String type, String os, String osVersion, String udid, String seleniumServer)53 {54 this.name = name;55 this.type = type;56 this.os = os;57 this.osVersion = osVersion;58 this.udid = udid;59 this.seleniumServer = seleniumServer;60 }61 public String getName()62 {63 return name;64 }65 public void setName(String name)66 {67 this.name = name;68 }69 public String getOs()70 {71 return os;72 }73 public void setOs(String os)74 {75 this.os = os;76 }77 public String getOsVersion()78 {79 return osVersion;80 }81 public void setOsVersion(String osVersion)82 {83 this.osVersion = osVersion;84 }85 public String getUdid()86 {87 return udid;88 }89 public void setUdid(String udid)90 {91 this.udid = udid;92 }93 public String getSeleniumServer()94 {95 return seleniumServer;96 }97 public void setSeleniumServer(String seleniumServer)98 {99 this.seleniumServer = seleniumServer;100 }101 public boolean isPhone()102 {103 return type.equalsIgnoreCase(SpecialKeywords.PHONE);104 }105 public boolean isTablet()106 {107 return type.equalsIgnoreCase(SpecialKeywords.TABLET);108 }109 public boolean isTv()110 {111 return type.equalsIgnoreCase(SpecialKeywords.TV);112 }113 public String getTestId()114 {115 return testId;116 }117 public void setTestId(String testId)118 {119 this.testId = testId;120 }121 public Type getType()122 {123 if (os.equalsIgnoreCase(SpecialKeywords.ANDROID))124 {125 if (isTablet())126 {127 return Type.ANDROID_TABLET;128 }129 if (isTv())130 {131 return Type.ANDROID_TV;132 }133 return Type.ANDROID_PHONE;134 } 135 else if (os.equalsIgnoreCase(SpecialKeywords.IOS))136 {137 if (isTablet())138 {139 return Type.IOS_TABLET;140 }141 return Type.IOS_PHONE;142 }143 throw new RuntimeException("Incorrect driver type. Please, check config file.");144 }145 146 public boolean isNull() {147 if (name == null || seleniumServer == null) {148 return true;149 }150 return name.isEmpty() || seleniumServer.isEmpty();151 }152 public int startRecording(String pathToFile) {153 if (!Configuration.getBoolean(Parameter.VIDEO_RECORDING)) {154 return -1;155 }156 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}...
CmdLine
Using AI Code Generation
1import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.CmdLine;2import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.CmdLineException;3import com.qaprosoft.carina.core.foundation.utils.android.recorder.Recorder;4import com.qaprosoft.carina.core.foundation.utils.android.AndroidDevice;5import com.qaprosoft.carina.core.foundation.utils.android.AndroidUtils;6import com.qaprosoft.carina.core.foundation.utils.android.DevicePool;7import com.qaprosoft.carina.core.foundation.utils.android.DeviceType;8import com.qaprosoft.carina.core.foundation.utils.android.DeviceUtils;9import com.qaprosoft.carina.core.foundation.utils.android.IDevice;10import com.qaprosoft.carina.core.foundation.utils.android.IDevicePool;11import com.qaprosoft.carina.core.foundation.utils.android.IDevicePool;12import com.qaprosoft.carina.core.foundation.utils.android.IDevicePool;13import com.qaprosoft.carina.core.foundation.utils.android.IDevicePool;14import com.qaprosoft.carina.core.foundation.utils.android.IDevicePool;15import com.qaprosoft.carina.core.foundation.utils.android.IDevicePool;
CmdLine
Using AI Code Generation
1import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.CmdLine;2import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.CmdLine.Command;3import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.CmdLine;4import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.CmdLine.Command;5import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.CmdLine;6import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.CmdLine.Command;7import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.CmdLine;8import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.CmdLine.Command;9import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.CmdLine;10import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.CmdLine.Command;11import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.CmdLine;12import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.CmdLine.Command;13import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.CmdLine;14import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.CmdLine.Command;15import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.CmdLine;16import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.CmdLine.Command;17import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.CmdLine;18import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.CmdLine.Command
CmdLine
Using AI Code Generation
1import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.CmdLine;2import java.util.HashMap;3public class CmdLineTest {4 public static void main(String[] args) {5 String[] cmdArgs = new String[] {6 };7 HashMap<String, String> options = CmdLine.parse(cmdArgs);8 if (options.containsKey("d")) {9 System.out.println("Device Name: " + options.get("d"));10 }11 if (options.containsKey("v")) {12 System.out.println("Device Version: " + options.get("v"));13 }14 if (options.containsKey("p")) {15 System.out.println("Platform Name: " + options.get("p"));16 }17 if (options.containsKey("a")) {18 System.out.println("App Path: " + options.get("a"));19 }20 if (options.containsKey("l")) {21 System.out.println("Log Path: " + options.get("l"));22 }23 }24}
CmdLine
Using AI Code Generation
1import org.apache.commons.lang3.StringUtils;2import org.apache.log4j.Logger;3import org.openqa.selenium.remote.DesiredCapabilities;4import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.CmdLine;5import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.CmdLine.CmdLineException;6import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.CmdLine.CmdLineParser;7import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.CmdLine.Option;8import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.CmdLine.OptionException;9import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.CmdLine.ParserException;10import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.CmdLine.UnknownOptionException;11import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.CmdLine.UnrecognizedOptionException;12import com.qaprosoft.carina.core.foundation.utils.android.recorder.utils.CmdLine.ValueException;13public class AndroidRecorder {14 private static final Logger LOGGER = Logger.getLogger(AndroidRecorder.class);15 private static final String DEFAULT_PORT = "4723";16 private static final String DEFAULT_APK_PATH = "src/test/resources/";17 private static final String DEFAULT_APK_NAME = "app-debug.apk";18 private static final String DEFAULT_APP_ACTIVITY = "com.qaprosoft.carina.demo.gui.activities.MainActivity";19 private static final String DEFAULT_APP_PACKAGE = "com.qaprosoft.carina.demo";
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!!