Skip to content
This repository has been archived by the owner on Nov 23, 2021. It is now read-only.

TrafficAnalyzer

Mikołaj Mański edited this page Aug 30, 2016 · 6 revisions

##TrafficAnalyzer

TrafficAnalyzer provides a set of tools that lets you capture and analyze network traffic between browser controlled by Bobcat and the internet.

##How to start? Add dependencies to your POM file:

<dependency>
    <groupId>com.cognifide.qa.bb</groupId>
    <artifactId>bb-traffic</artifactId>
    <version>${bb.version}</version>
</dependency>

Enable proxy by property:

proxy.enabled=true

Install TrafficModule in GuiceModule

install(new TrafficModule());

##Write a test

###JUnit In order to perform online traffic analysis, use the TrafficAnalyzer class. Its method analyzeTraffic spawns a new thread, which waits for desired request and returns Future object. The object can be used to monitor current state of analysis: method get() waits until the request or timeout occurs and isDone() don't wait but simply return true if the analysis has already finished (regardless the result).

Example:

import com.cognifide.qa.bb.proxy.analyzer.TrafficAnalyzer;
//..

  @Inject
  private TrafficAnalyzer analyzerUnderTest;

  @Test
  public void shouldReportThatExpectedCallWasPresent() throws ExecutionException, InterruptedException {

    //Prepare predicate
    Map<String, String> queryStringEntries = new HashMap<>();
    queryStringEntries.put("id", "GTM-W2M8HH");
    Future<Boolean> analysisResult = analyzerUnderTest.analyzeTraffic("/gtm.js", queryStringEntries, 10);

    //Open page in webDriver
    webDriver.get("http://www.cognifide.com/contact-us");

    //Check results
    assertThat(analysisResult.get(), is(true));
    }

You may also created more sophisticated conditions, using RequestPredicateImpl object and the second variant of the analyzeTraffic() method.

###Cucumber Traffic Log Analysis lets you first capture a number of requests sent from the browser and then analyze them one-by-one.

In order to analyze traffic in Cucumber scenarios you should use @RecordTraffic annotation and TrafficLogAnalyzer object (injected by Guice).

Traffic between the browser and the Internet will be intercepted during execution of the step annotated @RecordTraffic.

You should examine the captured log in the @Then step using TrafficLogAnalyzer. Use methods from TrafficLogPredicate class to create conditions that will be checked against the captured traffic entries.

import static com.cognifide.qa.bb.proxy.record.TrafficLogPredicate.pathStartsWith;
import static com.cognifide.qa.bb.proxy.record.TrafficLogPredicate.queryParamWithValueExists;
import static com.cognifide.qa.bb.proxy.record.TrafficLogPredicate.urlStartsWith;
 
import com.cognifide.qa.bb.proxy.record.TrafficLogAnalyzer;
import com.cognifide.qa.bb.traffic.aspects.RecordTraffic;
// ...
@Inject
private TrafficLogAnalyzer trafficLogAnalyzer; 
// ...
@Given("^I have opened Feedback page with capture$")
@RecordTraffic
public void i_have_opened_Feedback_page_with_capture() throws Throwable {
    feedbackPage.open();
    feedbackPage.isDisplayed();
}
 
@Then("stores init has fired")
public void stores_init_has_fired() {
//Check that request path starts with a parameter and contains query parameter
    trafficLogAnalyzer.assertContains(
            pathStartsWith("/etc/clientcontext/default/content/jcr:content/stores.init.js").add(
            queryParamWithValueExists("path", "/content/geometrixx/en/toolbar/feedback")
    ));
}

Getting started with Bobcat

  1. Getting started

AEM Related Features

  1. Authoring tutorial - Classic
  1. AEM Classic Authoring Advanced usage
  1. Authoring tutorial - Touch UI
Clone this wiki locally