使用Selenium,Maven,Browsermob和Browserstack通过Har文件提取网络请求

时间:2019-02-19 11:23:07

标签: java maven selenium-webdriver browserstack browsermob-proxy

我正在尝试在硒设置中使用Browsermob测试我们的Analytics(分析)。

但是,当我运行测试时,我尝试创建的Har文件根本没有创建,或者其中没有可解析并显示在命令提示符下的信息。

package pageObjects;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;
import java.rmi.UnexpectedException;

import org.junit.Assert;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Parameters;

import net.lightbody.bmp.BrowserMobProxy;
import net.lightbody.bmp.BrowserMobProxyServer;
import net.lightbody.bmp.core.har.Har;



public class setUp {

    protected static ThreadLocal<WebDriver> webDriver = new ThreadLocal<WebDriver>();

    public static final String WebURL = Credentials.PropertyFile.getURL();
    public static final String Environment = System.getProperty("Environment");
    static public BrowserMobProxy proxy = new BrowserMobProxyServer(); // getting browsermob proxy

    public static final String ANSI_RESET = "\u001B[0m";
    public static final String ANSI_BLACK = "\u001B[30m";
    public static final String ANSI_RED = "\u001B[31m";
    public static final String ANSI_GREEN = "\u001B[32m";
    public static final String ANSI_YELLOW = "\u001B[33m";
    public static final String ANSI_BLUE = "\u001B[34m";
    public static final String ANSI_PURPLE = "\u001B[35m";
    public static final String ANSI_CYAN = "\u001B[36m";
    public static final String ANSI_WHITE = "\u001B[37m";

     public WebDriver getWebDriver() {
            return webDriver.get();
        }


    @BeforeClass (alwaysRun = true)
    @Parameters(value = { "browser", "version", "os", "os_version", "project", "build" })
    protected void setCapabilities(String browser, String version, String os, String os_version, String project,
            String build) throws MalformedURLException, UnexpectedException {

        DesiredCapabilities capability = new DesiredCapabilities();
        capability.setCapability("browserName", browser);
        capability.setCapability("browserVersion", version);
        capability.setCapability("os", os);
        capability.setCapability("os_version", os_version);
        capability.setCapability("project", project);
        capability.setCapability("build", build);
        capability.setCapability("browserstack.debug", "true");
        capability.setCapability("browserstack.console", "warnings");
        capability.setCapability("resolution", "1920x1080");
        capability.setCapability("browserstack.local", "true");
        capability.setCapability("browserstack.localIdentifier", "Test123");
        capability.setCapability("browserstack.safari.enablePopups", "true");
        capability.setCapability("browserstack.safari.allowAllCookies", "true");
        capability.setCapability(CapabilityType.SUPPORTS_JAVASCRIPT, "true");
        capability.setCapability(CapabilityType.ACCEPT_SSL_CERTS, "true");
        capability.setCapability(CapabilityType.ACCEPT_INSECURE_CERTS, "true");
        capability.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, "true");
        capability.setCapability("browserstack.ie.enablePopups", "true");
        capability.setCapability("browserstack.edge.enablePopups", "true");
        capability.setCapability("unexpectedAlertBehaviour", "accept");
        capability.setCapability("ignoreProtectedModeSettings", "true");
        capability.setCapability("enablePersistentHover", "true");
        capability.setCapability(InternetExplorerDriver.IGNORE_ZOOM_SETTING, "true");
        capability.setCapability("browserstack.use_w3c", "true");
        capability.setCapability("browserstack.maskSendKeys", "true");

        Credentials.PropertyFile.setCapabilities(capability);

        webDriver.set(new RemoteWebDriver(new URL(Credentials.PropertyFile.getBrowserstackURL()), capability));
        EnvironmentErrors();



    }

    // Sending the Test Setup for Staging or Production //

    public static void TestSetup() throws InterruptedException, UnknownHostException {

        Capabilities cap = ((RemoteWebDriver) webDriver.get()).getCapabilities();
        String browserName = cap.getBrowserName().toString();

        proxy.newHar("Analytics"); // creating new HAR

        webDriver.get().get(WebURL);
        Thread.sleep(10000);

        System.out.println(ANSI_WHITE + webDriver.get().getTitle() + ("  Being tested on: ") + ANSI_GREEN + Environment
                + ANSI_WHITE + (" and ") + ANSI_GREEN + browserName + (" browser!"));
        webDriver.get().manage().window().maximize();
        Thread.sleep(5000);

        // Retrieve Har
                Har har = proxy.getHar();

                // Write Har to string
                java.io.StringWriter writer = new java.io.StringWriter();
                try {
                    har.writeTo(writer);
                } catch (IOException e) {
                    e.printStackTrace();
                }

                String harAsString = writer.toString();
                System.out.println(IndexElements.ANSI_BLUE + "=======CONSOLE=======" + harAsString);

    }

    // Environment Error Messages//
    public static void EnvironmentErrors() {
        try {
            // Verify if the environment field is empty, if so then print out line.
            if (Environment == null) {

                Assert.fail(ANSI_RED
                        + "Please ensure that you have filled in the correct Environment to test. Use -DEnvironment=XXXXXX (Staging or Production) within your terminal code");

            } else if (Environment.equals("")) {

                Assert.fail(ANSI_RED + "Please ensure that you have filled in the correct Environment to test.");
            }
        } catch (Exception Nothing) {
            // Do nothing

        }
    }

}

