Currently, HTTP requests via simpletest change the user agent in order to hit the test database rather than the real database. In bootstrap.inc, a check is made to see if the stated request time of the request is within a five-second window, which seems reasonable for _single_ local requests.

However, certain modules, like the Selenium module, and Simpletest PhantomJS, on which I am currently working, attempt to leverage Drupal's Simpletest framework in order to parse Javascript, including Ajax calls.

How the Selenium project deals with the problem

The Selenium module, which integrates with Selenium to test code in real browsers, provides a patch to core which replaces the hard-coded 5 second window by a hard-coded 500-second window.

Example usage for Simpletest PhantomJS integration

PhantomJS is a so-called headless browser which can interpret Javascript. Let's say you have a module which defines a page which is modified by an Ajax call, this results to two http requests. If they happen to be in the same 5-second window, everything works. If the second call is outside the 5-second window, the main database is hit rather than the test database.

My idea is to provide a hook_init implementation within Simpletest PhantomJS to exit if the call to Simpletest is outside the 5-second window, but any test which takes more than 5 seconds will then break.

Proposed solution

Use 5 seconds a default behaviour, but allow site developers to add something like the following in their settings.php file:

$conf['simpletest_time_window'] = 500;
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

alberto56’s picture

Status: Active » Needs review
FileSize
1.29 KB

Here's a first shot at a patch.

Status: Needs review » Needs work

The last submitted patch, 2104139-1-core-7.x-simpletest-dont-hardcode-5-sec-window.patch, failed testing.

alberto56’s picture

Version: 7.x-dev » 8.x-dev
Issue summary: View changes
FileSize
2.47 KB

Here is a version for 8.x

alberto56’s picture

Title: Don't hard code 5 second window for simpletest requests » Don't hard code 5 second window for simpletest requests, opening the door to tests of Ajax
Status: Needs work » Needs review

Status: Needs review » Needs work

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Anonymous’s picture

After #2771547: In Browser and FunctionalJavascript tests SIMPLETEST_USER_AGENT cookie needs to be set every 5 seconds we have:

+++ b/core/includes/bootstrap.inc
@@ -640,11 +640,15 @@ function drupal_valid_test_ua($new_prefix = NULL) {
-    if ($time_diff >= 0 && $time_diff <= 5 && $hmac === $test_hmac) {
+    if ($time_diff >= 0 && $time_diff <= 600 && $hmac === $test_hmac) {

So even though the value is still set hardly, it is changed in favor of what we want to get here. Maybe close this issue?

alberto56’s picture

Status: Needs review » Fixed

Right, this seems fixed to me, thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.