Jacobvu84 / serenity-pageobject-junit-webdriver

4 stars 1 forks source link

Capturing Network Traffic #76

Open Jacobvu84 opened 4 years ago

Jacobvu84 commented 4 years ago
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import net.lightbody.bmp.BrowserMobProxy;
import net.lightbody.bmp.core.har.Har;
import net.lightbody.bmp.core.har.HarEntry;
import net.lightbody.bmp.proxy.CaptureType;
import net.thucydides.browsermob.fixtureservices.BrowserMobFixtureService;
import net.thucydides.core.fixtureservices.ClasspathFixtureProviderService;
import net.thucydides.core.fixtureservices.FixtureService;

public class BrowserMobHelper {

    private static final Logger LOGGER = LoggerFactory.getLogger(BrowserMobHelper.class);

    private static BrowserMobHelper ourInstance = new BrowserMobHelper();
    private static BrowserMobProxy proxy;

    public static BrowserMobHelper getInstance() {
        if (proxy == null) {
            proxy = getProxyServer();
            LOGGER.info("=== Proxy has been started ===");
        }
        return ourInstance;
    }

    public static BrowserMobProxy getProxyServer() {
        ClasspathFixtureProviderService classpathFixtureProviderService = new ClasspathFixtureProviderService();
        List<FixtureService> fixtureServices = classpathFixtureProviderService.getFixtureServices();

        for (FixtureService fixtureService : fixtureServices) {
            if (fixtureService instanceof BrowserMobFixtureService) {
                BrowserMobFixtureService service = (BrowserMobFixtureService) fixtureService;
                service.setup();
                proxy = service.getProxyServer();
                return proxy;
            }
        }
        return null;
    }

    public BrowserMobHelper startRecording() {
        proxy.enableHarCaptureTypes(CaptureType.getAllContentCaptureTypes() );
        proxy.newHar("vuthelinh.com");
        return this;
    }

    public Har stopRecording() {
        Har har = proxy.getHar();
        if (har != null) {
            LOGGER.info("=== Proxy end Har ===");

            List<HarEntry> entries = har.getLog().getEntries();
            for(HarEntry entry :entries){
                LOGGER.info("===>>>>>>>>>>: "+entry.getRequest().getUrl());
            }

            return proxy.endHar();
        }
        return null;
    }

    public void stopProxy() {
        if (proxy != null) {
            proxy.stop();
            LOGGER.info("=== Proxy is stopped ===");
        }
    }

    public BrowserMobHelper overwriteGitHubResponse() {
        proxy.addResponseFilter((response, contents, messageInfo) -> {
            if (messageInfo.getUrl().contains("https://api.github.com/search/repositories")
                    && messageInfo.getUrl().contains("kotlin")) {
                contents.setTextContents(contents.getTextContents().replace("kotlin", "shmotlin"));
            }
        });

        return this;
    }
}
Jacobvu84 commented 4 years ago
....
 private BrowserMobHelper bmpHelper = BrowserMobHelper.getInstance();
....

 bmpHelper.startRecording();
......
......

try {
         bmpHelper.stopRecording().writeTo(new File(System.currentTimeMillis()+"_jacob.har"));
         bmpHelper.stopProxy();
} catch (IOException e) {
         e.printStackTrace();
}
.....
Jacobvu84 commented 4 years ago
Jacobvu84 commented 4 years ago
openssl x509 -inform pem -in ca-certificate-rsa.cer -outform der -out ca-certificate-rsa.pem

issuecomment

Jacobvu84 commented 4 years ago

HAR viewer: https://chrome.google.com/webstore/detail/http-archive-viewer/ebbdbdmhegaoooipfnjikefdpeoaidml/related

Jacobvu84 commented 4 years ago
import net.lightbody.bmp.BrowserMobProxy;
import net.lightbody.bmp.BrowserMobProxyServer;
import net.lightbody.bmp.client.ClientUtil;
import net.lightbody.bmp.core.har.Har;
import net.lightbody.bmp.core.har.HarEntry;
import net.lightbody.bmp.core.har.HarRequest;
import net.lightbody.bmp.core.har.HarResponse;
......

 for (HarEntry entry : har.getLog().getEntries()) {
          HarRequest request = entry.getRequest();
          HarResponse response = entry.getResponse();

         if(response.getStatus() == 500){
                    Assert.fail(request.getUrl() + " returns 500 error");
                }

                System.out.println(response.getStatus() + " : " + request.getUrl()
                        + ", " + entry.getTime() + "ms");

                assertThat(response.getStatus(), is(200));
            }