package Sonata_Portal_Enterprise;

import java.net.UnknownHostException;

import org.openqa.selenium.Proxy;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;

import net.lightbody.bmp.client.ClientUtil;
import net.lightbody.bmp.proxy.CaptureType;
import pageObjects.indexPage;

@SuppressWarnings("unused")
public class testIndex extends indexPage {

    @BeforeSuite(alwaysRun = true)

    public static void main() throws InterruptedException, UnknownHostException {

        DesiredCapabilities capability = new DesiredCapabilities();
        Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);
        capability.setCapability(CapabilityType.PROXY, seleniumProxy);
        proxy.setHarCaptureTypes(CaptureType.getAllContentCaptureTypes());
        proxy.enableHarCaptureTypes(CaptureType.getAllContentCaptureTypes());
        proxy.start();
    }

    @BeforeClass(alwaysRun = true)
    public static void setUp() throws InterruptedException, UnknownHostException {

        TestSetup();
        //String session = ((RemoteWebDriver) webDriver.get()).getSessionId().toString();
        //System.out.println(session);

    }

    @Test(groups = { "iFrame" }, priority = 1)

    public void iFrameVerification() throws InterruptedException {

        iFrame();
    }

    @Test(groups = { "Hero Elements" }, priority = 2)

    public void HeroVerification() {
        HeaderVerify();
        DelayIDVerify();
        LogSwitchVerify();
        RewardStickerVerify();
        AklamioLinkVerify();
    }

    @Test(groups = { "How It Works" }, priority = 3)

    public void HIWVerification() {

        HIWVerify();
    }

    @Test(groups = { "Products" }, priority = 4)

    public void ProductVerification() {

        ProductVerify();
    }

    @AfterClass(alwaysRun = true)
    public void TearDown() throws InterruptedException {
        webDriver.get().quit();

    }

    @AfterSuite(alwaysRun = true)
    public void proxyDown() throws InterruptedException {

        proxy.stop();
    }
}

因此,当运行所有这些命令时,它运行良好,但是我在控制台中得到了一个空的har文件。

=======CONSOLE======={"log":{"version":"1.2","creator":{"name":"BrowserMob Proxy","version":"2.1.5","comment":""},"pages":[{"id":"Analytics","startedDateTime":"2019-02-21T14:34:09.546Z","title":"Analytics","pageTimings":{"comment":""},"comment":""}],"entries":[],"comment":""}}

2 个答案:

答案 0 :(得分:1)

从性能日志中获取“消息” JSONObject

private static JSONArray getPerfEntryLogs(WebDriver driver) {
    LogEntries logEntries = driver.manage().logs().get(LogType.PERFORMANCE);
    JSONArray perfJsonArray = new JSONArray();
    logEntries.forEach(entry -> {
        JSONObject messageJSON = new JSONObject(entry.getMessage()).getJSONObject("message");
        perfJsonArray.put(messageJSON);
    });
    return perfJsonArray;
}

通过PerfLogs

public static void getHAR(WebDriver driver, String fileName) throws IOException {
    String destinationFile = "/HARs/" + fileName + ".har";
    ((JavascriptExecutor) driver).executeScript(
            "!function(e,o){e.src=\"https://cdn.jsdelivr.net/gh/Ankit3794/chrome_har_js@master/chromePerfLogsHAR.js\",e.onload=function(){jQuery.noConflict(),console.log(\"jQuery injected\")},document.head.appendChild(e)}(document.createElement(\"script\"));");
    File file = new File(destinationFile);
    file.getParentFile().mkdirs();
    FileWriter harFile = new FileWriter(file);
    harFile.write((String) ((JavascriptExecutor) driver).executeScript(
            "return module.getHarFromMessages(arguments[0])", getPerfEntryLogs(driver).toString()));
    harFile.close();
}

