Best Cerberus-source code snippet using org.cerberus.engine.execution.impl.RobotServerService.getUserAgentToUse
Source:RobotServerService.java
...665 LOG.warn("Country selected (" + tCExecution.getCountry() + ") not in Invariant table, default language set to English (en)");666 profile.setPreference("intl.accept_languages", "en");667 }668 // Set UserAgent if testCaseUserAgent or robotUserAgent is defined669 String usedUserAgent = getUserAgentToUse(tCExecution.getTestCaseObj().getUserAgent(), tCExecution.getUserAgent());670 if (!StringUtil.isNullOrEmpty(usedUserAgent)) {671 profile.setPreference("general.useragent.override", usedUserAgent);672 }673// capabilities.setCapability(FirefoxDriver.PROFILE, profile);674 if (tCExecution.getVerbose() <= 0) {675 options.setHeadless(true);676 }677 // Add the WebDriver proxy capability.678 if (tCExecution.getRobotExecutorObj() != null && "Y".equals(tCExecution.getRobotExecutorObj().getExecutorProxyActive())) {679 Proxy proxy = new Proxy();680 proxy.setHttpProxy(tCExecution.getRobotExecutorObj().getExecutorProxyHost() + ":" + tCExecution.getRemoteProxyPort());681 proxy.setSslProxy(tCExecution.getRobotExecutorObj().getExecutorProxyHost() + ":" + tCExecution.getRemoteProxyPort());682 options.setProxy(proxy);683 }684 options.setProfile(profile);685 return options;686// capabilities.setCapability(FirefoxOptions.FIREFOX_OPTIONS, options);687 } else if (browser.equalsIgnoreCase("IE")) {688 InternetExplorerOptions options = new InternetExplorerOptions();689 // Add the WebDriver proxy capability.690 if (tCExecution.getRobotExecutorObj() != null && "Y".equals(tCExecution.getRobotExecutorObj().getExecutorProxyActive())) {691 Proxy proxy = new Proxy();692 proxy.setHttpProxy(tCExecution.getRobotExecutorObj().getExecutorProxyHost() + ":" + tCExecution.getRemoteProxyPort());693 proxy.setSslProxy(tCExecution.getRobotExecutorObj().getExecutorProxyHost() + ":" + tCExecution.getRemoteProxyPort());694 options.setCapability("proxy", proxy);695 }696 return options;697// capabilities = DesiredCapabilities.internetExplorer();698 } else if (browser.equalsIgnoreCase("chrome")) {699 /**700 * Add custom capabilities701 */702 ChromeOptions options = new ChromeOptions();703// capabilities = DesiredCapabilities.chrome();704 // Maximize windows for chrome browser705 String targetScreensize = getScreenSizeToUse(tCExecution.getTestCaseObj().getScreenSize(), tCExecution.getScreenSize());706 if ((!StringUtil.isNullOrEmpty(targetScreensize)) && targetScreensize.contains("*")) {707 Integer screenWidth = Integer.valueOf(targetScreensize.split("\\*")[0]);708 Integer screenLength = Integer.valueOf(targetScreensize.split("\\*")[1]);709 String sizeOpts = "--window-size=" + screenWidth + "," + screenLength;710 options.addArguments(sizeOpts);711 LOG.debug("Selenium resolution (for Chrome) Activated : " + screenWidth + "*" + screenLength);712 }713 options.addArguments("start-maximized");714 if (tCExecution.getVerbose() <= 0) {715 options.addArguments("--headless");716 }717 // Set UserAgent if necessary718 String usedUserAgent = getUserAgentToUse(tCExecution.getTestCaseObj().getUserAgent(), tCExecution.getUserAgent());719 if (!StringUtil.isNullOrEmpty(usedUserAgent)) {720 options.addArguments("--user-agent=" + usedUserAgent);721 }722 // Add the WebDriver proxy capability.723 if (tCExecution.getRobotExecutorObj() != null && "Y".equals(tCExecution.getRobotExecutorObj().getExecutorProxyActive())) {724 Proxy proxy = new Proxy();725 proxy.setHttpProxy(tCExecution.getRobotExecutorObj().getExecutorProxyHost() + ":" + tCExecution.getRemoteProxyPort());726 proxy.setSslProxy(tCExecution.getRobotExecutorObj().getExecutorProxyHost() + ":" + tCExecution.getRemoteProxyPort());727 options.setCapability("proxy", proxy);728 }729 return options;730// capabilities.setCapability(ChromeOptions.CAPABILITY, options);731// additionalCapabilities.add(factoryRobotCapability.create(0, "", ChromeOptions.CAPABILITY, options.toString()));732 } else if (browser.contains("android")) {733 // Launch the proxy with the settings specified in the robot options (executor)734 // since proxy Settings is out the Appium's scope, you must set it manually on your device735 // set the same port on device and robot736 if (tCExecution.getRobotExecutorObj() != null && "Y".equals(tCExecution.getRobotExecutorObj().getExecutorProxyActive())) {737 this.startRemoteProxy(tCExecution);738 Proxy proxy = new Proxy();739 proxy.setHttpProxy(tCExecution.getRobotExecutorObj().getExecutorProxyHost() + ":" + tCExecution.getRemoteProxyPort());740 proxy.setSslProxy(tCExecution.getRobotExecutorObj().getExecutorProxyHost() + ":" + tCExecution.getRemoteProxyPort());741 }742 capabilities = DesiredCapabilities.android();743 } else if (browser.contains("ipad")) {744 capabilities = DesiredCapabilities.ipad();745 } else if (browser.contains("iphone")) {746 capabilities = DesiredCapabilities.iphone();747 } else if (browser.contains("safari")) {748 SafariOptions options = new SafariOptions();749 return options;750// capabilities = DesiredCapabilities.safari();751 } else {752 LOG.warn("Not supported Browser : " + browser);753 MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.EXECUTION_FA_SELENIUM);754 mes.setDescription(mes.getDescription().replace("%MES%", "Browser '" + browser + "' is not supported"));755 mes.setDescription("Not supported Browser : " + browser);756 throw new CerberusException(mes);757 }758 } catch (CerberusException ex) {759 MessageGeneral mes = new MessageGeneral(MessageGeneralEnum.EXECUTION_FA_SELENIUM);760 mes.setDescription(mes.getDescription().replace("%MES%", "Failed to set capability on the browser '" + browser + "' due to " + ex.getMessageError().getDescription()));761 throw new CerberusException(mes);762 }763 return capabilities;764 }765 /**766 * This method determine which user agent to use.767 *768 * @param userAgentTestCase769 * @param userAgentRobot770 * @return String containing the userAgent to use771 */772 private String getUserAgentToUse(String userAgentTestCase, String userAgentRobot) {773 if (StringUtil.isNullOrEmpty(userAgentRobot) && StringUtil.isNullOrEmpty(userAgentTestCase)) {774 return "";775 } else {776 return StringUtil.isNullOrEmpty(userAgentTestCase) ? userAgentRobot : userAgentTestCase;777 }778 }779 /**780 * This method determine which screenSize to use.781 *782 * @param screenSizeTestCase783 * @param screenSizeRobot784 * @return String containing the screensize to use785 */786 private String getScreenSizeToUse(String screenSizeTestCase, String screenSizeRobot) {...
getUserAgentToUse
Using AI Code Generation
1import org.cerberus.engine.execution.impl.RobotServerService;2import org.cerberus.engine.execution.impl.RobotServerService;3import org.cerberus.engine.entity.RobotExecution;4import org.cerberus.engine.entity.ExecutionContext;5import org.cerberus.engine.entity.HttpRequest;6RobotExecution robotExecution = (RobotExecution) executionContext.get("robotExecution");7String userAgent = RobotServerService.getUserAgentToUse(robotExecution);8HttpRequest currentHttpRequest = (HttpRequest) executionContext.get("currentHttpRequest");9currentHttpRequest.addHeader("User-Agent", userAgent);10executionContext.put("currentHttpRequest", currentHttpRequest);
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!!