Simpletest Web Tester question

Discussion of testing theory and practice, including methodologies (such as TDD, BDD, DDD, Agile, XP) and software - anything to do with testing goes here. (Formerly "The Testing Side of Development")

Moderator: General Moderators

Post Reply
georgeoc
Forum Contributor
Posts: 166
Joined: Wed Aug 09, 2006 4:21 pm
Location: London, UK

Simpletest Web Tester question

Post by georgeoc »

Hi all.

I have a form, submitted via POST, which displays a selectable list of 'items' to be installed. The PHP script which receives it installs whichever items were selected, then redirects to a new page using header() after it has finished processing. I have been testing functionality using the Simpletest WebTestCase, and would like to check that resubmitting the same form will result in an error message, as items may not be installed twice. In my browser, I can submit the form, let the page redirect, click the Back button and resubmit the form which will result in the error message I have coded. How can I test this in Simpletest? If I run this:

Code: Select all

$this->assertTrue($this->back());
...the page seems to have been reloaded - i.e. the list of items is now missing those which have just been installed. Any way to do this, either properly or by faking something?
However, running this:

Code: Select all

$this->assertTrue($this->retry());
...is no good either, as it reloads just the GET request for the redirect page, rather than the whole lot (reposting the form too).
TIA
Last edited by georgeoc on Mon Feb 18, 2008 9:01 am, edited 1 time in total.
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post by sweatje »

Maybe you need to go back twice to get to the form instead of the post redirect?
georgeoc
Forum Contributor
Posts: 166
Joined: Wed Aug 09, 2006 4:21 pm
Location: London, UK

Post by georgeoc »

Hmm. When I run back() then look at the current page source using showSource(), it is showing the form again, but the items I have just installed are missing from the list - i.e. I have no chance to resubmit the same form. I suspect Simpletest can't do what I want easily, and I might have to fiddle around with sending POST data using the test suite.
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post by sweatje »

Perhaps you should just make a helper function to fill out the form (in your test case) so it is trivial to reset the fields and repost the form.
georgeoc
Forum Contributor
Posts: 166
Joined: Wed Aug 09, 2006 4:21 pm
Location: London, UK

Post by georgeoc »

Either I'm being thick, or I haven't properly explained the problem. Both, probably. :wink:

The form is generated dynamically, and has a list of uninstalled items with associated checkboxes. I select a few of them, post the form, and I'm redirected to another page. I want to test what happens when I go back to the form and resubmit it - i.e. posting a form with the same array of items, all of which have already been installed.

The problem with back() is that it seems to reload the form, rather than using the copy in its history. So if the form has 10 items, I install 3 and then run back(), I get a form with 7 items.

You see the problem? Or is your suggestion still valid?
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post by sweatje »

Okay, perhaps a completely different tact then. Grab a clone of the simplebrowser object just before submitting the form, and then use that cloned object to resubmit. If you want to really hack simpletest, replace the current browser and use the regular assertions. If you want to play nicer, use the cloned simplebrowser object directly and parse the output directly.
georgeoc
Forum Contributor
Posts: 166
Joined: Wed Aug 09, 2006 4:21 pm
Location: London, UK

Post by georgeoc »

Nice idea. I'll have a look.

Thanks Jason (oh, and thanks for your book, and turning on the light in the dark cellar of Design Patterns....!)
georgeoc
Forum Contributor
Posts: 166
Joined: Wed Aug 09, 2006 4:21 pm
Location: London, UK

Post by georgeoc »

It's working already. FWIW - here is what I did. (Channels in this excerpt are what I refer to as items above).

Code: Select all

        $this->assertTrue($this->clickLink('Channels'));
 
        $browser = $this->_browser;
        $this->assertTrue($this->assertField('channel_path[]', FALSE));
        $this->assertTrue($this->setField('channel_path[]', 'channels/email/mbox/mbox.php'));
        $this->assertTrue($this->clickSubmit('Install Selected Channels'));
        
        $this->assertIdentical('1', $res = $this->db->getOne('SELECT COUNT(1) FROM ' . $this->db_prefix . '_channel'));
 
        $this->_browser = $browser;
        $this->assertTrue($this->clickSubmit('Install Selected Channels'));
 
        $this->assertWantedPattern('/mbox.*already installed/i');
        $this->assertIdentical('1', $res = $this->db->getOne('SELECT COUNT(1) FROM ' . $this->db_prefix . '_channel'));
 
Post Reply