Best Carina code snippet using com.qaprosoft.carina.browsermobproxy.rewrite.RewriteItem
Source:CustomRqFilter.java
...30public class CustomRqFilter implements RequestFilter {31 32 protected static final Logger LOGGER = Logger.getLogger(CustomRqFilter.class);33 34 private List<RewriteItem> rewrites = new ArrayList<>();35 @Override36 public HttpResponse filterRequest(HttpRequest rq, HttpMessageContents contents, HttpMessageInfo messageInfo) {37 if (rewrites.isEmpty()) {38 return null;39 }40 String reqUrl = rq.getUri();41 for (RewriteItem rewriteItem : rewrites) {42 if(reqUrl.matches(rewriteItem.getHost())) {43 // headers rewrite44 LOGGER.debug("Rewrite rule will be applied for host: ".concat(reqUrl));45 rq = applyHeaders(rq, rewriteItem.getHeaders());46 // body rewrite47 String content = contents.getTextContents();48 content.replaceAll(rewriteItem.getRegex(), rewriteItem.getReplacement());49 contents.setTextContents(content);50 }51 }52 53 return null;54 }55 56 57 /**58 * Apply headers to request59 * @param req60 * @param headers61 * @return updated request62 */63 private HttpRequest applyHeaders(HttpRequest req, List<HeaderItem> headers) {64 for (HeaderItem headerItem : headers) {65 switch (headerItem.getMethod()) {66 case ADD:67 req.headers().add(headerItem.getHeader().getKey(), headerItem.getHeader().getValue());68 break;69 case REMOVE:70 req.headers().remove(headerItem.getHeader().getKey());71 break;72 case UPDATE:73 req.headers().set(headerItem.getHeader().getKey(), headerItem.getHeader().getValue());74 break;75 default:76 break;77 }78 }79 return req;80 }81 82 public CustomRqFilter (List<RewriteItem> rewrites) {83 this.rewrites = rewrites;84 }85}...
Source:CustomRsFilter.java
...28 */29public class CustomRsFilter implements ResponseFilter {30 31 protected static final Logger LOGGER = Logger.getLogger(CustomRsFilter.class);32 private List<RewriteItem> rewrites = new ArrayList<>();33 /**34 * Update response headers35 * @param rs36 * @param headers37 */38 private void applyHeaders(HttpResponse rs, List<HeaderItem> headers) {39 for (HeaderItem headerItem : headers) {40 LOGGER.debug("Rewrite header: ".concat(headerItem.toString()));41 switch (headerItem.getMethod()) {42 case ADD:43 rs.headers().add(headerItem.getHeader().getKey(), headerItem.getHeader().getValue());44 break;45 case REMOVE:46 rs.headers().remove(headerItem.getHeader().getKey());47 break;48 case UPDATE:49 rs.headers().set(headerItem.getHeader().getKey(), headerItem.getHeader().getValue());50 break;51 default:52 break;53 }54 }55 }56 public CustomRsFilter(List<RewriteItem> rewrites) {57 this.rewrites = rewrites;58 }59 @Override60 public void filterResponse(HttpResponse response, HttpMessageContents contents, HttpMessageInfo messageInfo) {61 if (rewrites.isEmpty()) {62 return;63 }64 String reqUrl = messageInfo.getOriginalUrl();65 for (RewriteItem rewriteItem : rewrites) {66 if (reqUrl.matches(rewriteItem.getHost())) {67 // headers rewrite68 LOGGER.debug("Rewrite rule will be applied for host: ".concat(reqUrl));69 applyHeaders(response, rewriteItem.getHeaders());70 // body rewrite71 String content = contents.getTextContents();72 content.replaceAll(rewriteItem.getRegex(), rewriteItem.getReplacement());73 contents.setTextContents(content);74 }75 }76 }77}...
Source:RewriteItem.java
...15 ******************************************************************************/16package com.qaprosoft.carina.browsermobproxy.rewrite;17import java.util.ArrayList;18import java.util.List;19public class RewriteItem {20 private String host;21 private String regex;22 private String replacement;23 private List<HeaderItem> headers = new ArrayList<HeaderItem>();24 /**25 * Leave regexp empty in case you don't want to rewrite body26 * 27 * @param host28 * @param regexp29 * @param replacement30 * @param headers31 */32 public RewriteItem(final String host, final String regexp, final String replacement, final List<HeaderItem> headers) {33 this.host = host;34 this.regex = regexp;35 this.replacement = replacement;36 this.headers = headers;37 }38 public String getHost() {39 return host;40 }41 public void setHost(String host) {42 this.host = host;43 }44 public String getRegex() {45 return regex;46 }47 public void setRegex(String regex) {48 this.regex = regex;49 }50 public String getReplacement() {51 return replacement;52 }53 public void setReplacement(String replacement) {54 this.replacement = replacement;55 }56 public List<HeaderItem> getHeaders() {57 return headers;58 }59 public void setHeaders(List<HeaderItem> headers) {60 this.headers = headers;61 }62 @Override63 public String toString() {64 return "RewriteItem [host=" + host + ", regex=" + regex + ", replacement=" + replacement + ", headers=" + headers + "]";65 }66}...
RewriteItem
Using AI Code Generation
1import java.io.IOException;2import org.openqa.selenium.Proxy;3import org.openqa.selenium.WebDriver;4import org.openqa.selenium.chrome.ChromeDriver;5import org.openqa.selenium.chrome.ChromeOptions;6import org.openqa.selenium.remote.CapabilityType;7import org.openqa.selenium.remote.DesiredCapabilities;8import com.qaprosoft.carina.browsermobproxy.rewrite.RewriteItem;9import com.qaprosoft.carina.browsermobproxy.rewrite.RewriteRule;10import com.qaprosoft.carina.browsermobproxy.rewrite.RewriteRules;11import com.qaprosoft.carina.browsermobproxy.rewrite.RewriteRules.RewriteRuleType;12import net.lightbody.bmp.BrowserMobProxy;13import net.lightbody.bmp.BrowserMobProxyServer;14import net.lightbody.bmp.client.ClientUtil;15public class RewriteRuleTest {16 public static void main(String[] args) throws IOException {17 BrowserMobProxy proxy = new BrowserMobProxyServer();18 proxy.start(0);19 Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);20 DesiredCapabilities capabilities = new DesiredCapabilities();21 capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);22 ChromeOptions options = new ChromeOptions();23 options.addArguments("start-maximized");24 WebDriver driver = new ChromeDriver(options);25 proxy.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);26 proxy.newHar("yahoo.com");27 Har har = proxy.getHar();28 System.out.println("HAR file is created");29 driver.quit();30 proxy.stop();31 RewriteItem yahooRewriteItem = new RewriteItem("yahoo.com", "www.yahoo.com");32 RewriteRule yahooRewriteRule = new RewriteRule(RewriteRuleType.HOST, yahooRewriteItem);33 RewriteRules rewriteRules = new RewriteRules();34 rewriteRules.addRewriteRule(yahooRewriteRule);
RewriteItem
Using AI Code Generation
1import java.io.File;2import java.io.FileInputStream;3import java.io.FileNotFoundException;4import java.io.IOException;5import java.net.Inet4Address;6import java.net.InetAddress;7import java.net.UnknownHostException;8import java.util.ArrayList;9import java.util.List;10import org.apache.log4j.Logger;11import org.openqa.selenium.WebDriver;12import org.openqa.selenium.firefox.FirefoxDriver;13import org.openqa.selenium.remote.DesiredCapabilities;14import com.qaprosoft.carina.browsermobproxy.rewrite.RewriteItem;15import com.qaprosoft.carina.browsermobproxy.rewrite.RewriteItem.RewriteType;16import com.qaprosoft.carina.browsermobproxy.rewrite.RewriteRule;17import com.qaprosoft.carina.core.foundation.utils.Configuration;18import com.qaprosoft.carina.core.foundation.utils.R;19import com.qaprosoft.carina.core.foundation.utils.traffic.TrafficShaper;20import com.qaprosoft.carina.core.foundation.webdriver.DriverPool;21import net.lightbody.bmp.BrowserMobProxy;22import net.lightbody.bmp.BrowserMobProxyServer;23import net.lightbody.bmp.client.ClientUtil;24import net.lightbody.bmp.core.har.Har;25import net.lightbody.bmp.core.har.HarEntry;26import net.lightbody.bmp.core.har.HarNameValuePair;27import net.lightbody.bmp.core.har.HarResponse;28import net.lightbody.bmp.proxy.CaptureType;29public class Rewrite {30 private static final Logger LOGGER = Logger.getLogger(Rewrite.class);31 public static void main(String[] args) throws UnknownHostException, FileNotFoundException, IOException {32 BrowserMobProxy proxy = new BrowserMobProxyServer();33 proxy.setTrustAllServers(true);34 proxy.start(0);35 org.openqa.selenium.Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);36 DesiredCapabilities capabilities = new DesiredCapabilities();37 capabilities.setCapability("proxy", seleniumProxy);38 WebDriver driver = new FirefoxDriver(capabilities);39 proxy.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);40 proxy.newHar("yahoo.com");
RewriteItem
Using AI Code Generation
1import com.qaprosoft.carina.browsermobproxy.rewrite.RewriteItem;2import com.qaprosoft.carina.browsermobproxy.rewrite.RewriteRule;3import com.qaprosoft.carina.core.foundation.utils.Configuration;4import com.qaprosoft.carina.core.foundation.utils.R;5import com.qaprosoft.carina.core.foundation.webdriver.DriverPool;6import com.qaprosoft.carina.core.foundation.webdriver.decorator.ExtendedWebElement;7import com.qaprosoft.carina.core.foundation.webdriver.listener.EventFiringWebDriver;8import com.qaprosoft.carina.core.foundation.webdriver.listener.EventFiringWebDriverFactory;9import com.qaprosoft.carina.core.foundation.webdriver.listener.MobileEventFiringWebDriver;10import com.qaprosoft.carina.core.foundation.webdriver.listener.MobileEventFiringWebDriverFactory;11import com.qaprosoft.carina.core.foundation.webdriver.listener.MobileType;12import com.qaprosoft.carina.core.foundation.webdriver.listener.WebDriverEventListener;13import com.qaprosoft.carina.core.foundation.webdriver.listener.WebDriverListener;14import com.qaprosoft.carina.core.foundation.webdriver.listener.WebDriverListenerFactory;15import com.qaprosoft.carina.core.foundation.webdriver.listener.WebDriverType;16import com.qaprosoft.carina.core.foundation.webdriver.locator.ExtendedBy;17import com.qaprosoft.carina.core.foundation.webdriver.locator.ExtendedFieldDecorator;18import com.qaprosoft.carina.core.foundation.webdriver.locator.ICustomLocator;19import com.qaprosoft.carina.core.foundation.webdriver.locator.Locator;20import com.qaprosoft.carina.core.foundation.webdriver.locator.LocatorType;21import com.qaprosoft.carina.core.foundation.webdriver.locator.LocatorUtil;22import com.qaprosoft.carina.core.foundation.webdriver.locator.MobileLocator;23import com.qaprosoft.carina.core.foundation.webdriver.locator.MobileLocatorType;24import com.qaprosoft.carina.core.foundation.webdriver.locator.MobileLocatorUtil;25import com.qaprosoft.carina.core.foundation.webdriver.locator.MultipleLocator;26import com.qaprosoft.carina.core.foundation.webdriver.locator.MultipleLocatorType;27import com.qaprosoft.carina.core.foundation.webdriver.locator.MultipleLocatorUtil;28import com.qaprosoft.carina.core.foundation.webdriver.locator.SmartLocator;29import com.qaprosoft.carina.core.foundation.webdriver.locator.SmartLocatorType;30import com.qaprosoft.carina.core.foundation.webdriver.locator.SmartLocatorUtil;31import com.qaprosoft.carina.core.foundation.webdriver.report.DriverHelper;32import com.qaprosoft.car
RewriteItem
Using AI Code Generation
1RewriteItem rewriteItem = new RewriteItem();2RewriteItem rewriteItem = new RewriteItem();3rewriteItem.setRedirectType(RedirectType.PERMANENT);4RewriteItem rewriteItem = new RewriteItem();5rewriteItem.setRedirectType(RedirectType.TEMPORARY);6RewriteItem rewriteItem = new RewriteItem();7rewriteItem.setRedirectType(RedirectType.TEMPORARY);8rewriteItem.setRegex(true);9RewriteItem rewriteItem = new RewriteItem();10rewriteItem.setRedirectType(RedirectType.TEMPORARY);11rewriteItem.setRegex(true);12rewriteItem.setCaseInsensitive(true);13RewriteItem rewriteItem = new RewriteItem();14rewriteItem.setRedirectType(RedirectType.TEMPORARY);15rewriteItem.setRegex(true);16rewriteItem.setCaseInsensitive(true);17rewriteItem.setFirstMatchOnly(true);18RewriteItem rewriteItem = new RewriteItem();19rewriteItem.setRedirectType(RedirectType.TEMPORARY);20rewriteItem.setRegex(true);21rewriteItem.setCaseInsensitive(true);
RewriteItem
Using AI Code Generation
1package com.qaprosoft.carina.browsermobproxy.rewrite;2import java.io.IOException;3import java.util.HashMap;4import java.util.Map;5import org.apache.log4j.Logger;6import org.openqa.selenium.WebDriver;7import com.qaprosoft.carina.core.foundation.webdriver.DriverPool;8import com.qaprosoft.carina.core.foundation.webdriver.decorator.ExtendedWebElement;9import com.qaprosoft.carina.core.foundation.webdriver.decorator.HtmlElementLocatorFactory;10import com.qaprosoft.carina.core.foundation.webdriver.decorator.PageFactory;11import net.lightbody.bmp.BrowserMobProxy;12import net.lightbody.bmp.BrowserMobProxyServer;13import net.lightbody.bmp.client.ClientUtil;14import net.lightbody.bmp.core.har.Har;15import net.lightbody.bmp.core.har.HarEntry;16import net.lightbody.bmp.core.har.HarNameValuePair;17import net.lightbody.bmp.core.har.HarRequest;18import net.lightbody.bmp.core.har.HarResponse;19import net.lightbody.bmp.proxy.CaptureType;20import net.lightbody.bmp.util.HttpMessageContents;21import net.lightbody.bmp.util.HttpMessageInfo;22public class TestProxy {23 private static final Logger LOGGER = Logger.getLogger(TestProxy.class);24 public static void main(String[] args) throws Exception {25 BrowserMobProxy proxy = new BrowserMobProxyServer();26 proxy.start(0);27 org.openqa.selenium.Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);28 WebDriver driver = DriverPool.getDriver("chrome", seleniumProxy);29 proxy.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);30 proxy.newHar("google.com");31 Har har = proxy.getHar();32 for (HarEntry entry : har.getLog().getEntries()) {33 HarRequest request = entry.getRequest();34 HarResponse response = entry.getResponse();35 String url = request.getUrl();36 LOGGER.info("URL: " + url);37 if (url.contains("google-analytics.com")) {38 Map<String, String> headers = new HashMap<String, String>();39 for (HarNameValuePair header : response.get
RewriteItem
Using AI Code Generation
1import java.io.File;2import java.io.IOException;3import java.net.InetSocketAddress;4import java.net.MalformedURLException;5import java.net.URL;6import java.util.ArrayList;7import java.util.List;8import java.util.concurrent.TimeoutException;9import org.openqa.selenium.Proxy;10import org.openqa.selenium.WebDriver;11import org.openqa.selenium.firefox.FirefoxDriver;12import org.openqa.selenium.firefox.FirefoxProfile;13import org.openqa.selenium.remote.CapabilityType;14import org.openqa.selenium.remote.DesiredCapabilities;15import org.openqa.selenium.support.ui.ExpectedCondition;16import org.openqa.selenium.support.ui.WebDriverWait;17import com.qaprosoft.carina.browsermobproxy.rewrite.RewriteItem;18import com.qaprosoft.carina.browsermobproxy.rewrite.RewriteUtil;19import net.lightbody.bmp.BrowserMobProxy;20import net.lightbody.bmp.BrowserMobProxyServer;21import net.lightbody.bmp.client.ClientUtil;22import net.lightbody.bmp.core.har.Har;23import net.lightbody.bmp.core.har.HarEntry;24import net.lightbody.bmp.core.har.HarNameValuePair;25import net.lightbody.bmp.core.har.HarRequest;26import net.lightbody.bmp.core.har.HarResponse;27import net.lightbody.bmp.proxy.CaptureType;28public class RewriteResponseTest {29 public static void main(String[] args) throws MalformedURLException, IOException, TimeoutException, InterruptedException {30 BrowserMobProxy proxy = new BrowserMobProxyServer();31 proxy.start(0);32 Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);33 DesiredCapabilities capabilities = new DesiredCapabilities();34 capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);35 FirefoxProfile profile = new FirefoxProfile();36 profile.setAcceptUntrustedCertificates(true);37 profile.setAssumeUntrustedCertificateIssuer(false);38 capabilities.setCapability(FirefoxDriver.PROFILE, profile);39 WebDriver driver = new FirefoxDriver(capabilities);40 proxy.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);41 proxy.newHar("yahoo.com");
RewriteItem
Using AI Code Generation
1RewriteItem rewriteItem = new RewriteItem();2rewriteItem.setUrlReplacementType("host");3rewriteItem.setUrlReplacementType("host");4rewriteItem.setMethod("GET");5rewriteItem.setMethod("GET");6rewriteItem.setStatusCode(200);7rewriteItem.setStatusCode(200);8rewriteItem.setResponseHeader("Content-Type", "text/html");9rewriteItem.setResponseHeader("Content-Type", "text/html");10rewriteItem.setResponseBody("Hello World");11rewriteItem.setResponseBody("Hello World");12RewriteRule rewriteRule = new RewriteRule();13rewriteRule.setUrlReplacementType("host");14rewriteRule.setUrlReplacementType("host");15rewriteRule.setMethod("GET");16rewriteRule.setMethod("GET");17rewriteRule.setStatusCode(200);18rewriteRule.setStatusCode(200);19rewriteRule.setResponseHeader("Content-Type", "text/html");20rewriteRule.setResponseHeader("Content-Type", "text/html");21rewriteRule.setResponseBody("Hello World");22rewriteRule.setResponseBody("Hello World");23RewriteRule rewriteRule = new RewriteRule();24rewriteRule.setUrlReplacementType("host");25rewriteRule.setUrlReplacementType("host");26rewriteRule.setMethod("GET");27rewriteRule.setMethod("GET");28rewriteRule.setStatusCode(200);29rewriteRule.setStatusCode(200);30rewriteRule.setResponseHeader("Content-Type", "text/html
RewriteItem
Using AI Code Generation
1public class RewriteItem {2 private String url;3 private String method;4 private String contentType;5 private String content;6 private String redirectUrl;7 private int responseCode;8 private String responseContentType;9 private String responseBody;10 private String responseHeaders;11 public RewriteItem(String url, String method, String contentType, String content, String redirectUrl, int responseCode, String responseContentType, String responseBody, String responseHeaders) {12 this.url = url;13 this.method = method;14 this.contentType = contentType;15 this.content = content;16 this.redirectUrl = redirectUrl;17 this.responseCode = responseCode;18 this.responseContentType = responseContentType;19 this.responseBody = responseBody;20 this.responseHeaders = responseHeaders;21 }22 public String getUrl() {23 return url;24 }25 public String getMethod() {26 return method;27 }28 public String getContentType() {29 return contentType;30 }31 public String getContent() {32 return content;33 }34 public String getRedirectUrl() {35 return redirectUrl;36 }37 public int getResponseCode() {38 return responseCode;39 }40 public String getResponseContentType() {41 return responseContentType;42 }43 public String getResponseBody() {44 return responseBody;45 }46 public String getResponseHeaders() {47 return responseHeaders;48 }49}50public class RewriteItem {51 private String url;52 private String method;53 private String contentType;54 private String content;55 private String redirectUrl;56 private int responseCode;57 private String responseContentType;58 private String responseBody;59 private String responseHeaders;60 public RewriteItem(String url, String method, String contentType, String content, String redirectUrl, int responseCode, String responseContentType, String responseBody, String responseHeaders) {61 this.url = url;62 this.method = method;63 this.contentType = contentType;64 this.content = content;65 this.redirectUrl = redirectUrl;66 this.responseCode = responseCode;67 this.responseContentType = responseContentType;68 this.responseBody = responseBody;69 this.responseHeaders = responseHeaders;70 }71 public String getUrl() {72 return url;73 }74 public String getMethod() {75 return method;76 }77 public String getContentType() {78 return contentType;
RewriteItem
Using AI Code Generation
1import java.io.IOException;2import org.apache.log4j.Logger;3import org.openqa.selenium.By;4import org.openqa.selenium.WebDriver;5import org.openqa.selenium.WebElement;6import org.openqa.selenium.chrome.ChromeDriver;7import org.openqa.selenium.chrome.ChromeOptions;8import org.openqa.selenium.remote.DesiredCapabilities;9import org.openqa.selenium.support.ui.ExpectedConditions;10import org.openqa.selenium.support.ui.WebDriverWait;11import com.qaprosoft.carina.browsermobproxy.rewrite.RewriteItem;12import com.qaprosoft.carina.browsermobproxy.rewrite.RewriteRule;13import com.qaprosoft.carina.browsermobproxy.rewrite.RewriteRuleList;14import com.qaprosoft.carina.browsermobproxy.rewrite.RewriteRuleList.RuleType;15import com.qaprosoft.carina.browsermobproxy.rewrite.RewriteUtil;16import com.qaprosoft.carina.browsermobproxy.rewrite.RewriteUtil.RewriteType;17import net.lightbody.bmp.BrowserMobProxy;18import net.lightbody.bmp.BrowserMobProxyServer;19import net.lightbody.bmp.client.ClientUtil;20import net.lightbody.bmp.core.har.Har;21import net.lightbody.bmp.core.har.HarEntry;22import net.lightbody.bmp.core.har.HarNameValuePair;23import net.lightbody.bmp.core.har.HarRequest;24import net.lightbody.bmp.proxy.CaptureType;25public class RewriteTest {26 private static final Logger LOGGER = Logger.getLogger(RewriteTest.class);27 public static void main(String[] args) throws IOException {28 BrowserMobProxy proxy = new BrowserMobProxyServer();29 proxy.start(0);30 org.openqa.selenium.Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);31 DesiredCapabilities capabilities = new DesiredCapabilities();32 capabilities.setCapability("proxy", seleniumProxy);33 ChromeOptions options = new ChromeOptions();34 options.addArguments("--ignore-certificate-errors");35 capabilities.setCapability(ChromeOptions.CAPABILITY, options);36 WebDriver driver = new ChromeDriver(capabilities);37 proxy.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);
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!!