Skip to content

Commit

Permalink
#19: Now transforming HAR in monitor.
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelkiessling committed Mar 6, 2017
1 parent e1410e6 commit 382bbb8
Show file tree
Hide file tree
Showing 8 changed files with 1,198 additions and 7 deletions.
1 change: 0 additions & 1 deletion bin/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
$testcaseRepository = new TestcaseRepository($dbConnection, $logger);

$runner = new Runner($testcaseRepository, '/var/tmp', $testcaseId);
$runner->prepare();

$logger->info('About to start Selenium run...');
$testresultModel = $runner->run();
Expand Down
61 changes: 61 additions & 0 deletions src/JourneyMonitor/Monitor/JobRunner/HarTransformer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

declare(strict_types=1);

namespace JourneyMonitor\Monitor\JobRunner;

class HarTransformer
{
/**
* @param string $originalHar The original HAR JSON structure
* @param string[] $urls Array of URLs that defines where to split the HAR into multiple pages
* @return string The transformed HAR JSON structure
*/
public function splitIntoMultiplePages(string $originalHar, $urls)
{
$harObject = json_decode($originalHar);
$urls = $this->normalizeUrls($urls);

$pageStartedDateTimes = [];
$pageStartedDateTimes[] = $harObject->log->entries[0]->startedDateTime;
$currentIndex = 0;

foreach ($harObject->log->entries as $entry) {
foreach ($urls as $index => $url) {
if ($index > $currentIndex && $url !== $urls[$currentIndex] && $entry->request->url === $url) {
$pageStartedDateTimes[] = $entry->startedDateTime;
$currentIndex = $index;
}
}
$entry->pageref = 'Request '. ($currentIndex + 1) . ': ' . $urls[$currentIndex];
}

$pages = [];
foreach ($pageStartedDateTimes as $index => $pageStartedDateTime) {
$page = new \stdClass();
$page->id = 'Request '. ($index + 1) . ': ' . $urls[$index];
$page->startedDateTime = $pageStartedDateTime;
$page->title = 'Request '. ($index + 1) . ': ' . $urls[$index];
$pageTimings = new \stdClass();
$pageTimings->comment = '';
$page->pageTimings = $pageTimings;
$page->comment = '';
$pages[] = $page;
}

$harObject->log->pages = $pages;
return json_encode($harObject);
}

private function normalizeUrls($urls)
{
$normalizedUrls = [];
foreach ($urls as $url) {
if (strstr($url, '#')) {
$url = substr($url, 0, strpos($url, '#'));
}
$normalizedUrls[] = $url;
}
return $normalizedUrls;
}
}
2 changes: 1 addition & 1 deletion src/JourneyMonitor/Monitor/JobRunner/LogAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public function pageloadTimeoutOccured($outputLines) {
* those that the Selenium script navigated to, not secondary HTTP requests which resulted from these page loads.
*
* @param string $logContent
* @return Array|String Array of requested URLs
* @return string[] Array of requested URLs
*/
public function getUrlsOfRequestedPages($logContent)
{
Expand Down
14 changes: 11 additions & 3 deletions src/JourneyMonitor/Monitor/JobRunner/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public function __construct(TestcaseRepository $testcaseRepository, $directory,
$this->testcaseModel = $this->testcaseRepository->getById($testcaseId);
}

public function prepare()
public function prepareSeleneseScript()
{
file_put_contents(
$this->directory . DIRECTORY_SEPARATOR . 'journeymonitor-testcase-' . $this->testcaseModel->getId() . '.html',
Expand All @@ -28,6 +28,8 @@ public function prepare()

public function run($retry = 0)
{
$this->prepareSeleneseScript();

$found = false;
while (!$found) {
$jobId = mt_rand(1, 999999999);
Expand Down Expand Up @@ -61,6 +63,13 @@ public function run($retry = 0)

$har = file_get_contents('/var/tmp/journeymonitor-testcase-run-' . $jobId . '-har');

$la = new LogAnalyzer();
$ht = new HarTransformer();

$urls = $la->getUrlsOfRequestedPages($output);

$transformedHar = $ht->splitIntoMultiplePages($har, $urls);

unlink('/var/tmp/journeymonitor-testcase-run-' . $jobId . '-har');
unlink('/var/tmp/journeymonitor-testcase-run-' . $jobId . '.lock');
unlink('/var/tmp/journeymonitor-testcase-run-' . $jobId . '-exit-status');
Expand All @@ -70,7 +79,6 @@ public function run($retry = 0)
print('Internal selenium-runner error, trying again...' . "\n");
return $this->run($retry + 1);
} else {
$la = new LogAnalyzer();
$failScreenshotFilenameWithoutExtension = null;
foreach ($output as $line) {
if ($la->lineContainsScreenshot($line)) {
Expand All @@ -85,7 +93,7 @@ public function run($retry = 0)
}
}

return new TestresultModel(TestresultModel::generateId(), $this->testcaseModel, $datetimeRun, $exitCode, $output, $failScreenshotFilenameWithoutExtension, (string)$har);
return new TestresultModel(TestresultModel::generateId(), $this->testcaseModel, $datetimeRun, $exitCode, $output, $failScreenshotFilenameWithoutExtension, $transformedHar);
}
}
}
27 changes: 27 additions & 0 deletions tests/JourneyMonitor/Monitor/JobRunner/HarTransformerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace JourneyMonitor\Monitor\JobRunner;

use PHPUnit_Framework_TestCase;

class HarTransformerTest extends PHPUnit_Framework_TestCase
{
public function testSplitIntoMultiplePages()
{
$originalHar = file_get_contents(__DIR__ . '/../../../fixtures/testrun.original.har.json');
$transformedHar = file_get_contents(__DIR__ . '/../../../fixtures/testrun.transformed.har.json');

// Working around the fact that the JSON in the file is "beautified".
$transformedHar = json_encode(json_decode($transformedHar));

$urls = [
0 => 'https://www.galeria-kaufhof.de/',
1 => 'https://www.galeria-kaufhof.de/search?q=hose',
2 => 'https://www.galeria-kaufhof.de/',
];

$ht = new HarTransformer();

$this->assertEquals($transformedHar, $ht->splitIntoMultiplePages($originalHar, $urls));
}
}
4 changes: 2 additions & 2 deletions tests/JourneyMonitor/Monitor/JobRunner/RunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class RunnerTest extends \PHPUnit_Framework_TestCase
{
private $vfsRoot;

public function test() {
public function testPrepare() {
$stubTestcaseModel = new TestcaseModel('b', 'The Foo', 'han.solo@rebels.org', '*/15', 'foo-b');

$mockTestcaseRepository = $this->getMockBuilder(TestcaseRepository::class)
Expand All @@ -26,7 +26,7 @@ public function test() {
$this->vfsRoot = vfsStream::setup('test');

$runner = new Runner($mockTestcaseRepository, vfsStream::url('test'), 'b');
$runner->prepare();
$runner->prepareSeleneseScript();

$this->assertSame(
'foo-b',
Expand Down
Loading

0 comments on commit 382bbb8

Please sign in to comment.