在下面的要点评论中查看完整的说明

https://gist.github.com/Ankit3794/01b63199bd7ed4f2539a088463e54615#gistcomment-3126071

答案 1 :(得分:0)

我在这里做了一个完整的帖子:

https://stackoverflow.com/a/54787791/2129578

不是通过Firefox,而是为了获取大量信息,这可能会使您改用chrome。

{
    "_id" : ObjectId("5c6809bfeee81cdc4953365f"),
    "message" : {
        "method" : "Network.responseReceived",
        "params" : {
            "frameId" : "B8EA5F56374687AEB539AB181FDCED99",
            "loaderId" : "52695EFAAFD219AE98D5E8B6C2117E80",
            "requestId" : "1000056404.62",
            "response" : {
                "connectionId" : 310,
                "connectionReused" : false,
                "encodedDataLength" : 422,
                "fromDiskCache" : false,
                "fromServiceWorker" : false,
                "headers" : {
                    "access-control-allow-credentials" : "true",
                    "access-control-allow-headers" : "Origin, Content-Type, X-Auth-Token, Credentials",
                    "access-control-allow-methods" : "GET, POST",
                    "access-control-allow-origin" : "https://www.zoopla.co.uk",
                    "age" : "4220",
                    "allow" : "POST",
                    "content-length" : "4",
                    "content-type" : "text/html; charset=utf-8",
                    "date" : "Sat, 16 Feb 2019 11:51:28 GMT",
                    "etag" : "W/\"4-Yf+Bwwqjx254r+pisuO9HfpJ6FQ\"",
                    "status" : "200",
                    "via" : "1.1 c76a5a41a8483a9e5dcccdfeb87a16ca.cloudfront.net (CloudFront)",
                    "x-amz-cf-id" : "43bvMhO44Tnthck_QsrbPAfdfK_1DqBQ3jF3s_HS4sSS4lvPjNCf7A==",
                    "x-cache" : "Hit from cloudfront",
                    "x-powered-by" : "Express"
                },
                "mimeType" : "text/html",
                "protocol" : "h2",
                "remoteIPAddress" : "54.230.202.6",
                "remotePort" : 443,
                "requestHeaders" : {
                    ":authority" : "rq6hpdqxaej27lz.api.zpg.co.uk",
                    ":method" : "OPTIONS",
                    ":path" : "/tachyon/PageLoaded",
                    ":scheme" : "https",
                    "accept" : "*/*",
                    "accept-encoding" : "gzip, deflate, br",
                    "access-control-request-headers" : "credentials",
                    "access-control-request-method" : "POST",
                    "origin" : "https://www.zoopla.co.uk",
                    "referer" : "https://www.zoopla.co.uk/",
                    "user-agent" : "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) HeadlessChrome/72.0.3626.109 Safari/537.36"
                },
                "securityDetails" : {
                    "certificateId" : 0,
                    "certificateTransparencyCompliance" : "unknown",
                    "cipher" : "AES_128_GCM",
                    "issuer" : "Amazon",
                    "keyExchange" : "ECDHE_RSA",
                    "keyExchangeGroup" : "P-256",
                    "protocol" : "TLS 1.2",
                    "sanList" : [ 
                        "rq6hpdqxaej27lz.api.zpg.co.uk"
                    ],
                    "signedCertificateTimestampList" : [],
                    "subjectName" : "rq6hpdqxaej27lz.api.zpg.co.uk",
                    "validFrom" : 1529971200,
                    "validTo" : 1564142400
                },
                "securityState" : "secure",
                "status" : 200,
                "statusText" : "",
                "timing" : {
                    "connectEnd" : 111.609,
                    "connectStart" : 38.831,
                    "dnsEnd" : 38.831,
                    "dnsStart" : 0.11,
                    "proxyEnd" : -1,
                    "proxyStart" : -1,
                    "pushEnd" : 0,
                    "pushStart" : 0,
                    "receiveHeadersEnd" : 152.249,
                    "requestTime" : 149716.097654,
                    "sendEnd" : 111.818,
                    "sendStart" : 111.735,
                    "sslEnd" : 111.605,
                    "sslStart" : 68.456,
                    "workerReady" : -1,
                    "workerStart" : -1
                },
                "url" : "https://rq6hpdqxaej27lz.api.zpg.co.uk/tachyon/PageLoaded"
            },
            "timestamp" : 149716.270334,
            "type" : "XHR"
        }
    },
    "webview" : "B8EA5F56374687AEB539AB181FDCED99"
}

在执行页面请求时,您将从浏览器的“网络”标签中为每次调用获得一个类似的对象。

检查一下。