Извлечение сетевых запросов через файл Har с использованием Selenium, Maven, Browsermob и Browserstack - PullRequest
0 голосов
/ 19 февраля 2019

Я пытаюсь протестировать нашу аналитику с помощью Browsermob в нашей настройке селена.

Однако, когда я запускаю тесты, файл 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":""}}

1 Ответ

0 голосов
/ 20 февраля 2019

Я сделал полный пост здесь:

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

Не для Firefox, но для объема информации, которую вы можете получить, это может заставить вас использовать хром вместо.

{
    "_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"
}

Когда вы выполняете запрос страницы, вы будете получать подобный объект для каждого вызова на вкладке сети браузера.

Проверьте это.

...