How to use setProxy method of com.qaprosoft.carina.core.foundation.webdriver.device.Device class

Best Carina code snippet using com.qaprosoft.carina.core.foundation.webdriver.device.Device.setProxy

Source:Device.java Github

copy

Full Screen

...71 setOs(remoteDevice.getOs());72 setOsVersion(remoteDevice.getOsVersion());73 setUdid(remoteDevice.getUdid());74 setRemoteURL(remoteDevice.getRemoteURL());75 setProxyPort(remoteDevice.getProxyPort());76 }77 public Device(Capabilities capabilities) {78 // 1. read from CONFIG and specify if any: capabilities.deviceName, capabilities.device (browserstack notation)79 // 2. read from capabilities object and set if if it is not null80 String deviceName = R.CONFIG.get(SpecialKeywords.MOBILE_DEVICE_NAME);81 if (!R.CONFIG.get(SpecialKeywords.MOBILE_DEVICE_BROWSERSTACK_NAME).isEmpty()) {82 deviceName = R.CONFIG.get(SpecialKeywords.MOBILE_DEVICE_BROWSERSTACK_NAME);83 }84 if (capabilities.getCapability("deviceName") != null) {85 deviceName = capabilities.getCapability("deviceName").toString();86 }87 if (capabilities.getCapability("deviceModel") != null) {88 // deviceModel is returned from capabilities with name of device for local appium runs89 deviceName = capabilities.getCapability("deviceModel").toString();90 }91 setName(deviceName);92 // TODO: should we register default device type as phone?93 String deviceType = SpecialKeywords.PHONE;94 if (!R.CONFIG.get(SpecialKeywords.MOBILE_DEVICE_TYPE).isEmpty()) {95 deviceType = R.CONFIG.get(SpecialKeywords.MOBILE_DEVICE_TYPE);96 }97 if (capabilities.getCapability("deviceType") != null) {98 deviceType = capabilities.getCapability("deviceType").toString();99 }100 setType(deviceType);101 setOs(Configuration.getPlatform());102 String platformVersion = R.CONFIG.get(SpecialKeywords.MOBILE_DEVICE_PLATFORM_VERSION);103 if (!R.CONFIG.get(SpecialKeywords.MOBILE_DEVICE_BROWSERSTACK_PLATFORM_VERSION).isEmpty()) {104 platformVersion = R.CONFIG.get(SpecialKeywords.MOBILE_DEVICE_BROWSERSTACK_PLATFORM_VERSION);105 }106 if (capabilities.getCapability("platformVersion") != null) {107 platformVersion = capabilities.getCapability("platformVersion").toString();108 }109 setOsVersion(platformVersion);110 String deviceUdid = R.CONFIG.get(SpecialKeywords.MOBILE_DEVICE_UDID);111 if (capabilities.getCapability("udid") != null) {112 deviceUdid = capabilities.getCapability("udid").toString();113 }114 setUdid(deviceUdid);115 116 String proxyPort = R.CONFIG.get(SpecialKeywords.MOBILE_PROXY_PORT);117 if (capabilities.getCapability("proxy_port") != null) {118 proxyPort = capabilities.getCapability("proxy_port").toString();119 }120 setProxyPort(proxyPort);121 }122 public boolean isPhone() {123 return getType().equalsIgnoreCase(SpecialKeywords.PHONE);124 }125 public boolean isTablet() {126 return getType().equalsIgnoreCase(SpecialKeywords.TABLET);127 }128 public boolean isTv() {129 return getType().equalsIgnoreCase(SpecialKeywords.TV);130 }131 public Type getDeviceType() {132 if (isNull()) {133 // if no device initialized it means that desktop UI automation is used134 return Type.DESKTOP;135 }136 if (getOs().equalsIgnoreCase(SpecialKeywords.ANDROID)) {137 if (isTablet()) {138 return Type.ANDROID_TABLET;139 }140 if (isTv()) {141 return Type.ANDROID_TV;142 }143 return Type.ANDROID_PHONE;144 } else if (getOs().equalsIgnoreCase(SpecialKeywords.IOS) || getOs().equalsIgnoreCase(SpecialKeywords.MAC)) {145 if (isTablet()) {146 return Type.IOS_TABLET;147 }148 return Type.IOS_PHONE;149 }150 throw new RuntimeException("Incorrect driver type. Please, check config file for " + toString());151 }152 public String toString() {153 return String.format("name: %s; type: %s; os: %s; osVersion: %s; udid: %s; remoteURL: %s", getName(),154 getType(), getOs(), getOsVersion(), getUdid(), getRemoteURL());155 }156 public boolean isNull() {157 if (getName() == null) {158 return true;159 }160 return getName().isEmpty();161 }162 public void connectRemote() {163 if (isNull())164 return;165 LOGGER.info("adb connect " + getRemoteURL());166 String[] cmd = CmdLine.insertCommandsAfter(executor.getDefaultCmd(), "connect", getRemoteURL());167 executor.execute(cmd);168 CommonUtils.pause(1);169 String[] cmd2 = CmdLine.insertCommandsAfter(executor.getDefaultCmd(), "devices");170 executor.execute(cmd2);171 // TODO: add several attempt of connect until device appear among connected devices172 // quick workaround to do double connect...173 executor.execute(cmd);174 executor.execute(cmd2);175 }176 public void disconnectRemote() {177 if (isNull())178 return;179 // [VD] No need to do adb command as stopping STF session do it correctly180 // in new STF we have huge problems with sessions disconnect181 LOGGER.info("adb disconnect " + getRemoteURL());182 String[] cmd = CmdLine.insertCommandsAfter(executor.getDefaultCmd(), "disconnect", getRemoteURL());183 executor.execute(cmd);184 }185 @Deprecated186 public void dropFile(String pathToFile) {187 if (this.isNull())188 return;189 String[] cmd = CmdLine.insertCommandsAfter(executor.getDefaultCmd(), "-s", getAdbName(), "shell", "rm", pathToFile);190 executor.execute(cmd);191 }192 public String getFullPackageByName(final String name) {193 List<String> packagesList = getInstalledPackages();194 LOGGER.info("Found packages: ".concat(packagesList.toString()));195 String resultPackage = null;196 for (String packageStr : packagesList) {197 if (packageStr.matches(String.format(".*%s.*", name))) {198 LOGGER.info("Package was found: ".concat(packageStr));199 resultPackage = packageStr;200 break;201 }202 }203 if (null == resultPackage) {204 LOGGER.info("Package wasn't found using following name: ".concat(name));205 resultPackage = "not found";206 }207 return resultPackage;208 }209 public List<String> getInstalledPackages() {210 String deviceUdid = getAdbName();211 LOGGER.info("Device udid: ".concat(deviceUdid));212 String[] cmd = CmdLine.createPlatformDependentCommandLine("adb", "-s", deviceUdid, "shell", "pm", "list", "packages");213 LOGGER.info("Following cmd will be executed: " + Arrays.toString(cmd));214 List<String> packagesList = executor.execute(cmd);215 return packagesList;216 }217 public boolean isAppInstall(final String packageName) {218 return !getFullPackageByName(packageName).contains("not found");219 }220 @Deprecated221 public void pullFile(String pathFrom, String pathTo) {222 if (isNull())223 return;224 String[] cmd = CmdLine.insertCommandsAfter(executor.getDefaultCmd(), "-s", getAdbName(), "pull", pathFrom, pathTo);225 executor.execute(cmd);226 }227 private Boolean getScreenState() {228 // determine current screen status229 // adb -s <udid> shell dumpsys input_method | find "mScreenOn"230 String[] cmd = CmdLine.insertCommandsAfter(executor.getDefaultCmd(), "-s", getAdbName(), "shell", "dumpsys",231 "input_method");232 List<String> output = executor.execute(cmd);233 Boolean screenState = null;234 String line;235 Iterator<String> itr = output.iterator();236 while (itr.hasNext()) {237 // mScreenOn - default value for the most of Android devices238 // mInteractive - for Nexuses239 line = itr.next();240 if (line.contains("mScreenOn=true") || line.contains("mInteractive=true")) {241 screenState = true;242 break;243 }244 if (line.contains("mScreenOn=false") || line.contains("mInteractive=false")) {245 screenState = false;246 break;247 }248 }249 if (screenState == null) {250 LOGGER.error(getUdid()251 + ": Unable to determine existing device screen state!");252 return screenState; // no actions required if state is not recognized.253 }254 if (screenState) {255 LOGGER.info(getUdid() + ": screen is ON");256 }257 if (!screenState) {258 LOGGER.info(getUdid() + ": screen is OFF");259 }260 return screenState;261 }262 public void screenOff() {263 if (!Configuration.getPlatform().equalsIgnoreCase(SpecialKeywords.ANDROID)) {264 return;265 }266 if (!Configuration.getBoolean(Parameter.MOBILE_SCREEN_SWITCHER)) {267 return;268 }269 if (isNull())270 return;271 Boolean screenState = getScreenState();272 if (screenState == null) {273 return;274 }275 if (screenState) {276 String[] cmd = CmdLine.insertCommandsAfter(executor.getDefaultCmd(), "-s", getAdbName(), "shell", "input",277 "keyevent", "26");278 executor.execute(cmd);279 CommonUtils.pause(5);280 screenState = getScreenState();281 if (screenState) {282 LOGGER.error(getUdid() + ": screen is still ON!");283 }284 if (!screenState) {285 LOGGER.info(getUdid() + ": screen turned off.");286 }287 }288 }289 public void screenOn() {290 if (!Configuration.getPlatform().equalsIgnoreCase(SpecialKeywords.ANDROID)) {291 return;292 }293 if (!Configuration.getBoolean(Parameter.MOBILE_SCREEN_SWITCHER)) {294 return;295 }296 if (isNull())297 return;298 Boolean screenState = getScreenState();299 if (screenState == null) {300 return;301 }302 if (!screenState) {303 String[] cmd = CmdLine.insertCommandsAfter(executor.getDefaultCmd(), "-s", getAdbName(), "shell",304 "input", "keyevent", "26");305 executor.execute(cmd);306 CommonUtils.pause(5);307 // verify that screen is Off now308 screenState = getScreenState();309 if (!screenState) {310 LOGGER.error(getUdid() + ": screen is still OFF!");311 }312 if (screenState) {313 LOGGER.info(getUdid() + ": screen turned on.");314 }315 }316 }317 public void pressKey(int key) {318 if (isNull())319 return;320 String[] cmd = CmdLine.insertCommandsAfter(executor.getDefaultCmd(), "-s", getAdbName(), "shell", "input",321 "keyevent", String.valueOf(key));322 executor.execute(cmd);323 }324 public void pause(long timeout) {325 CommonUtils.pause(timeout);326 }327 public void clearAppData() {328 clearAppData(Configuration.getMobileApp());329 }330 public void clearAppData(String app) {331 if (!Configuration.getPlatform().equalsIgnoreCase(SpecialKeywords.ANDROID)) {332 return;333 }334 if (isNull())335 return;336 // adb -s UDID shell pm clear com.myfitnesspal.android337 String packageName = getApkPackageName(app);338 String[] cmd = CmdLine.insertCommandsAfter(executor.getDefaultCmd(), "-s", getAdbName(), "shell", "pm", "clear", packageName);339 executor.execute(cmd);340 }341 public String getApkPackageName(String apkFile) {342 // aapt dump badging <apk_file> | grep versionCode343 // aapt dump badging <apk_file> | grep versionName344 // output:345 // package: name='com.myfitnesspal.android' versionCode='9025' versionName='develop-QA' platformBuildVersionName='6.0-2704002'346 String packageName = "";347 String[] cmd = CmdLine.insertCommandsAfter("aapt dump badging".split(" "), apkFile);348 List<String> output = executor.execute(cmd);349 // parse output command and get appropriate data350 for (String line : output) {351 if (line.contains("versionCode") && line.contains("versionName")) {352 LOGGER.debug(line);353 String[] outputs = line.split("'");354 packageName = outputs[1]; // package355 }356 }357 return packageName;358 }359 public void uninstallApp(String packageName) {360 if (isNull())361 return;362 // adb -s UDID uninstall com.myfitnesspal.android363 String[] cmd = CmdLine.insertCommandsAfter(executor.getDefaultCmd(), "-s", getAdbName(), "uninstall", packageName);364 executor.execute(cmd);365 }366 public void installApp(String apkPath) {367 if (isNull())368 return;369 // adb -s UDID install com.myfitnesspal.android370 String[] cmd = CmdLine.insertCommandsAfter(executor.getDefaultCmd(), "-s", getAdbName(), "install", "-r", apkPath);371 executor.execute(cmd);372 }373 public synchronized void installAppSync(String apkPath) {374 if (isNull())375 return;376 // adb -s UDID install com.myfitnesspal.android377 String[] cmd = CmdLine.insertCommandsAfter(executor.getDefaultCmd(), "-s", getAdbName(), "install", "-r", apkPath);378 executor.execute(cmd);379 }380 /*381 * public void reinstallApp() {382 * if (!Configuration.getPlatform().equalsIgnoreCase(SpecialKeywords.ANDROID)) {383 * return;384 * }385 * 386 * if (isNull())387 * return;388 * 389 * String mobileApp = Configuration.getMobileApp();390 * String oldMobileApp = Configuration.get(Parameter.MOBILE_APP_PREUPGRADE);391 * 392 * if (!oldMobileApp.isEmpty()) {393 * //redefine strategy to do upgrade scenario394 * R.CONFIG.put(Parameter.MOBILE_APP_UNINSTALL.getKey(), "true");395 * R.CONFIG.put(Parameter.MOBILE_APP_INSTALL.getKey(), "true");396 * }397 * 398 * if (Configuration.getBoolean(Parameter.MOBILE_APP_UNINSTALL)) {399 * // explicit reinstall the apk400 * String[] apkVersions = getApkVersion(mobileApp);401 * if (apkVersions != null) {402 * String appPackage = apkVersions[0];403 * 404 * String[] apkInstalledVersions = getInstalledApkVersion(appPackage);405 * 406 * LOGGER.info("installed app: " + apkInstalledVersions[2] + "-" + apkInstalledVersions[1]);407 * LOGGER.info("new app: " + apkVersions[2] + "-" + apkVersions[1]);408 * 409 * if (apkVersions[1].equals(apkInstalledVersions[1]) && apkVersions[2].equals(apkInstalledVersions[2]) && oldMobileApp.isEmpty()) {410 * LOGGER.info(411 * "Skip application uninstall and cache cleanup as exactly the same version is already installed.");412 * } else {413 * uninstallApp(appPackage);414 * clearAppData(appPackage);415 * isAppInstalled = false;416 * if (!oldMobileApp.isEmpty()) {417 * LOGGER.info("Starting sync install operation for preupgrade app: " + oldMobileApp);418 * installAppSync(oldMobileApp);419 * }420 * 421 * if (Configuration.getBoolean(Parameter.MOBILE_APP_INSTALL)) {422 * // install application in single thread to fix issue with gray Google maps423 * LOGGER.info("Starting sync install operation for app: " + mobileApp);424 * installAppSync(mobileApp);425 * }426 * }427 * }428 * } else if (Configuration.getBoolean(Parameter.MOBILE_APP_INSTALL) && !isAppInstalled) {429 * LOGGER.info("Starting install operation for app: " + mobileApp);430 * installApp(mobileApp);431 * isAppInstalled = true;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", getAdbName(), "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 public String[] getApkVersion(String apkFile) {465 // aapt dump badging <apk_file> | grep versionCode466 // aapt dump badging <apk_file> | grep versionName467 // output:468 // package: name='com.myfitnesspal.android' versionCode='9025' versionName='develop-QA' platformBuildVersionName='6.0-2704002'469 String[] res = new String[3];470 res[0] = "";471 res[1] = "";472 res[2] = "";473 String[] cmd = CmdLine.insertCommandsAfter("aapt dump badging".split(" "), apkFile);474 List<String> output = executor.execute(cmd);475 // parse output command and get appropriate data476 for (String line : output) {477 if (line.contains("versionCode") && line.contains("versionName")) {478 LOGGER.debug(line);479 String[] outputs = line.split("'");480 res[0] = outputs[1]; // package481 res[1] = outputs[3]; // versionCode482 res[2] = outputs[5]; // versionName483 }484 }485 return res;486 }487 public List<String> execute(String[] cmd) {488 return executor.execute(cmd);489 }490 public void setProxy(final String host, final String port, final String ssid, final String password) {491 if (!getOs().equalsIgnoreCase(DeviceType.Type.ANDROID_PHONE.getFamily())) {492 LOGGER.error("Proxy configuration is available for Android ONLY");493 throw new RuntimeException("Proxy configuration is available for Android ONLY");494 }495 if (!isAppInstall(SpecialKeywords.PROXY_SETTER_PACKAGE)) {496 final String proxySetterFileName = "./proxy-setter-temp.apk";497 File targetFile = new File(proxySetterFileName);498 downloadFileFromJar(SpecialKeywords.PROXY_SETTER_RES_PATH, targetFile);499 installApp(proxySetterFileName);500 }501 String deviceUdid = getAdbName();502 LOGGER.info("Device udid: ".concat(deviceUdid));503 String[] cmd = CmdLine.createPlatformDependentCommandLine("adb", "-s", deviceUdid, "shell", "am", "start", "-n",504 "tk.elevenk.proxysetter/.MainActivity", "-e", "host", host, "-e", "port", port, "-e", "ssid", ssid, "-e", "key", password);...

Full Screen

Full Screen

setProxy

Using AI Code Generation

copy

Full Screen

1import com.qaprosoft.carina.core.foundation.webdriver.device.Device;2import com.qaprosoft.carina.core.foundation.webdriver.device.DevicePool;3import org.openqa.selenium.Proxy;4import org.openqa.selenium.remote.CapabilityType;5import org.openqa.selenium.remote.DesiredCapabilities;6import org.testng.annotations.Test;7public class ProxyTest {8 public void testProxy() {9 Device device = DevicePool.getDevice();10 Proxy proxy = new Proxy();11 proxy.setHttpProxy("localhost:8888");12 DesiredCapabilities capabilities = new DesiredCapabilities();13 capabilities.setCapability(CapabilityType.PROXY, proxy);14 device.setProxy(capabilities);15 }16}17import com.qaprosoft.carina.core.foundation.webdriver.device.Device;18import com.qaprosoft.carina.core.foundation.webdriver.device.DevicePool;19import org.openqa.selenium.Proxy;20import org.openqa.selenium.remote.CapabilityType;21import org.openqa.selenium.remote.DesiredCapabilities;22import org.testng.annotations.Test;23public class ProxyTest {24 public void testProxy() {25 Device device = DevicePool.getDevice();26 Proxy proxy = new Proxy();27 proxy.setHttpProxy("localhost:8888");28 DesiredCapabilities capabilities = new DesiredCapabilities();29 capabilities.setCapability(CapabilityType.PROXY, proxy);30 device.setProxy(capabilities);31 }32}33import com.qaprosoft.carina.core.foundation.webdriver.device.Device;34import com.qaprosoft.carina.core.foundation.webdriver.device.DevicePool;35import org.openqa.selenium.Proxy;36import org.openqa.selenium.remote.CapabilityType;37import org.openqa.selenium.remote.DesiredCapabilities;38import org.testng.annotations.Test;39public class ProxyTest {40 public void testProxy() {41 Device device = DevicePool.getDevice();42 Proxy proxy = new Proxy();43 proxy.setHttpProxy("localhost:8888");44 DesiredCapabilities capabilities = new DesiredCapabilities();45 capabilities.setCapability(CapabilityType.PROXY, proxy);46 device.setProxy(capabilities);47 }48}49import com.qaprosoft.carina.core.foundation.webdriver.device.Device;50import com.qaprosoft.carina.core.foundation.webdriver.device.DevicePool;51import org.openqa.selenium.Proxy;52import org.openqa.selenium.remote.Cap

Full Screen

Full Screen

setProxy

Using AI Code Generation

copy

Full Screen

1Device.setProxy("proxy.qaprosoft.com", 8080);2Device.setProxy("proxy.qaprosoft.com", 8080);3Device.setProxy("proxy.qaprosoft.com", 8080);4Device.setProxy("proxy.qaprosoft.com", 8080);5Device.setProxy("proxy.qaprosoft.com", 8080);6Device.setProxy("proxy.qaprosoft.com", 8080);7Device.setProxy("proxy.qaprosoft.com", 8080);8Device.setProxy("proxy.qaprosoft.com", 8080);9Device.setProxy("proxy.qaprosoft.com", 8080);10Device.setProxy("proxy.qaprosoft.com", 8080);11Device.setProxy("proxy.qaprosoft.com", 8080);12Device.setProxy("proxy.qaprosoft.com", 8080);13Device.setProxy("proxy.qaprosoft.com", 8080);14Device.setProxy("proxy.qaprosoft.com", 8080);15Device.setProxy("proxy.qaprosoft

Full Screen

Full Screen

setProxy

Using AI Code Generation

copy

Full Screen

1Device.setProxy("localhost", 8888);2Device.setProxy("localhost", 8888);3Device.setProxy("localhost", 8888);4Device.setProxy("localhost", 8888);5Device.setProxy("localhost", 8888);6Device.setProxy("localhost", 8888);7Device.setProxy("localhost", 8888);8Device.setProxy("localhost", 8888);

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful