Best Cerberus-source code snippet using org.cerberus.crud.entity.TestCaseExecution.getRobotObj
Source:RobotServerService.java
...227 additionalFinalCapabilities.add(factoryRobotCapability.create(0, "", cap.getKey(), cap.getValue().toString()));228 }229 // Init inputCapabilities and set it from Robot values.230 List<RobotCapability> inputCapabilities = new ArrayList<>();231 if (tCExecution.getRobotObj() != null) {232 inputCapabilities = tCExecution.getRobotObj().getCapabilities();233 }234 tCExecution.addFileList(recorderService.recordCapabilities(tCExecution, inputCapabilities, additionalFinalCapabilities));235 } catch (Exception ex) {236 LOG.error("Exception Saving Robot Caps {} Exception: {}", tCExecution.getId(), ex.toString(), ex);237 }238 // SetUp Proxy239 String hubUrl = StringUtil.cleanHostURL(RobotServerService.getBaseUrl(StringUtil.formatURLCredential(240 tCExecution.getSession().getHostUser(),241 tCExecution.getSession().getHostPassword(), session.getHost()),242 session.getPort())) + "/wd/hub";243 LOG.debug("Hub URL :{}", hubUrl);244 URL url = new URL(hubUrl);245 HttpCommandExecutor executor = null;246 boolean isProxy = proxyService.useProxy(hubUrl, system);247 Factory factory = new OkHttpClient.Factory();248 // Timeout Management249 int robotTimeout = parameterService.getParameterIntegerByKey("cerberus_robot_timeout", system, 60000);250 Duration rbtTimeOut = Duration.ofMillis(robotTimeout);251 factory.builder().connectionTimeout(rbtTimeOut);252 if (isProxy) {253 // Proxy Management254 String proxyHost = parameterService.getParameterStringByKey("cerberus_proxy_host", system, DEFAULT_PROXY_HOST);255 int proxyPort = parameterService.getParameterIntegerByKey("cerberus_proxy_port", system, DEFAULT_PROXY_PORT);256 java.net.Proxy myproxy = new java.net.Proxy(java.net.Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));257 if (parameterService.getParameterBooleanByKey("cerberus_proxyauthentification_active", system, DEFAULT_PROXYAUTHENT_ACTIVATE)) {258 String proxyUser = parameterService.getParameterStringByKey("cerberus_proxyauthentification_user", system, DEFAULT_PROXYAUTHENT_USER);259 String proxyPassword = parameterService.getParameterStringByKey("cerberus_proxyauthentification_password", system, DEFAULT_PROXYAUTHENT_PASSWORD);260 // TODO delete if comment bellow has no impact on the non reg campaign261/*262 Authenticator proxyAuthenticator = new Authenticator() {263 public Request authenticate(Route route, Response response) throws IOException {264 String credential = Credentials.basic(proxyUser, proxyPassword);265 return response.request().newBuilder()266 .header("Proxy-Authorization", credential)267 .build();268 }269 };270*/271 }272 factory.builder().proxy(myproxy);273 } else {274 factory.builder().proxy(java.net.Proxy.NO_PROXY);275 }276 executor = new HttpCommandExecutor(new HashMap<>(), url, factory);277 // SetUp Driver278 LOG.debug("Set Driver");279 WebDriver driver = null;280 AppiumDriver appiumDriver = null;281 switch (tCExecution.getApplicationObj().getType().toUpperCase()) {282 case Application.TYPE_GUI:283 if (caps.getPlatform() != null && caps.getPlatform().is(Platform.ANDROID)) {284 // Appium does not support connection from HTTPCommandExecutor. When connecting from Executor, it stops to work after a couple of instructions.285 appiumDriver = new AndroidDriver(url, caps);286 } else if (caps.getPlatform() != null && (caps.getPlatform().is(Platform.IOS) || caps.getPlatform().is(Platform.MAC))) {287 appiumDriver = new IOSDriver(url, caps);288 }289 driver = appiumDriver == null ? new RemoteWebDriver(executor, caps) : appiumDriver;290 tCExecution.setRobotProviderSessionID(getSession(driver, tCExecution.getRobotProvider()));291 tCExecution.setRobotSessionID(getSession(driver));292 break;293 case Application.TYPE_APK:294 // add a lock on app path this part of code, because we can't install 2 apk with the same name simultaneously295 String appUrl = null;296 if (caps.getCapability("app") != null) {297 appUrl = caps.getCapability("app").toString();298 }299 if (appUrl != null) { // FIX : appium can't install 2 apk simultaneously, so implement a litle latency between execution300 synchronized (this) {301 // with appium 1.7.2, we can't install 2 fresh apk simultaneously. Appium have to prepare the apk (transformation) on the first execution before (see this topic https://discuss.appium.io/t/execute-2-android-test-simultaneously-problem-during-install-apk/22030)302 // provoque a latency if first test is already running and apk don't finish to be prepared303 if (apkAlreadyPrepare.containsKey(appUrl) && Boolean.TRUE.equals(!apkAlreadyPrepare.get(appUrl))) {304 Thread.sleep(10000);305 } else {306 apkAlreadyPrepare.put(appUrl, false);307 }308 }309 }310 appiumDriver = new AndroidDriver(url, caps);311 if (apkAlreadyPrepare.containsKey(appUrl)) {312 apkAlreadyPrepare.put(appUrl, true);313 }314 driver = appiumDriver;315 tCExecution.setRobotProviderSessionID(getSession(driver, tCExecution.getRobotProvider()));316 tCExecution.setRobotSessionID(getSession(driver));317 break;318 case Application.TYPE_IPA:319 appiumDriver = new IOSDriver(url, caps);320 driver = appiumDriver;321 tCExecution.setRobotProviderSessionID(getSession(driver, tCExecution.getRobotProvider()));322 tCExecution.setRobotSessionID(getSession(driver));323 break;324 case Application.TYPE_FAT:325 // Check sikuli extension is reachable326 if (!sikuliService.isSikuliServerReachableOnRobot(session)) {327 MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.VALIDATION_FAILED_SIKULI_COULDNOTCONNECT);328 mes.setDescription(mes.getDescription().replace("%SSIP%", tCExecution.getSession().getHost()));329 mes.setDescription(mes.getDescription().replace("%SSPORT%", tCExecution.getSession().getPort()));330 throw new CerberusException(mes);331 }332 // If CountryEnvParameter IP is set, open the App333 if (!tCExecution.getCountryEnvironmentParameters().getIp().isEmpty()) {334 sikuliService.doSikuliActionOpenApp(session, tCExecution.getCountryEnvironmentParameters().getIp());335 }336 break;337 }338 // We record Server Side Caps.339 if (driver != null) {340 try {341 // Init additionalFinalCapabilities and set it from real caps.342 List<RobotCapability> serverCapabilities = new ArrayList<>();343 for (Map.Entry<String, Object> cap : ((HasCapabilities) driver).getCapabilities().asMap().entrySet()) {344 serverCapabilities.add(factoryRobotCapability.create(0, "", cap.getKey(), cap.getValue().toString()));345 }346 tCExecution.addFileList(recorderService.recordServerCapabilities(tCExecution, serverCapabilities));347 } catch (Exception ex) {348 LOG.error("Exception Saving Server Robot Caps " + tCExecution.getId(), ex);349 }350 }351 /*352 * Defining the timeout at the driver level. Only in case of no353 * Appium Driver (see354 * https://github.com/vertigo17/Cerberus/issues/754)355 */356 if (driver != null && appiumDriver == null) {357 driver.manage().timeouts().pageLoadTimeout(cerberus_selenium_pageLoadTimeout, TimeUnit.MILLISECONDS);358 driver.manage().timeouts().implicitlyWait(cerberus_selenium_implicitlyWait, TimeUnit.MILLISECONDS);359 driver.manage().timeouts().setScriptTimeout(cerberus_selenium_setScriptTimeout, TimeUnit.MILLISECONDS);360 }361 if (appiumDriver != null) {362 appiumDriver.manage().timeouts().implicitlyWait(cerberus_appium_wait_element, TimeUnit.MILLISECONDS);363 }364 tCExecution.getSession().setDriver(driver);365 tCExecution.getSession().setAppiumDriver(appiumDriver);366 /*367 * If Gui application, maximize window Get IP of Node in case of368 * remote Server. Maximize does not work for chrome browser We also369 * get the Real UserAgent from the browser.370 */371 if (tCExecution.getApplicationObj().getType().equalsIgnoreCase(Application.TYPE_GUI)372 && !caps.getPlatform().equals(Platform.ANDROID) && !caps.getPlatform().equals(Platform.IOS)373 && !caps.getPlatform().equals(Platform.MAC)) {374 // Maximize is not supported on Opera.375 if (!caps.getBrowserName().equals(BrowserType.CHROME) && !tCExecution.getBrowser().equalsIgnoreCase("opera")) {376 driver.manage().window().maximize();377 }378 getIPOfNode(tCExecution);379 // If screenSize is defined, set the size of the screen.380 String targetScreensize = getScreenSizeToUse(tCExecution.getTestCaseObj().getScreenSize(), tCExecution.getScreenSize());381 LOG.debug("Selenium resolution : {}", targetScreensize);382 if (!tCExecution.getBrowser().equalsIgnoreCase(BrowserType.CHROME)) {383 // For chrome the resolution has already been defined at capabilities level.384 if ((!StringUtil.isNullOrEmpty(targetScreensize)) && targetScreensize.contains("*")) {385 Integer screenWidth = Integer.valueOf(targetScreensize.split("\\*")[0]);386 Integer screenLength = Integer.valueOf(targetScreensize.split("\\*")[1]);387 setScreenSize(driver, screenWidth, screenLength);388 LOG.debug("Selenium resolution Activated : {}*{}", screenWidth, screenLength);389 }390 }391 // Getting windows size Not supported on Opera.392 if (!tCExecution.getBrowser().equalsIgnoreCase("opera")) {393 tCExecution.setScreenSize(getScreenSize(driver));394 }395 tCExecution.setRobotDecli(tCExecution.getRobotDecli().replace("%SCREENSIZE%", tCExecution.getScreenSize()));396 String userAgent = (String) ((JavascriptExecutor) driver).executeScript("return navigator.userAgent;");397 tCExecution.setUserAgent(userAgent);398 }399 // unlock device if deviceLockUnlock is active400 if (tCExecution.getRobotExecutorObj() != null && appiumDriver instanceof LocksDevice401 && "Y".equals(tCExecution.getRobotExecutorObj().getDeviceLockUnlock())) {402 ((LocksDevice) appiumDriver).unlockDevice();403 }404 // Check if Sikuli is available on node.405 if (driver != null) {406 tCExecution.getSession().setSikuliAvailable(sikuliService.isSikuliServerReachableOnNode(tCExecution.getSession()));407 }408 tCExecution.getSession().setStarted(true);409 } catch (CerberusException exception) {410 LOG.error(exception.toString(), exception);411 throw new CerberusException(exception.getMessageError(), exception);412 } catch (MalformedURLException exception) {413 LOG.error(exception.toString(), exception);414 MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.VALIDATION_FAILED_URL_MALFORMED);415 mes.setDescription(mes.getDescription().replace("%URL%", tCExecution.getSession().getHost() + ":" + tCExecution.getSession().getPort()));416 throw new CerberusException(mes, exception);417 } catch (UnreachableBrowserException exception) {418 LOG.warn("Could not connect to : {}:{}", tCExecution.getSeleniumIP(), tCExecution.getSeleniumPort());419 MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.VALIDATION_FAILED_SELENIUM_COULDNOTCONNECT);420 mes.setDescription(mes.getDescription().replace("%SSIP%", tCExecution.getSeleniumIP()));421 mes.setDescription(mes.getDescription().replace("%SSPORT%", tCExecution.getSeleniumPort()));422 mes.setDescription(mes.getDescription().replace("%ERROR%", exception.toString()));423 throw new CerberusException(mes, exception);424 } catch (Exception exception) {425 LOG.error(exception.toString(), exception);426 MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.EXECUTION_FA_SELENIUM);427 mes.setDescription(mes.getDescription().replace("%MES%", exception.toString()));428 executorService.stopRemoteProxy(tCExecution);429 throw new CerberusException(mes, exception);430 } finally {431 executionThreadPoolService.executeNextInQueueAsynchroneously(false);432 }433 }434 private String getSession(WebDriver driver, String robotProvider) {435 String session = "";436 switch (robotProvider) {437 case TestCaseExecution.ROBOTPROVIDER_BROWSERSTACK:438 case TestCaseExecution.ROBOTPROVIDER_LAMBDATEST: // For LambdaTest we get the exeid not here but by service call at the end of the execution.439 case TestCaseExecution.ROBOTPROVIDER_NONE:440 session = ((RemoteWebDriver) driver).getSessionId().toString();441 break;442 case TestCaseExecution.ROBOTPROVIDER_KOBITON:443 session = ((HasCapabilities) driver).getCapabilities().getCapability("kobitonSessionId").toString();444 break;445 default:446 }447 return session;448 }449 private String getSession(WebDriver driver) {450 String session = "";451 session = ((RemoteWebDriver) driver).getSessionId().toString();452 return session;453 }454 private String guessRobotProvider(String host) {455 if (host.contains("browserstack")) {456 return TestCaseExecution.ROBOTPROVIDER_BROWSERSTACK;457 }458 if (host.contains("kobiton")) {459 return TestCaseExecution.ROBOTPROVIDER_KOBITON;460 }461 if (host.contains("lambdatest")) {462 return TestCaseExecution.ROBOTPROVIDER_LAMBDATEST;463 }464 return TestCaseExecution.ROBOTPROVIDER_NONE;465 }466 /**467 * Set DesiredCapabilities468 *469 * @param tCExecution470 * @return471 * @throws CerberusException472 */473 private MutableCapabilities setCapabilities(TestCaseExecution tCExecution) throws CerberusException {474 // Instanciate DesiredCapabilities475 MutableCapabilities caps = new MutableCapabilities();476 // In case browser is not defined at that level, we force it to firefox.477 if (StringUtil.isNullOrEmpty(tCExecution.getBrowser())) {478 tCExecution.setBrowser("");479 }480 // Set Browser Capabilities481 caps = this.setCapabilityBrowser(caps, tCExecution.getBrowser(), tCExecution);482 // Loop on RobotCapabilities to feed DesiredCapabilities Capability must be String, Integer or Boolean483 List<RobotCapability> additionalCapabilities = new ArrayList<>();484 if (tCExecution.getRobotObj() != null) {485 additionalCapabilities = tCExecution.getRobotObj().getCapabilitiesDecoded();486 }487 if (additionalCapabilities != null) {488 for (RobotCapability additionalCapability : additionalCapabilities) {489 LOG.debug("RobotCaps on Robot : {} caps: {} Value: {}", additionalCapability.getRobot(), additionalCapability.getCapability(), additionalCapability.getValue());490 if ((caps.getCapability(additionalCapability.getCapability()) == null)491 || ((caps.getCapability(additionalCapability.getCapability()) != null) && (caps.getCapability(additionalCapability.getCapability()).toString().isEmpty()))) { // caps does not already exist so we can set it.492 if (StringUtil.isBoolean(additionalCapability.getValue())) {493 caps.setCapability(additionalCapability.getCapability(), StringUtil.parseBoolean(additionalCapability.getValue()));494 } else if (StringUtil.isInteger(additionalCapability.getValue())) {495 caps.setCapability(additionalCapability.getCapability(), Integer.valueOf(additionalCapability.getValue()));496 } else {497 caps.setCapability(additionalCapability.getCapability(), additionalCapability.getValue());498 }499 }500 }501 } else {502 additionalCapabilities = new ArrayList<>();503 }504 // Feed DesiredCapabilities with values get from Robot505 if (!StringUtil.isNullOrEmpty(tCExecution.getPlatform())506 && ((caps.getCapability("platform") == null)507 || ((caps.getCapability("platform") != null)508 && (caps.getCapability("platform").toString().equals("ANY")509 || caps.getCapability("platform").toString().isEmpty())))) {510 caps.setCapability("platformName", tCExecution.getPlatform());511 }512 if (!StringUtil.isNullOrEmpty(tCExecution.getVersion())513 && ((caps.getCapability("version") == null)514 || ((caps.getCapability("version") != null)515 && (caps.getCapability("version").toString().isEmpty())))) {516 caps.setCapability("version", tCExecution.getVersion());517 }518 if (tCExecution.getRobotExecutorObj() != null) {519 // Setting deviceUdid and device name from executor.520 if (!StringUtil.isNullOrEmpty(tCExecution.getRobotExecutorObj().getDeviceUuid())521 && ((caps.getCapability("udid") == null)522 || ((caps.getCapability("udid") != null)523 && (caps.getCapability("udid").toString().isEmpty())))) {524 caps.setCapability("udid", tCExecution.getRobotExecutorObj().getDeviceUuid());525 }526 if (!StringUtil.isNullOrEmpty(tCExecution.getRobotExecutorObj().getDeviceName())527 && ((caps.getCapability("deviceName") == null)528 || ((caps.getCapability("deviceName") != null)529 && (caps.getCapability("deviceName").toString().isEmpty())))) {530 caps.setCapability("deviceName", tCExecution.getRobotExecutorObj().getDeviceName());531 }532 if (!StringUtil.isNullOrEmpty(tCExecution.getRobotExecutorObj().getDeviceName())) {533 if (tCExecution.getApplicationObj().getType().equalsIgnoreCase(Application.TYPE_APK)534 && ((caps.getCapability("systemPort") == null)535 || ((caps.getCapability("systemPort") != null)536 && (caps.getCapability("systemPort").toString().isEmpty())))) {537 caps.setCapability("systemPort", tCExecution.getRobotExecutorObj().getDevicePort());538 } else if (tCExecution.getApplicationObj().getType().equalsIgnoreCase(Application.TYPE_IPA)539 && ((caps.getCapability("wdaLocalPort") == null)540 || ((caps.getCapability("wdaLocalPort") != null)541 && (caps.getCapability("wdaLocalPort").toString().isEmpty())))) {542 caps.setCapability("wdaLocalPort", tCExecution.getRobotExecutorObj().getDevicePort());543 }544 }545 }546 // if application is a mobile one, then set the "app" capability to theapplication binary path547 if (tCExecution.getApplicationObj().getType().equalsIgnoreCase(Application.TYPE_APK)548 || tCExecution.getApplicationObj().getType().equalsIgnoreCase(Application.TYPE_IPA)) {549 // Set the app capability with the application path550 if (!StringUtil.isNullOrEmpty(tCExecution.getMyHost())551 && (isNotAlreadyDefined(caps, "app"))) {552 caps.setCapability("app", tCExecution.getMyHost());553 } else if (isNotAlreadyDefined(caps, "app")) {554 caps.setCapability("app", tCExecution.getCountryEnvironmentParameters().getIp());555 }556 if (!StringUtil.isNullOrEmpty(tCExecution.getCountryEnvironmentParameters().getMobileActivity())557 && (tCExecution.getApplicationObj().getType().equalsIgnoreCase(Application.TYPE_APK)558 && isNotAlreadyDefined(caps, "appWaitActivity"))) {559 caps.setCapability("appWaitActivity", tCExecution.getCountryEnvironmentParameters().getMobileActivity());560 }561 if (tCExecution.getApplicationObj().getType().equalsIgnoreCase(Application.TYPE_APK)562 && (isNotAlreadyDefined(caps, "automationName"))) {563 caps.setCapability("automationName", "UIAutomator2"); // use UIAutomator2 by default564 }565 }566 // Setting specific capabilities of external cloud providers.567 switch (tCExecution.getRobotProvider()) {568 case TestCaseExecution.ROBOTPROVIDER_BROWSERSTACK:569 if (!StringUtil.isNullOrEmpty(tCExecution.getTag()) && isNotAlreadyDefined(caps, "build")) {570 caps.setCapability("build", tCExecution.getTag());571 }572 if (isNotAlreadyDefined(caps, "project")) {573 caps.setCapability("project", tCExecution.getApplication());574 }575 if (isNotAlreadyDefined(caps, "name")) {576 String externalExeName = parameterService.getParameterStringByKey("cerberus_browserstack_defaultexename", tCExecution.getSystem(), "Exe : %EXEID%");577 externalExeName = externalExeName.replace("%EXEID%", String.valueOf(tCExecution.getId()));578 caps.setCapability("name", externalExeName);579 }580 if (tCExecution.getVerbose() >= 2) {581 if (isNotAlreadyDefined(caps, "browserstack.debug")) {582 caps.setCapability("browserstack.debug", true);583 }584 if (isNotAlreadyDefined(caps, "browserstack.console")) {585 caps.setCapability("browserstack.console", "warnings");586 }587 if (isNotAlreadyDefined(caps, "browserstack.networkLogs")) {588 caps.setCapability("browserstack.networkLogs", true);589 }590 }591 //Create or override these capabilities if proxy required.592 if (StringUtil.parseBoolean(tCExecution.getRobotExecutorObj().getExecutorProxyActive())) {593 caps.setCapability("browserstack.local", true);594 caps.setCapability("browserstack.user", tCExecution.getRobotExecutorObj().getHostUser());595 caps.setCapability("browserstack.key", tCExecution.getRobotExecutorObj().getHostPassword());596 caps.setCapability("browserstack.localIdentifier", tCExecution.getExecutionUUID());597 }598 break;599 case TestCaseExecution.ROBOTPROVIDER_LAMBDATEST:600 if (!StringUtil.isNullOrEmpty(tCExecution.getTag()) && isNotAlreadyDefined(caps, "build")) {601 caps.setCapability("build", tCExecution.getTag());602 }603 if (isNotAlreadyDefined(caps, "name")) {604 String externalExeName = parameterService.getParameterStringByKey("cerberus_lambdatest_defaultexename", tCExecution.getSystem(), "Exe : %EXEID% - %TESTDESCRIPTION%");605 externalExeName = externalExeName.replace("%EXEID%", String.valueOf(tCExecution.getId()));606 externalExeName = externalExeName.replace("%TESTFOLDER%", String.valueOf(tCExecution.getTest()));607 externalExeName = externalExeName.replace("%TESTID%", String.valueOf(tCExecution.getTestCase()));608 externalExeName = externalExeName.replace("%TESTDESCRIPTION%", String.valueOf(tCExecution.getDescription()));609 caps.setCapability("name", externalExeName);610 }611 if (tCExecution.getVerbose() >= 2) {612 if (isNotAlreadyDefined(caps, "video")) {613 caps.setCapability("video", true);614 }615 if (isNotAlreadyDefined(caps, "visual")) {616 caps.setCapability("visual", true);617 }618 if (isNotAlreadyDefined(caps, "network")) {619 caps.setCapability("network", true);620 }621 if (isNotAlreadyDefined(caps, "console")) {622 caps.setCapability("console", true);623 }624 }625 break;626 case TestCaseExecution.ROBOTPROVIDER_KOBITON:627 if (isNotAlreadyDefined(caps, "sessionName")) {628 String externalExeName = parameterService.getParameterStringByKey("cerberus_kobiton_defaultsessionname", tCExecution.getSystem(), "%EXEID% : %TEST% - %TESTCASE%");629 externalExeName = externalExeName.replace("%EXEID%", String.valueOf(tCExecution.getId()));630 externalExeName = externalExeName.replace("%APPLI%", String.valueOf(tCExecution.getApplication()));631 externalExeName = externalExeName.replace("%TAG%", String.valueOf(tCExecution.getTag()));632 externalExeName = externalExeName.replace("%TEST%", String.valueOf(tCExecution.getTest()));633 externalExeName = externalExeName.replace("%TESTCASE%", String.valueOf(tCExecution.getTestCase()));634 externalExeName = externalExeName.replace("%TESTCASEDESC%", String.valueOf(tCExecution.getTestCaseObj().getDescription()));635 caps.setCapability("sessionName", externalExeName);636 }637 if (isNotAlreadyDefined(caps, "sessionDescription")) {638 String externalExeName = parameterService.getParameterStringByKey("cerberus_kobiton_defaultsessiondescription", tCExecution.getSystem(), "%TESTCASEDESC%");639 externalExeName = externalExeName.replace("%EXEID%", String.valueOf(tCExecution.getId()));640 externalExeName = externalExeName.replace("%APPLI%", String.valueOf(tCExecution.getApplication()));641 externalExeName = externalExeName.replace("%TAG%", String.valueOf(tCExecution.getTag()));642 externalExeName = externalExeName.replace("%TEST%", String.valueOf(tCExecution.getTest()));643 externalExeName = externalExeName.replace("%TESTCASE%", String.valueOf(tCExecution.getTestCase()));644 externalExeName = externalExeName.replace("%TESTCASEDESC%", String.valueOf(tCExecution.getTestCaseObj().getDescription()));645 caps.setCapability("sessionDescription", externalExeName);646 }647 if (isNotAlreadyDefined(caps, "deviceGroup")) {648 caps.setCapability("deviceGroup", "KOBITON"); // use UIAutomator2 by default649 }650 break;651 case TestCaseExecution.ROBOTPROVIDER_NONE:652 break;653 default:654 }655 return caps;656 }657 private boolean isNotAlreadyDefined(MutableCapabilities caps, String capability) {658 return ((caps.getCapability(capability) == null)659 || ((caps.getCapability(capability) != null)660 && (caps.getCapability(capability).toString().isEmpty())));661 }662 /**663 * Instantiate DesiredCapabilities regarding the browser664 *665 * @param capabilities666 * @param browser667 * @param tCExecution668 * @return669 * @throws CerberusException670 */671 private MutableCapabilities setCapabilityBrowser(MutableCapabilities capabilities, String browser, TestCaseExecution tCExecution) throws CerberusException {672 try {673 // Get User Agent to use.674 String usedUserAgent;675 usedUserAgent = getUserAgentToUse(tCExecution.getTestCaseObj().getUserAgent(), tCExecution.getUserAgent());676 LoggingPreferences logPrefs = new LoggingPreferences();677 logPrefs.enable(LogType.BROWSER, Level.ALL);678 switch (browser) {679 case "firefox":680 FirefoxOptions optionsFF = new FirefoxOptions();681 FirefoxProfile profile = new FirefoxProfile();682 profile.setPreference("app.update.enabled", false);683 // Language684 try {685 Invariant invariant = invariantService.convert(invariantService.readByKey("COUNTRY", tCExecution.getCountry()));686 if (invariant.getGp2() == null) {687 LOG.warn("Country selected ({}) has no value of GP2 in Invariant table, default language set to English (en)", tCExecution.getCountry());688 profile.setPreference("intl.accept_languages", "en");689 } else {690 profile.setPreference("intl.accept_languages", invariant.getGp2());691 }692 } catch (CerberusException ex) {693 LOG.warn("Country selected ({}) not in Invariant table, default language set to English (en)", tCExecution.getCountry());694 profile.setPreference("intl.accept_languages", "en");695 }696 // Force a specific profile for that session (allows reusing cookies and browser preferences).697 if (tCExecution.getRobotObj() != null && !StringUtil.isNullOrEmpty(tCExecution.getRobotObj().getProfileFolder())) {698 optionsFF.addArguments("--profile");699 optionsFF.addArguments(tCExecution.getRobotObj().getProfileFolder());700 }701 // Set UserAgent if testCaseUserAgent or robotUserAgent is defined702 if (!StringUtil.isNullOrEmpty(usedUserAgent)) {703 profile.setPreference("general.useragent.override", usedUserAgent);704 }705 // Verbose level and Headless706 if (tCExecution.getVerbose() <= 0) {707 optionsFF.setHeadless(true);708 }709 // Add the WebDriver proxy capability.710 if (tCExecution.getRobotExecutorObj() != null && "Y".equals(tCExecution.getRobotExecutorObj().getExecutorProxyActive())) {711 Proxy proxy = new Proxy();712 proxy.setHttpProxy(tCExecution.getRobotExecutorObj().getExecutorProxyHost() + ":" + tCExecution.getRemoteProxyPort());713 proxy.setSslProxy(tCExecution.getRobotExecutorObj().getExecutorProxyHost() + ":" + tCExecution.getRemoteProxyPort());714 proxy.setProxyType(Proxy.ProxyType.MANUAL);715 LOG.debug("Setting Firefox proxy to : {}", proxy);716 optionsFF.setProxy(proxy);717 }718 optionsFF.setProfile(profile);719 // Accept Insecure Certificates.720 optionsFF.setAcceptInsecureCerts(tCExecution.getRobotObj() == null || tCExecution.getRobotObj().isAcceptInsecureCerts());721 // Collect Logs on Selenium side.722 optionsFF.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);723 return optionsFF;724 case "chrome":725 ChromeOptions optionsCH = new ChromeOptions();726 // Maximize windows for chrome browser727 String targetScreensize = getScreenSizeToUse(tCExecution.getTestCaseObj().getScreenSize(), tCExecution.getScreenSize());728 if ((!StringUtil.isNullOrEmpty(targetScreensize)) && targetScreensize.contains("*")) {729 Integer screenWidth = Integer.valueOf(targetScreensize.split("\\*")[0]);730 Integer screenLength = Integer.valueOf(targetScreensize.split("\\*")[1]);731 String sizeOpts = "--window-size=" + screenWidth + "," + screenLength;732 optionsCH.addArguments(sizeOpts);733 LOG.debug("Selenium resolution (for Chrome) Activated : " + screenWidth + "*" + screenLength);734 } else {735 optionsCH.addArguments("start-maximized");736 }737 // Language738 try {739 Invariant invariant = invariantService.convert(invariantService.readByKey("COUNTRY", tCExecution.getCountry()));740 if (invariant.getGp2() == null) {741 LOG.warn("Country selected ({}) has no value of GP2 in Invariant table, default language set to English (en)", tCExecution.getCountry());742 optionsCH.addArguments("--lang=en");743 } else {744 optionsCH.addArguments("--lang=" + invariant.getGp2());745 }746 } catch (CerberusException ex) {747 LOG.warn("Country selected ({}) not in Invariant table, default language set to English (en)", tCExecution.getCountry());748 optionsCH.addArguments("--lang=en");749 }750 // Force a specific profile for that session (allows reusing cookies and browser preferences).751 if (tCExecution.getRobotObj() != null && !StringUtil.isNullOrEmpty(tCExecution.getRobotObj().getProfileFolder())) {752 optionsCH.addArguments("user-data-dir=" + tCExecution.getRobotObj().getProfileFolder());753 }754 // Set UserAgent if necessary755 if (!StringUtil.isNullOrEmpty(usedUserAgent)) {756 optionsCH.addArguments("--user-agent=" + usedUserAgent);757 }758 // Verbose level and Headless759 if (tCExecution.getVerbose() <= 0) {760 optionsCH.addArguments("--headless");761 }762 // Add the WebDriver proxy capability.763 if (tCExecution.getRobotExecutorObj() != null && "Y".equals(tCExecution.getRobotExecutorObj().getExecutorProxyActive())) {764 Proxy proxy = new Proxy();765 proxy.setHttpProxy(tCExecution.getRobotExecutorObj().getExecutorProxyHost() + ":" + tCExecution.getRemoteProxyPort());766 proxy.setSslProxy(tCExecution.getRobotExecutorObj().getExecutorProxyHost() + ":" + tCExecution.getRemoteProxyPort());767 proxy.setNoProxy("");768 proxy.setProxyType(Proxy.ProxyType.MANUAL);769 LOG.debug("Setting Chrome proxy to : {}", proxy);770 optionsCH.setCapability(DEFAULT_PROXY_HOST, proxy);771 }772 // Accept Insecure Certificates.773 if (tCExecution.getRobotObj() != null && !tCExecution.getRobotObj().isAcceptInsecureCerts()) {774 optionsCH.setAcceptInsecureCerts(false);775 } else {776 optionsCH.setAcceptInsecureCerts(true);777 }778 // Extra Browser Parameters.779 if (tCExecution.getRobotObj() != null && !StringUtil.isNullOrEmpty(tCExecution.getRobotObj().getExtraParam())) {780 optionsCH.addArguments(tCExecution.getRobotObj().getExtraParam());781 }782 // Collect Logs on Selenium side.783 optionsCH.setCapability("goog:loggingPrefs", logPrefs);784 return optionsCH;785 case "safari":786 SafariOptions optionsSA = new SafariOptions();787 if (tCExecution.getRobotExecutorObj() != null && "Y".equals(tCExecution.getRobotExecutorObj().getExecutorProxyActive())) {788 Proxy proxy = new Proxy();789 proxy.setHttpProxy(tCExecution.getRobotExecutorObj().getExecutorProxyHost() + ":" + tCExecution.getRemoteProxyPort());790 proxy.setSslProxy(tCExecution.getRobotExecutorObj().getExecutorProxyHost() + ":" + tCExecution.getRemoteProxyPort());791 optionsSA.setProxy(proxy);792 }793 return optionsSA;794 case "IE":...
getRobotObj
Using AI Code Generation
1Robot robot = getRobotObj();2String robotExecutor = getRobotExecutor();3String robotHost = getRobotHost();4String robotPort = getRobotPort();5String robotPlatform = getRobotPlatform();6String robotBrowser = getRobotBrowser();7String robotVersion = getRobotVersion();8String robotBrowserVersion = getRobotBrowserVersion();9String robotUrl = getRobotUrl();10String robotHost = getRobotHost();11String robotPort = getRobotPort();12String robotPlatform = getRobotPlatform();13String robotBrowser = getRobotBrowser();
getRobotObj
Using AI Code Generation
1public class TestCaseExecution {2 private Robot robot;3 private Robot robot2;4 private Robot robot3;5 private Robot robot4;6 private Robot robot5;7 private Robot robot6;8 private Robot robot7;9 private Robot robot8;10 private Robot robot9;11 private Robot robot10;12 private Robot robot11;13 private Robot robot12;14 private Robot robot13;15 private Robot robot14;16 private Robot robot15;17 private Robot robot16;18 private Robot robot17;19 private Robot robot18;20 private Robot robot19;21 private Robot robot20;22 private Robot robot21;23 private Robot robot22;24 private Robot robot23;25 private Robot robot24;26 private Robot robot25;27 private Robot robot26;28 private Robot robot27;29 private Robot robot28;30 private Robot robot29;31 private Robot robot30;32 private Robot robot31;33 private Robot robot32;34 private Robot robot33;35 private Robot robot34;36 private Robot robot35;37 private Robot robot36;38 private Robot robot37;39 private Robot robot38;40 private Robot robot39;41 private Robot robot40;42 private Robot robot41;43 private Robot robot42;44 private Robot robot43;45 private Robot robot44;46 private Robot robot45;47 private Robot robot46;48 private Robot robot47;49 private Robot robot48;50 private Robot robot49;51 private Robot robot50;52 private Robot robot51;53 private Robot robot52;54 private Robot robot53;55 private Robot robot54;56 private Robot robot55;57 private Robot robot56;58 private Robot robot57;59 private Robot robot58;60 private Robot robot59;61 private Robot robot60;62 private Robot robot61;63 private Robot robot62;64 private Robot robot63;65 private Robot robot64;66 private Robot robot65;67 private Robot robot66;68 private Robot robot67;69 private Robot robot68;70 private Robot robot69;71 private Robot robot70;72 private Robot robot71;73 private Robot robot72;74 private Robot robot73;
getRobotObj
Using AI Code Generation
1import org.cerberus.crud.entity.TestCaseExecution;2import org.openqa.selenium.By;3import org.openqa.selenium.WebDriver;4import org.openqa.selenium.WebElement;5TestCaseExecution tce = getRobotObj();6WebDriver driver = tce.getDriver();7WebElement element = driver.findElement(By.id("id"));8element.sendKeys("value");9element.click();
getRobotObj
Using AI Code Generation
1import org.cerberus.engine.entity.MessageEvent;2import org.cerberus.crud.entity.Robot;3import org.cerberus.crud.entity.TestCaseExecution;4import org.cerberus.crud.factory.IFactoryRobot;5import org.cerberus.crud.service.IRobotService;6import org.springframework.beans.factory.annotation.Autowired;7import org.springframework.stereotype.Service;8public class RobotService implements IRobotService {9 private IFactoryRobot factoryRobot;10 public MessageEvent createRobot(TestCaseExecution tCExecution) {11 MessageEvent message = new MessageEvent(MessageEventEnum.PROPERTY_SUCCESS);12 message.setDescription(message.getDescription().replace("%ITEM%", "Robot").replace("%OPERATION%", "CREATE"));13 return message;14 }15 public Robot getRobotFromExecution(TestCaseExecution tCExecution) {16 Robot robot = tCExecution.getRobotObj();17 return robot;18 }19 public String getRobotNameFromExecution(TestCaseExecution tCExecution) {20 Robot robot = tCExecution.getRobotObj();21 String robotName = robot.getRobot();22 return robotName;23 }24 public String getRobotVersionFromExecution(TestCaseExecution tCExecution) {25 Robot robot = tCExecution.getRobotObj();26 String robotVersion = robot.getRobotVersion();27 return robotVersion;28 }29 public Robot getRobotFromNameAndVersion(String robotName, String robotVersion) {30 Robot robot = factoryRobot.create(robotName, robotVersion);31 return robot;32 }33 public String getRobotVersionFromRobot(Robot robot) {34 String robotVersion = robot.getRobotVersion();35 return robotVersion;36 }37 public String getRobotNameFromRobot(Robot robot) {38 String robotName = robot.getRobot();39 return robotName;40 }41}42public Robot getRobotFromExecution(TestCaseExecution tCExecution) {
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!!