Best Carina code snippet using com.qaprosoft.carina.browsermobproxy.ProxyPool.createProxy
Source:ProxyPool.java  
...63     * create BrowserMobProxy Server object64     * @return BrowserMobProxy65     * 66     */67    public static BrowserMobProxy createProxy() {68        BrowserMobProxyServer proxy = new BrowserMobProxyServer();69        proxy.setTrustAllServers(true);70        //System.setProperty("jsse.enableSNIExtension", "false");71        72        // disable MITM in case we do not need it73        proxy.setMitmDisabled(Configuration.getBoolean(Parameter.BROWSERMOB_MITM));74        75        return proxy;76    }77    78    public static void setupBrowserMobProxy()79    {80        if (Configuration.getBoolean(Parameter.BROWSERMOB_PROXY)) {81            long threadId = Thread.currentThread().getId();82            BrowserMobProxy proxy = startProxy();83            Integer port = proxy.getPort();84            proxyPortsByThread.put(threadId, port);85            86            // reuse "proxy_host" to be able to share valid publicly available host. 87            // it is useful when java and web tests are executed absolutely in different containers/networks. 88            if (Configuration.get(Parameter.PROXY_HOST).isEmpty()) {89            	String currentIP = NetworkUtil.getIpAddress();90            	R.CONFIG.put(Parameter.PROXY_HOST.getKey(), currentIP);91            }92            93            LOGGER.warn("Set http/https proxy settings only to use with BrowserMobProxy host: " + Configuration.get(Parameter.PROXY_HOST) + "; port: "94                    + proxyPortsByThread.get(threadId));95            96            R.CONFIG.put(Parameter.PROXY_PORT.getKey(), port.toString());97            98            R.CONFIG.put("proxy_protocols", "http,https");99            // follow steps to configure https traffic sniffering: https://github.com/lightbody/browsermob-proxy#ssl-support100            // the most important are:101            // download - https://github.com/lightbody/browsermob-proxy/blob/master/browsermob-core/src/main/resources/sslSupport/ca-certificate-rsa.cer102            // import to system (for android we should use certifiate installer in system settings->security...)103        }104    }105    // https://github.com/lightbody/browsermob-proxy/issues/264 'started' flag is not set to false after stopping BrowserMobProxyServer106    // Due to the above issue we can't control BrowserMob isRunning state and shouldn't stop it107    // TODO: investigate possibility to clean HAR files if necessary108    109    /**110     * stop BrowserMobProxy Server111     * 112     */113    /*114    public static void stopProxy() {115        long threadId = Thread.currentThread().getId();116        LOGGER.debug("stopProxy starting...");117        if (proxies.containsKey(threadId)) {118            BrowserMobProxy proxy = proxies.get(threadId);119            if (proxy != null) {120                LOGGER.debug("Found registered proxy by thread: " + threadId);121                if (proxy.isStarted()) {122                    LOGGER.info("Stopping BrowserMob proxy...");123                    proxy.stop();124                } else {125                    LOGGER.info("Stopping BrowserMob proxy skipped as it is not started.");126                }127            }128            proxies.remove(threadId);129        }130        LOGGER.debug("stopProxy finished...");131    }*/132    133    // ------------------------- BOWSERMOB PROXY ---------------------134    135    private static final ConcurrentHashMap<Long, BrowserMobProxy> proxies = new ConcurrentHashMap<Long, BrowserMobProxy>();136    137    /**138     * Checking whether BROWSERMOB_PORT is declared. then it will be used as port for browsermob proxy139     * Otherwise first available port from BROWSERMOB_PORTS_RANGE will be used140     * 141     * @return port142     */143	public static int getProxyPortFromConfig() {144		if (!Configuration.get(Parameter.BROWSERMOB_PORT).isEmpty())145			return Configuration.getInt(Parameter.BROWSERMOB_PORT);146		else if (!Configuration.get(Parameter.BROWSERMOB_PORTS_RANGE).isEmpty()) {147			for (Map.Entry<Integer, Boolean> pair : proxyPortsFromRange.entrySet()) {148				if (pair.getValue()) {149					LOGGER.info("Making BrowserMob proxy port busy: " + pair.getKey());150					pair.setValue(false);151					return pair.getKey().intValue();152				}153			}154			throw new RuntimeException(155					"All ports from Parameter.BROWSERMOB_PORTS_RANGE are currently busy. Please change execution thread count");156		}157		throw new RuntimeException(158				"Neither Parameter.BROWSERMOB_PORT nor Parameter.BROWSERMOB_PORTS_RANGE are specified!");159	}160    // TODO: investigate possibility to return interface to support JettyProxy161    /**162     * start BrowserMobProxy Server163     * 164     * @return BrowserMobProxy165     * 166     */167    public static synchronized BrowserMobProxy startProxy() {168        return startProxy(getProxyPortFromConfig());169    }170    171    public static synchronized BrowserMobProxy startProxy(int proxyPort) {172        if (!Configuration.getBoolean(Parameter.BROWSERMOB_PROXY)) {173            LOGGER.debug("Proxy is disabled.");174            return null;175        }176        // integrate browserMob proxy if required here177        BrowserMobProxy proxy = null;178        long threadId = Thread.currentThread().getId();179        if (proxies.containsKey(threadId)) {180            proxy = proxies.get(threadId);181        }182        if (proxyPortsByThread.containsKey(threadId)) {183            proxyPort = proxyPortsByThread.get(threadId);184        }185        // case when proxy was already instantiated but port doesn't correspond to current device186        if (null == proxy || proxy.getPort() != proxyPort) {187            proxy = ProxyPool.createProxy();188            proxies.put(Thread.currentThread().getId(), proxy);189        }190        191        if (!proxy.isStarted()) {192            LOGGER.info("Starting BrowserMob proxy...");193        	// TODO: [VD] confirmed with MB that restart was added just in case. Maybe comment/remove?194            killProcessByPort(proxyPort);195            proxy.start(proxyPort);196        } else {197            LOGGER.info("BrowserMob proxy is already started on port " + proxy.getPort());198        }199        return proxy;200    }201    ...Source:BrowserMobTest.java  
...133    public void testRegisterProxy(String arg) {134        ProxyPool.setupBrowserMobProxy();135        int tempPort = ProxyPool.getProxy().getPort();136        ProxyPool.stopProxy();137        BrowserMobProxy proxy = ProxyPool.createProxy();138        proxy.setTrustAllServers(true);139        proxy.setMitmDisabled(false);140        ProxyPool.registerProxy(proxy);141142        ProxyPool.startProxy(tempPort);143        int actualPort = ProxyPool.getProxy().getPort();144        LOGGER.info(String.format("Checking Ports Before (%s) After (%s)", tempPort, actualPort));145        Assert.assertEquals(tempPort, actualPort, "Proxy Port before, after do not match on current thread");146    }147148    private void initialize() {149        ProxyPool.setupBrowserMobProxy();150        SystemProxy.setupProxy();151
...createProxy
Using AI Code Generation
1package com.qaprosoft.carina.browsermobproxy;2import java.io.File;3import java.io.IOException;4import java.util.Map;5import org.apache.log4j.Logger;6import org.openqa.selenium.Proxy;7import org.openqa.selenium.WebDriver;8import org.openqa.selenium.firefox.FirefoxDriver;9import org.openqa.selenium.firefox.FirefoxProfile;10import com.qaprosoft.carina.browsermobproxy.ProxyPool;11import com.qaprosoft.carina.core.foundation.utils.Configuration;12public class ProxyTest {13    private static final Logger LOGGER = Logger.getLogger(ProxyTest.class);14    public static void main(String[] args) throws IOException {15        File profileDir = new File(Configuration.get(Configuration.Parameter.PROFILE_DIR));16        FirefoxProfile profile = new FirefoxProfile(profileDir);17        WebDriver driver = new FirefoxDriver(profile);18        ProxyPool proxyPool = new ProxyPool();19        Proxy proxy = proxyPool.createProxy();20        driver.quit();21        Map<String, Object> har = proxyPool.getHar();22        System.out.println(har);23    }24}25package com.qaprosoft.carina.browsermobproxy;26import java.io.File;27import java.io.IOException;28import java.util.Map;29import org.apache.log4j.Logger;30import org.openqa.selenium.Proxy;31import org.openqa.selenium.WebDriver;32import org.openqa.selenium.firefox.FirefoxDriver;33import org.openqa.selenium.firefox.FirefoxProfile;34import com.qaprosoft.carina.browsermobproxy.ProxyPool;35import com.qaprosoft.carina.core.foundation.utils.Configuration;36public class ProxyTest {37    private static final Logger LOGGER = Logger.getLogger(ProxyTest.class);38    public static void main(String[] args) throws IOException {39        File profileDir = new File(Configuration.get(Configuration.Parameter.PROFILE_DIR));40        FirefoxProfile profile = new FirefoxProfile(profileDir);41        WebDriver driver = new FirefoxDriver(profile);42        ProxyPool proxyPool = new ProxyPool();43        Proxy proxy = proxyPool.createProxy();44        driver.quit();45        Map<String, Object> har = proxyPool.getHar();46        System.out.println(har);47    }48}createProxy
Using AI Code Generation
1ProxyPool proxyPool = new ProxyPool();2ProxyServer proxy = proxyPool.createProxy();3proxyPool.setProxy(proxy);4proxyPool.startProxy();5ProxyPool proxyPool = new ProxyPool();6ProxyServer proxy = proxyPool.createProxy();7proxyPool.setProxy(proxy);8proxyPool.startProxy();9ProxyPool proxyPool = new ProxyPool();10ProxyServer proxy = proxyPool.createProxy();11proxyPool.setProxy(proxy);12proxyPool.startProxy();13ProxyPool proxyPool = new ProxyPool();14ProxyServer proxy = proxyPool.createProxy();15proxyPool.setProxy(proxy);16proxyPool.startProxy();17ProxyPool proxyPool = new ProxyPool();18ProxyServer proxy = proxyPool.createProxy();19proxyPool.setProxy(proxy);20proxyPool.startProxy();21ProxyPool proxyPool = new ProxyPool();22ProxyServer proxy = proxyPool.createProxy();23proxyPool.setProxy(proxy);24proxyPool.startProxy();25ProxyPool proxyPool = new ProxyPool();26ProxyServer proxy = proxyPool.createProxy();27proxyPool.setProxy(proxy);28proxyPool.startProxy();29ProxyPool proxyPool = new ProxyPool();30ProxyServer proxy = proxyPool.createProxy();31proxyPool.setProxy(proxy);32proxyPool.startProxy();33ProxyPool proxyPool = new ProxyPool();34ProxyServer proxy = proxyPool.createProxy();35proxyPool.setProxy(proxy);36proxyPool.startProxy();37ProxyPool proxyPool = new ProxyPool();38ProxyServer proxy = proxyPool.createProxy();39proxyPool.setProxy(proxy);40proxyPool.startProxy();createProxy
Using AI Code Generation
1ProxyPool proxyPool = new ProxyPool();2ProxyServer proxyServer = proxyPool.createProxy();3proxyServer.start(0);4int port = proxyServer.getPort();5String ip = proxyServer.getHostName();6SeleniumProxy seleniumProxy = proxyServer.seleniumProxy();7String proxyAddress = seleniumProxy.getProxy();8int proxyPort = seleniumProxy.getPort();9String proxyAddressAndPort = seleniumProxy.getHttpProxy();10String username = seleniumProxy.getHttpProxyUsername();11String password = seleniumProxy.getHttpProxyPassword();12String sslCert = seleniumProxy.getSslCert();13String sslCertPassword = seleniumProxy.getSslCertPassword();14String sslCertKey = seleniumProxy.getSslCertKey();15String sslCertKeyPassword = seleniumProxy.getSslCertKeyPassword();16String sslCertSubject = seleniumProxy.getSslCertSubject();17String sslCertIssuer = seleniumProxy.getSslCertIssuer();18String sslCertAlgorithm = seleniumProxy.getSslCertAlgorithm();19String sslCertValidity = seleniumProxy.getSslCertValidity();20Date sslCertValidityStartDate = seleniumProxy.getSslCertValidityStart();21Date sslCertValidityEndDate = seleniumProxy.getSslCertValidityEnd();createProxy
Using AI Code Generation
1import com.qaprosoft.carina.browsermobproxy.ProxyPool;2public class 1 {3public static void main(String[] args) {4    ProxyPool proxyPool = new ProxyPool();5    int port = proxyPool.createProxy();6    System.out.println("Proxy server created on port: " + port);7}8}9import com.qaprosoft.carina.browsermobproxy.ProxyPool;10public class 2 {11public static void main(String[] args) {12    ProxyPool proxyPool = new ProxyPool();13    int port = proxyPool.getProxy();14    System.out.println("Proxy server created on port: " + port);15}16}17import com.qaprosoft.carina.browsermobproxy.ProxyPool;18public class 3 {19public static void main(String[] args) {20    ProxyPool proxyPool = new ProxyPool();21    int port = proxyPool.getProxy();22    System.out.println("Proxy server created on port: " + port);23    port = proxyPool.getProxy();24    System.out.println("Proxy server created on port: " + port);25    port = proxyPool.getProxy();26    System.out.println("Proxy server created on port: " + port);27    port = proxyPool.getProxy();28    System.out.println("Proxy server created on port: " + port);29    port = proxyPool.getProxy();30    System.out.println("Proxy server created on port: " + port);31}32}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!!
