Comments

beginner’s picture

Project: SimpleTest » Drupal core
Version: 6.x-2.x-dev » 7.x-dev
Component: Code » simpletest.module
Category: feature » task

#ahah is part of Drupal's Form API.
There ought to be a way to test it.

calebgilbert’s picture

Status: Needs work » Active

Selenium has a robust api for testing AHAH/JS/AJAX. I've written an article with accompanying screencast about using Selenium on a Drupal site.

Interestingly enough, Simpletest's own documentation encourages people to look elsewhere for testing AHAH/JS/AJAX (bottom of page):

Most significant is a new generation of tools that run directly in the web browser are now available. These include Selenium and Watir. They are non-trivial to set up and slow to run, but can essentially test anything. As SimpleTest does not support JavaScript you would probably have to look at these tools anyway if you have highly dynamic pages

damien tournoud’s picture

Status: Active » Needs review
StatusFileSize
new4.55 KB

Here is a first patch to test AHAH replacements.

We implement the drupalAHAH() that simulate AHAH replacement in our SimpleTest internal browser.

Completely untested at this point.

damien tournoud’s picture

Status: Needs review » Needs work

The last submitted patch failed testing.

damien tournoud’s picture

Status: Needs work » Needs review
StatusFileSize
new16.4 KB

This is the first working patch. It still depends on #348634: Ease AHAH testing: allow direct posting.

The API for AHAH testing is very simple. Simply call $this->drupalAHAH() with the label of the button you want to activate (this will require some light refactoring to allow other types of widgets to be tested):

    // Load the initial form.
    $this->drupalGet("simpletest-test/form");

    // Method: 'replace' on the ahah2_internal wrapper.
    $this->drupalAHAH(t("Replace content"));

drupalAHAH() does everything for you: identifying where to perform the request, loading the JSON and replacing the correct DOM node. If drupalAHAH(), the content of the internal browser is changed, and you can perform assertion on it, or even using drupalPost() if you want.

damien tournoud’s picture

StatusFileSize
new15.27 KB

New version of the same patch, now with an extended API (and slightly less kitten killed in the process).

You know have to pass an element ID to drupalAHAH(). The benefit of this is that it now works with every element type. So if I have:

  $form['more-elements'] = array(
    '#type' => 'submit',
    '#value' => t('More choices'),
    '#description' => t("If the amount of boxes above isn't enough, click here to add more choices."),
    '#weight' => 1,
    '#submit' => array('poll_more_choices_submit'), // If no javascript action.
    '#ahah' => array(
      'path' => 'poll/js',
      'wrapper' => 'poll-choices',
      'method' => 'replace',
      'effect' => 'fade',
    ),
  );

I will call:

    // Load the initial form.
    $this->drupalGet('my/form');

    // Trigger an AHAH on the "More choices" button.
    $this->drupalAHAH('edit-more-choices');

And enjoy dynamic AHAH replacement in SimpleTest :)

Status: Needs review » Needs work

The last submitted patch failed testing.

dmitrig01’s picture

You have this:
<div id="ahah5_external"><div id="ahah5_internal">original content</div></div>

Only one id per page - they are unique.

Dmitri

beginner’s picture

Re #9: Those are different IDs. one says: external, the other says: internal.

damien tournoud’s picture

Status: Active » Needs review
StatusFileSize
new26.56 KB

This is a merge of #348634: Ease AHAH testing: allow direct posting with this patch.

damien tournoud’s picture

StatusFileSize
new27.7 KB

Took care of some comments by Dries in #348634: Ease AHAH testing: allow direct posting:

- 'url': the effective URL used. I assume that 'effective' relates to the fact that we can have redirects, and that 'effective URL' means that we return the last URLs in the chain of redirects. It would be good to see that confirmed or otherwise clarified in the phpDoc.

Exactly, clarified.

- When I read $this->drupalSetContent($response['content'], $response['url']);, I want to rename that function to drupalSetResponse(). In my mind, the URL is not content. Anyway, that might be outside the scope of this patch, and I have not looked at all uses of drupalSetContent. Something is itching though.

That's a bit kitten killing, but I renamed this to drupalSetResponse(), fixed and updated the PHPDoc accordingly.

- Maybe drupalPost and drupalPostDirect should have a 'see also' so they reference each other?

Agreed. Fixed.

- Change without checking the validity of posted values to without checking the validity of posted values against the form?
- Unlike drupalPost(), this function doesn't load the result in the internal browser -- I'm wondering why.

This is because drupalPostDirect() bypass the internal browser completely. I updated the PHPDoc to clarify this.

damien tournoud’s picture

StatusFileSize
new29.41 KB

Forgot to merge the other uses of drupalSetContent().

damien tournoud’s picture

StatusFileSize
new29.42 KB

And fixed the patch description.

damien tournoud’s picture

StatusFileSize
new32.12 KB

Rerolled. Ran the test suite manually on this, 100% pass.

Status: Needs review » Needs work

The last submitted patch failed testing.

damien tournoud’s picture

Status: Needs work » Needs review
StatusFileSize
new31.67 KB

Rerolled following the node/8 patch.

damien tournoud’s picture

StatusFileSize
new28.53 KB

Extracted the renaming of SimpleTestTestCase in a separate issue (#357595: Rename SimpleTestTestCase to SimpleTestFunctionalTest).

Status: Needs review » Needs work

The last submitted patch failed testing.

damien tournoud’s picture

StatusFileSize
new28.44 KB

Free reroll.

damien tournoud’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch failed testing.

wim leers’s picture

Subscribing.

boombatower’s picture

Status: Needs work » Postponed

I would really like to see #340283: Abstract SimpleTest browser in to its own object get in and integrated with SimpleTest then make the changes to it.

rfay’s picture

Status: Postponed » Needs work

OK, so now #340283: Abstract SimpleTest browser in to its own object is in. Can we get this in?

rfay’s picture

Let's have BOF about this subject at Drupalcon Paris. This is terribly important stuff: Drupal depends on javascript, and we're not testing any behaviors that involve JS. For example, HEAD was broken day before yesterday because a new JS piece was introduced that broke install.php. Our automated test framework did not tell us this, because there's no JS piece.

Imitating or simulating JS is not really adequate, although it's something. Unfortunately, when the JS changes (as it will) the test breaks. Since we aim to test the code, not code to the test, this really isn't right. Let's take a look at Selenium, let's talk about what has to be done here.

#237566: Automated JavaScript unit testing framework proposes an automated JS test framework. I'll note the idea of a BOF there too.

rfay’s picture

There is some nice work going on to support Javascript testing #237566: Automated JavaScript unit testing framework. Reviews there are welcome. With a little more progress we may be able to do functional testing, allowing us to test #ajax.

boombatower’s picture

I discussed this with webchick and we decided the best approach was to test the autocomplete widget generically (once) through js testing framework and test all the callbacks and such through SimpleTest since it has access to the data and can easily parse json.

Other areas of js testing like proper AHAH form building and such will need to wait until we have the proper environment separation for functional testing, which will be a followup to #237566: Automated JavaScript unit testing framework.

rfay’s picture

Version: 7.x-dev » 8.x-dev
rfay’s picture

nod_’s picture

Title: Test #ahah and ajax » Test #ajax
Issue tags: +JavaScript

just putting the right name, not called ahah for a few years now :)

nod_’s picture

Issue summary: View changes
Status: Needs work » Closed (duplicate)

We want to test ajax stuff, but not by messing with our API too much, by using behat #2232271: [Meta] Use Behat for validation testing.