Page 1 of 1
Simpletest Web Tester question
Posted: Wed Aug 23, 2006 5:52 pm
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:
...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
Posted: Wed Aug 23, 2006 6:53 pm
by sweatje
Maybe you need to go back twice to get to the form instead of the post redirect?
Posted: Wed Aug 23, 2006 6:57 pm
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.
Posted: Wed Aug 23, 2006 7:01 pm
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.
Posted: Wed Aug 23, 2006 7:08 pm
by georgeoc
Either I'm being thick, or I haven't properly explained the problem. Both, probably.
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?
Posted: Wed Aug 23, 2006 7:12 pm
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.
Posted: Wed Aug 23, 2006 7:14 pm
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....!)
Posted: Wed Aug 23, 2006 7:31 pm
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'));