Ver código fonte

Fixing issue where delay wasn't working for screen captures and added tests to verify

Jonny Wenmoth 11 anos atrás
pai
commit
b4939b816c

+ 1 - 3
src/JonnyW/PhantomJs/Resources/procedures/capture.proc

@@ -101,9 +101,7 @@ phantom.onError = function(msg, trace) {
  * @param callable $callback
  */
 page.open ('{{ request.getUrl() }}', '{{ request.getMethod() }}', '{{ request.getBody() }}', function (status) {
-    
-    var delay = Number(delay);
-    
+
     if(!delay) {
         return procedure.execute(status);
     }

+ 98 - 7
src/JonnyW/PhantomJs/Tests/Integration/ClientTest.php

@@ -61,7 +61,7 @@ EOF;
 
         $client = $this->getClient();
         $client->getProcedureLoader()->addLoader($procedureLoader);
-        
+
         $request  = $client->getMessageFactory()->createRequest();
         $response = $client->getMessageFactory()->createResponse();
 
@@ -313,12 +313,12 @@ EOF;
 
     /**
      * Test delay logs start time
-     * in client.
+     * in client for default request.
      *
      * @access public
      * @return void
      */
-    public function testDelayLogsStartTimeInClient()
+    public function testDelayLogsStartTimeInClientForDefaultRequest()
     {
         $delay = 2;
 
@@ -342,12 +342,12 @@ EOF;
 
     /**
      * Test delay logs end time
-     * in client.
+     * in client for default request.
      *
      * @access public
      * @return void
      */
-    public function testDelayLogsEndTimeInClient()
+    public function testDelayLogsEndTimeInClientForDefaultRequest()
     {
         $delay = 2;
 
@@ -371,12 +371,12 @@ EOF;
 
     /**
      * Test delay delays page render for
-     * specified time.
+     * specified time for default request.
      *
      * @access public
      * @return void
      */
-    public function testDelayDelaysPageRenderForSpecifiedTime()
+    public function testDelayDelaysPageRenderForSpecifiedTimeForDefaultRequest()
     {
         $delay = 2;
 
@@ -402,6 +402,97 @@ EOF;
         $this->assertSame(($startTime+$delay), $endTime);
     }
 
+    /**
+     * Test delay logs start time
+     * in client for capture request.
+     *
+     * @access public
+     * @return void
+     */
+    public function testDelayLogsStartTimeInClientForCaptureRequest()
+    {
+        $delay = 2;
+
+        $client = $this->getClient();
+
+        $request  = $client->getMessageFactory()->createCaptureRequest();
+        $response = $client->getMessageFactory()->createResponse();
+
+        $request->setMethod('GET');
+        $request->setUrl('http://jonnnnyw.github.io/php-phantomjs/tests/test-capture.html');
+        $request->setDelay($delay);
+
+        $client->send($request, $response);
+
+        $logs = explode("\n", $client->getLog());
+
+        $startIndex = $this->getLogEntryIndex($logs, 'Delaying page render for');
+
+        $this->assertTrue(($startIndex !== false));
+    }
+
+    /**
+     * Test delay logs end time
+     * in client for capture request.
+     *
+     * @access public
+     * @return void
+     */
+    public function testDelayLogsEndTimeInClientForCaptureRequest()
+    {
+        $delay = 2;
+
+        $client = $this->getClient();
+
+        $request  = $client->getMessageFactory()->createCaptureRequest();
+        $response = $client->getMessageFactory()->createResponse();
+
+        $request->setMethod('GET');
+        $request->setUrl('http://jonnnnyw.github.io/php-phantomjs/tests/test-capture.html');
+        $request->setDelay($delay);
+
+        $client->send($request, $response);
+
+        $logs = explode("\n", $client->getLog());
+
+        $endIndex = $this->getLogEntryIndex($logs, 'Rendering page after');
+
+        $this->assertTrue(($endIndex !== false));
+    }
+
+    /**
+     * Test delay delays page render for
+     * specified time for capture request.
+     *
+     * @access public
+     * @return void
+     */
+    public function testDelayDelaysPageRenderForSpecifiedTimeForCaptureRequest()
+    {
+        $delay = 2;
+
+        $client = $this->getClient();
+
+        $request  = $client->getMessageFactory()->createCaptureRequest();
+        $response = $client->getMessageFactory()->createResponse();
+
+        $request->setMethod('GET');
+        $request->setUrl('http://jonnnnyw.github.io/php-phantomjs/tests/test-capture.html');
+        $request->setDelay($delay);
+
+        $client->send($request, $response);
+
+        $logs = explode("\n", $client->getLog());
+
+        $startIndex = $this->getLogEntryIndex($logs, 'Delaying page render for');
+        $endIndex   = $this->getLogEntryIndex($logs, 'Rendering page after');
+
+        $startTime = strtotime(substr($logs[$startIndex], 0 , 19));
+        $endTime   = strtotime(substr($logs[$endIndex], 0 , 19));
+
+        $this->assertSame(($startTime+$delay), $endTime);
+    }
+
     /**
      * Test debug logs debug info to
      * client log.

+ 2 - 2
src/JonnyW/PhantomJs/Tests/Unit/ClientTest.php

@@ -46,7 +46,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
 
         $this->assertInstanceOf('\JonnyW\PhantomJs\Message\MessageFactoryInterface', $client->getMessageFactory());
     }
-    
+
     /**
      * Test get procedure loader returns
      * instance of proecure loader.
@@ -62,7 +62,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
 
         $this->assertInstanceOf('\JonnyW\PhantomJs\Procedure\ProcedureLoaderInterface', $client->getProcedureLoader());
     }
-    
+
     /**
      * Test set phantom JS throws invalid
      * executable exception if phantom